Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Why is Spring Autowire not working for my JIRA plugin classes?

Ryan M Hardy January 16, 2012

I have my JIRA plugin separated out into a few different Maven projects, one with JIRA-agnostic core code, one with common JIRA integration code, and a JIRA plugin project (i.e. using Atlassian SDK for building/testing) with deployment-specific customization, with dependencies on the other projects. I am using Spring in the non-JIRA plugin projects without any issues. However, when testing my plugin in the refapp (via call to atlas-run or atlas-debug), I am getting failures during initialization due to null pointers; stepping through the code reveals that any dependency that is to be populated via the Autowire annotation is NOT getting set. My Spring config works fine in the framework projects (I have a decent test framework in place). I have just today started to dig into the docs regarding mixing Spring config in with the Atlassian Plugin config XML, and I believe I have all as it should be (from https://developer.atlassian.com/display/PLUGINFRAMEWORK/Advanced+Configuration+with+Spring+XML+Files and related articles). Now, I know that one solution is to go in and manually set ALL properties explicitly in the Spring config, but this REALLY defeats the awesomeness of the Autowire mechanism. Any help here is appreciated....

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Ryan M Hardy January 16, 2012

I added the following line to the top of my Spring config in my JIRA Plugin project:

<context:annotation-config />

I was doing a component-scan in my other projects, but not in the plug-in project, and so was not getting a AutowiredAnnotationBeanPostProcessor registered. This rectified issue.

1 vote
Higashi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 10, 2015

To be able to use @Autowired you have to: 

  • enable spring auto-scan in /META-INF/spring/*.xml file with:

    <context:component-scan base-package="foo.bar.package" />
    <context:annotation-config />
  • In above xml file, remember to add parameter to <beans /> tag: default-autowire="autodetect"

  • If referencing JIRA-container bean ex. com.atlassian.jira.security.JiraAuthenticationContext, it would be good to add it's import to above file:

    <osgi:reference id="authenticationContext" interface="com.atlassian.jira.security.JiraAuthenticationContext"/>
  • Finally, your class that imports witth @Autowired have to have default, non argument constructor

 

More information: https://developer.atlassian.com/docs/atlassian-platform-common-components/plugin-framework/behind-the-scenes-in-the-plugin-framework/automatic-generation-of-spring-configuration

You may hopefully inject external components with OSGI annotation  though, still by annotating setter: @org.springframework.osgi.extensions.annotation.ServiceReference

You may also be interested in this plugin: https://bitbucket.org/atlassian/atlassian-spring-scanner

TAGS
AUG Leaders

Atlassian Community Events