How to pass a parameter to a Spring component?

Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 22, 2013

Hi,

I have a service and I would like to pass it an argument from atlassian-plugin.xml, such as:

<component key="playsql-service" class="com.playsql.Service">
  <property name="param1">configuration.xml</property>
</component>

Then the Java class would be:

public class Service {
  public Service(String param1) {
   // I expect to receive "configuration.xml" in param1
  }
}

NB. I can see that they speak of Service Parameters on https://developer.atlassian.com/display/PLUGINFRAMEWORK/Component+Plugin+Module but I couldn't figure how to use them.

Thank you!

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
David Pinn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 28, 2013

The service-properties element can be used to assign values to certain keys within the atlassian-plugin.xml file; but it isn't the right tool for the job you have.

The service-properties element is used when you want to use the filter attribute of the component-import element.

You should set up your own Spring configuration file as described in the Component Plugin Module documentation:

"If you wish to have more control over how your components are created and configured, you can create your own Spring configuration file, stored in META-INF/spring in your plugin jar."

You can use normal Spring xml constructs in your Spring configuration file, which you should place in the META-INF/spring directory under your resources directory.

0 votes
Kul Bhushan Prasad August 14, 2015

Got the way, As mentioned by David, there is a need to create Spring Configruation file and should be stored in META-INF/spring folder and that will be loaded automatically.

Here are the steps to do it while developing the Plugin.

1) Create the Spring configruation file under the folder like

\src\main\resources\META-INF\spring\plugin-context.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <bean id="MyExternalWebService"
          class="com.company.confluence.services.MyExternalWebService">
        <constructor-arg value="url" />
        <constructor-arg value="user" />
        <constructor-arg value="password" />
    </bean>
</beans>

2) Now simply inject this object into Target class like

 

public void setMyExternalWebService(MyExternalWebService myExternalWebService){
this.myExternalWebService = myExternalWebService;
}

3) Done, you will get the object of MyExternalWebservice by setter injection. Go ahead and use it.

0 votes
danielwester
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 23, 2013
I've never used the service properties but according to the doc you'd need to do: <component key="playsql-service" class="com.playsql.Service"> <service-properties> <entry key="param1" value="configuration.xml"/> </service-properties> </component> Make sure to check the plugin framework is 2.3 or above.
Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 23, 2013
How do you then write the class or the constructor to be injected this value?
Kul Bhushan Prasad August 13, 2015

+1 to Adrien: How do you inject the value as constructor injection. please provide the example

TAGS
AUG Leaders

Atlassian Community Events