Howto access a component in a Confluence LifecycleItem (lifecycle module)

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!

2 answers

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?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

118 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you