How do you inject a list of objects?

Say I have an interface, like WidgetGroker, and I have several implementations of WidgetGroker. I want my Component to have a class layout like this:

class JiraEventListener {

private Collection<WidgetGroker> grokers;

 

public JiraEventListener(Collection<WidgetGroker> theGrokers) {

    this.grokers = theGrokers;

}

 

@EventListener

public void eventListener(...) { .... }

// ...

}

I tried this and spring exploded with "no unique bean" errors or "no matching ctor" or whatever. Is there a way to do this? I have a <component> thing in my atlassian-plugin.xml for each implementation of WidgetGroker as well as a proper listener definition for JiraEventListener.

Thanks!

-Carl

EDIT: formatting fail =(

1 answer

1 accepted

3 votes
Accepted answer

You can do that by adding a Spring configuration file in resources/META-INF/spring/plugin-components.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:osgi="http://www.springframework.org/schema/osgi"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                                 http://www.springframework.org/schema/osgi
                                 http://www.springframework.org/schema/osgi/spring-osgi.xsd"
             default-autowire="autodetect"&gt;

  &lt;bean id="jiraEventListener" class="com.company.product.package.JiraEventListener"&gt;
    &lt;constructor-arg&gt;
      &lt;list&gt;
        &lt;ref bean="groker1"/&gt;
        &lt;ref bean="groker2"/&gt;
        &lt;ref bean="groker3"/&gt;
      &lt;/list&gt;
    &lt;/constructor-arg&gt;
  &lt;/bean&gt;

&lt;/beans&gt;

Awesome! I will try this out and accept your answer when I get it working =D

Hi Carl, let us know if this answer works for you :)

Sorry it took me so long to find time to test this answer - it totally works for me! Thanks very much! =D

BTW, if your constructor takes multiple arguments, you probably have to list them too - so my actual code looks something like this:

&lt;bean id="blah"&gt;
  &lt;constructor-arg ref="arg1"/&gt;
  &lt;constructor-arg ref="arg1"/&gt;
  &lt;constructor-arg ref="arg2"/&gt;
  &lt;constructor-arg&gt;
    &lt;list&gt;
      &lt;ref bean="arg3-item1"/&gt;
      &lt;ref bean="arg3-item2"/&gt;
    &lt;/list&gt;
  &lt;/constructor-arg&gt;
  &lt;constructor-arg ref="arg4"/&gt;
  &lt;!-- .... --&gt;
 &lt;/bean&gt;

EDIT: fixed the code - recently realized it wasn't quite right.

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...

314 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