I'm trying to migrate a v1 to a v2 OSGI based Plugin. Our Plugin has a LifeCycleItem which is registered as a lifecycle module in the atlassian-plugin.xml like this:
<atlassian-plugin key="${project.plugin.key}" name="${project.plugin.name}" enabled="true" plugins-version="2"> <!-- ... --> <component key="myComponent" class="com.company.MyComponentImpl"> <interface>com.company.MyComponent</interface> </component> <lifecycle key="mylifecyle" name="My LifeCycle" class="com.company.MyComponent" sequence="1200"> <description/> </lifecycle> <!-- ... --> </atlassian-plugin>
Right now, the LifeCycleItem is trying to access this component as the following:
public class MyLifeCycle implements LifecycleItem { private MyComponent myComponent; public void startup(LifecycleContext lifecycleContext) throws Exception { // ... myComponent = (MyComponent) ContainerManager.getComponent("myComponentKey"); myComponent.doSomething(lifecycleContext); // ... } // ... }
I know I can't use "ContainerManager.getComponent()" any more. But when I try to use simple dependency injection (as mentioned here) using a setter or a constructor, I either get a NullpointerException or a BeanInstantiationException (saying "No default constructor found").
The following code snippets are examples how I actual try to get the component:
This example results in a NullPointerException:
public class MyLifeCycle implements LifecycleItem { private MyComponent myComponent; public void startup(LifecycleContext lifecycleContext) throws Exception { // ... myComponent.doSomething(lifecycleContext); // ... } public void setMyComponent (MyComponent myComponent) { this.myComponent= myComponent; } // ... }
The follwing results in BeanInstantiationException (saying "No default constructor found"):
public class MyLifeCycle implements LifecycleItem { private MyComponent myComponent; public MyLifeCycle(MyComponent myComponent) { this.myComponent= myComponent; } public void startup(LifecycleContext lifecycleContext) throws Exception { // ... myComponent.doSomething(lifecycleContext); // ... } // ... }
So my final question is: How can I access a component in a Lifecycle Item?
Thanks for any hint or advice!
This question is from 2014, there is still no answer, and DI in LifecycleItems still does not work. Says it all.
I've solved this issue now using a StaticAccessor. But this leads to the question, if the spring context on startup is already available, why is a LifeCycle Item not autowired?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.