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
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

350 views 0 1
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