How to fix 'Injection of autowired dependencies failed' issue?
Asked by Krzysztof Novak:

Hi,

We are looking at developing/enhancing a Bamboo plugin. We've come across a problem with the Atlassian IOC mechanism. The problem is a failure to resolve an auto-wired (via Autowire annotation) bean called "linkedDeploymentProjectCacheService" in the Atlassian SDK class com.atlassian.bamboo.ww2.BambooActionSupport. Our plugin class com.samsung.test.autowire.view.ViewChainResultsAction indirectly inherits from BambooActionSupport.

The Bamboo server throws the following exception when it attempts to install the plugin:

```
2016-04-07 09:48:23,953 ERROR [localhost-startStop-1] [BambooPluginUtils] A problem has occurred when instantiating action class [com.samsung.test.autowire.view.ViewChainResultsAction], skipping action [viewChainChangeResults]org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.samsung.test.autowire.view.ViewChainResultsAction': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atlassian.bamboo.deployments.cache.LinkedDeploymentProjectCacheService com.atlassian.bamboo.ww2.BambooActionSupport.linkedDeploymentProjectCacheService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.atlassian.bamboo.deployments.cache.LinkedDeploymentProjectCacheService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:342)    at sun.reflect.GeneratedMethodAccessor148.invoke(Unknown Source)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:497)    at com.atlassian.plugin.osgi.spring.DefaultSpringContainerAccessor.createBean(DefaultSpringContainerAccessor.java:97)    at com.atlassian.bamboo.plugin.xwork.PluginAwareObjectFactory.autowireIfContainerManagerPlugin(PluginAwareObjectFactory.java:69)
```

Our project skeleton (pom.xml, atlassian-plugin.xml etc.) has been generated by the latest version of the Atlassian SDK 6.2.4 tool atlas-create-bamboo-plugin against Bamboo 5.10.3.

Any clues?


Answers:
author: Przemek Bruski
body: As a workaround, try adding a component import statement in your plugin xml for linkedDeploymentProjectCacheService (com.atlassian.bamboo.deployments.cache.LinkedDeploymentProjectCacheService).

We will fix it in one of the forthcoming Bamboo releases.

comments: author: Krzysztof Novak
body: Hi Przemek.

Thanks for a prompt reply and a helpful suggestion.

Our action class inherits from com.atlassian.bamboo.ww2.actions.chains.ViewChainResult, and this forced me to add a few more imports. This allowed me to install my test plugin without the server throwing an exception. The plugin itself seems to be functional. Still, the UPM reports an error after the installation without anything specific present in the server logs.

Also, including a component-import tag in the SDK-generated atlassian-plugin.xml forces me to comment out the \<Atlassian-Plugin-Key\> tag. Apparently, the effect of that is that I no longer have a 'transformer-less plugin' (you wouldn't happen to know if this is actually documented anywhere?). Is that likely to be an issue?

Cheers,

Krzysztof Novak


author: Krzysztof Novak
body: By the way, you wouldn't happen to know when the next Bamboo release is going to be available?


author: Krzysztof Novak
body: Unfortunately, after further testing, the plugin, despite installing more or less successfully, failed at run time. The reason for the failure was another IOC problem, this time in the direct parent of our action class: com.atlassian.bamboo.ww2.actions.chains.ViewChainResult. More specifically, it has two fields: deploymentProjectService and commentService, both annotated with Autowired.

The \<component-import\> trick fails to fix this particular problem. I assume that those issues stem from the switch from Spring DM to Gemini Blueprint. The whole mechanism seems to be quite fragile and rather poorly documented at the moment. Hopefully, the upcoming release will fix that.


author: Przemek Bruski
body: If you have a transformerless plugin, you can use the \\@Import annotation on a reference to a com.atlassian.bamboo.deployments.cache.LinkedDeploymentProjectCacheService (and other beans) on a scanned bean somewhere in your plugin. This will pull the reference into your plugin context.

The next release should be released next week. The fix to your issue won't be available till 5.11.1, we only fixed the problem on Thursday and the change is too big to ship right away.

It won't fix the ViewChainResult problem though. BambooActionSupport is a general purpose class and the inclusion of these dependencies there was a mistake. ViewChainResult needs these references wired to operate. You will need to find a way to have it wired properly. I am a bit surprised that component-import didn't fix it. Maybe adding \\@Import annotations will? Are you able to wire these beans in your own components? They are both plugin-available, so you should be able to do this. If you can't or you can but they are still unavailable to your action, it's definitely worth looking into.


author: Krzysztof Novak
body: Thanks for all the suggestions. Good to hear that the problem is going to be fixed.

For now, I have managed to find a workaround by changing our action class to inherit from ChainResultsAction rather than ViewChainResults. Luckily, ChainResultsAction (and its ancestors) doesn't use @Autowire annotation which appears to be the source of our problem.

As for wiring the references myself, I have tried a lot of different things. I even rammed them into the parent class using Java Reflections, but it was such an ugly hack that I kept looking for something more appealing.

IMHO, the relatively new mechanism with annotations and the Spring scanner plugin needs far more comprehensive documentation. The most useful would be a detailed tutorial and a collection of various working examples with different types of modules.


author: Markus Schulte
body: In my case (Bamboo 5.11.0), this workaround works, but I had to remove "Atlassian-Plugin-Key" from pom.xml (as created by atlas-create-bamboo-plugin), too.

I have no clue, how this removing "Atlassian-Plugin-Key" affects the build, but I hope, the reason for this issue will be resolved until I am ready for releasing my plugin.

```
diff --git a/pom.xml b/pom.xml&lt;build&gt;&lt;plugins&gt;&lt;plugin&gt; &lt;groupId&gt;com.atlassian.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-bamboo-plugin&lt;/artifactId&gt; &lt;instructions&gt;  &lt;configuration&gt;-  &lt;Atlassian-Plugin-Key&gt;${atlassian.plugin.key}&lt;/Atlassian-Plugin-Key&gt;+  &lt;!-- &lt;Atlassian-Plugin-Key&gt;${atlassian.plugin.key}&lt;/Atlassian-Plugin-Key&gt; --&gt;
```


author: Krzysztof Novak
body: "Atlassian-Plugin-Key" marks a plugin as 'transformerless' i.e. its JAR is **not** processed by the Atlassian installer and it's assumed to have a complete and functional IOC configuration inside its JAR (META-INF/spring folder). I also had to comment it out for my plugin. See <https://developer.atlassian.com/docs/advanced-topics/configuration-of-instructions-in-atlassian-plugins> for some, but not a lot of, details :smile:.


Tags: bamboo-development