5

What works

Suppose I have a spring bean definition of an ArrayList:

<bean id="availableLanguages" class="java.util.ArrayList">
    <constructor-arg>
        <bean class="java.util.Arrays" factory-method="asList">
            <constructor-arg>
                <list>
                    <value>de</value>
                    <value>en</value>
                </list>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>

Now I can inject this into all kinds of beans, e.g. like this:

@Controller
class Controller {
    @Autowired
    public Controller(ArrayList<String> availableLanguages) {
        // ...
    }
}

This works great.

How it breaks

However if I change my controller a tiny bit and use the type List instead of ArrayList like this:

@Controller
class Controller {
    @Autowired
    public Controller(List<String> availableLanguages) {
        // ...
    }
}

Then instead I get a list of all beans of type String rather then the bean I defined. However I actually want to wrap my List into an unmodifiable List, but this will only be possible if I downgrade my dependency to a list.

So far discovered workaround

The following XML file:

<bean id="availableLanguages" class="java.util.Collections" factory-method="unmodifiableList">
    <constructor-arg>
        <bean class="java.util.Arrays" factory-method="asList">
            <constructor-arg>
                <list>
                    <value>de</value>
                    <value>en</value>
                </list>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>

works together with this controller:

@Controller
class Controller {
    @Autowired
    public Controller(Object availableLanguages) {
        List<String> theList = (List<String>)availableLanguages;
    }
}

While this works the extra type cast is ugly.

Findings so far

I figured that there is a special handling for collections in Spring 4.2.5 (the currently most recent version) which seems to cause all the trouble. It creates special behaviour when a parameter is an interface that extends Collection. Thus I can workaround by using Object or a concrete implementation as parameter type.

Question

Is there any way to directly inject a list into a bean? How?

yankee
  • 38,872
  • 15
  • 103
  • 162
  • You can use namespace `util`. Take a look in [this](http://stackoverflow.com/questions/2416056/how-to-define-a-list-bean-in-spring) asnwer. – josivan Mar 19 '16 at 20:36
  • @josivan: As far as I can see this would change the way how I define the list. However the definition of the list is not the problem. It is the injection that causes the problem or am I missing anything about what `util` does? – yankee Mar 19 '16 at 20:41
  • `util:list` is a regular bean in Spring context, you can inject it normally. I notice that you have a mix of Annotation and XML, then tray to use `@Qualifier` in constructor. For example `@Qualifier("myList")` – josivan Mar 19 '16 at 20:45
  • I'd use a concrete type like 'LanguageList'. I know this does not answer your question but it may solve your problem. – atamanroman Mar 19 '16 at 20:52
  • 1
    I had the same problem few days ago. Check it: http://stackoverflow.com/questions/6267138/spring-autowire-a-list. Use `@Resource` annotation instead of `@Autowired`, it find beans but its name, not y type. – Vladimir Vagaytsev Mar 19 '16 at 22:13

1 Answers1

0

Using @Qualifier will inject the bean with the given qualifier. You can name the list which you want to be a bean and that will work fine.

yaswanth
  • 2,349
  • 1
  • 23
  • 33