I'm working with a plugin that we are converting from dependency injection using an atlassian-plugin.xml configuration, to using a standard Spring DM configuration.
In other words, we are switching out these:
<component> / <component-import> / <module-type>
With an annotation based Spring-scanner and class annotations:
##META-INF/spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:osgi="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-5.0-PREVIEW.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-5.0-PREVIEW.xsd">
<context:component-scan base-package="no.resight.jira.plugins.booking"/>
</beans>
...
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.activeobjects.external.ActiveObjects;
...
@Component
public class MyClass
{
@AutoWire
public MyClass(ActiveObjects ao, GroupManager gm) {
//Do something with ActiveObjects etc..
}
}
The issue is that I cannot figure out how to inject the standard Jira API/Core classes using Spring, as illustrated in the class definition above. Both injected classes above gives an NoSuchBeanDefinitionException.
Does anyone know how to properly set up Spring to look for beans not found in the namespace of my own plugin?
FYI: I am trying to avoid using the Atlassian Spring Scanner 2 library due to it's limitations related to configuring Spring profiles.
So, I believe I found a solution to the issue of injecting JIRA-components when using a pure Spring 5 XML configuration. The trick was in using the <osgi:reference>-tag (more info).
Example class:
package no.resight.powercatch.pcpluginupdater.startupbeans;
import com.atlassian.jira.config.properties.ApplicationProperties;
import com.atlassian.jira.security.groups.GroupManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SpringTestBean {
private final GroupManager groupManager;
private final ApplicationProperties applicationProperties;
@Autowired
public SpringTestBean(GroupManager groupManager, ApplicationProperties applicationProperties) {
this.groupManager = groupManager;
this.applicationProperties = applicationProperties;
System.out.println("HELLO FROM SPRINGTESTBEAN");
}
}
Example configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/osgi/spring-osgi-2.0-m1.xsd
">
<osgi:reference id="groupManager" interface="com.atlassian.jira.security.groups.GroupManager"/>
<osgi:reference id="applicationProperties" interface="com.atlassian.jira.config.properties.ApplicationProperties"/>
<context:component-scan base-package="no.resight.powercatch.pcpluginupdater"/>
</beans>
Things to note:
Hope that helps someone!
UPDATE:
Is it turns out, Spring DM is in fact deprecated in favour of Gemini Blueprint. GB is the direct successor of DM, and the documentation can largely be shared between them. The only change required to use GB is to simplify the XML in the example above:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:context="http://www.springframework.org/schema/context">
<!-- Any Jira interface that should be used for dependency injection must be declared here (this loosely replaces the legacy <component-import> statement) -->
<reference id="groupManager" interface="com.atlassian.jira.security.groups.GroupManager"/>
<!-- Scan the given package for bean declarations and annotations (@Component, @Autowire, etc.) -->
<context:component-scan base-package="no.resight.jira.plugins.booking"/>
</blueprint>
Hi Tormod Haugene.
Don't you have any skeleton for jira plugin project based on Spring? Could you please share it?
I've spent a lot of time, but still not able to create it :(
Many thanks in advance!
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.