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

Florian Sauter October 28, 2014

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

1 vote
Christoph Kober November 29, 2016

This question is from 2014, there is still no answer, and DI in LifecycleItems still does not work. Says it all.

0 votes
Florian Sauter October 29, 2014

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
TAGS
AUG Leaders

Atlassian Community Events