Repository navigation
Description
Chris Beams opened SPR-8519 and commented
A number of issues (see linked issues) have been brought up over time regarding confusion about how to use annotation-based injection for Spring beans that implement java.util.Collection. This issue aims to provide a single and definitive place to address these concerns.
Consider a Spring bean "fooList" of type java.util.List (but could be any bean ultimately implementing java.util.Collection):
@Bean
public List<Foo> fooList() {
List<Foo> fooList = new ArrayList<Foo>();
fooList.add(new Foo(1));
fooList.add(new Foo(2));
return fooList;
}
And consider a Spring @Component "fooService" that depends on this collection. It is natural to imagine using @Autowired or @Inject to express the dependency:
@Component
public class FooService {
private List<Foo> fooList;
@Autowired
public setFooList(List<Foo> fooList) {
this.fooList = fooList;
}
}
This configuration will fail with an exception similar to the following:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of
type [Foo] found for dependency [collection of Foo]: expected at least 1 bean which
qualifies as autowire candidate for this dependency. Dependency annotations: {}
The reason for this is that when injecting an object of type List<Foo>, @Autowired expects one or more individual Spring beans of type Foo to exist in the container. Any such beans are added to a new list of type Foo and injected. Therefore, the @Autowired method signature above could be read as "Please collect all Spring beans of type Foo into a List<Foo> and inject that list into my setFooList setter method".
These semantics are at odds with the actual intent in the situation above, which reads more like: "Please find and inject the Spring bean of type List<Foo> into my setFooList setter method".
The situation is often complicated by the fact that asking to inject the Spring bean of type List<Foo> is meaningless at runtime due to type erasure in Java generics. For this reason, it makes sense to try to qualify the bean by name, as below:
@Component
public class FooService {
private List<Foo> fooList;
@Autowired
public setFooList(@Qualifier("fooList") List<Foo> fooList) {
this.fooList = fooList;
}
}
Unfortunately, the effect is the same, rendering an exception similar to the following:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of
type [Foo] found for dependency [collection of Foo]: expected at least 1 bean which
qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Qualifier(value=fooList)}
Again, this is due to the semantics of @Autowired collections, as described above.
Note that in the examples above, Spring's @Autowired is interchangeable with JSR-330's @Inject, as is Spring's @Qualifier with JSR-330's @Named. They are processed by the same code and thus have the same semantics with regard to collection injection.
This issue then, addresses the common request to provide support for injecting beans of type java.util.Collection using @Autowired+@Qualifier and/or @Inject/@Named as described above.
Issue Links:
- Doc: @Autowired does not support Collection and Map as actual bean types [SPR-4492] #9170 Doc:
@Autowireddoes not support Collection and Map as actual bean types - can't @autowire collections [SPR-5038] #9713 can't
@autowirecollections - NoSuchBeanDefinitionException autowiring to collection with no matching types [SPR-6777] #11443 NoSuchBeanDefinitionException autowiring to collection with no matching types
- @Qualifier fails to resolve Lists containing enumerated types [SPR-7676] #12332
@Qualifierfails to resolve Lists containing enumerated types ("supersedes") - Cannot @Autowired java.util.concurrent.BlockingQueue [SPR-7798] #12454 Cannot
@Autowiredjava.util.concurrent.BlockingQueue ("supersedes") - Unable to inject @Named list [SPR-8237] #12885 Unable to inject
@Namedlist ("supersedes") - @Autowire does not work as expected for Collections based Repository implementation [SPR-3946] #8626
@Autowiredoes not work as expected for Collections based Repository implementation ("supersedes")
Activity
spring-projects-issues commentedon Jul 7, 2011
Chris Beams commented
Resolving this issue as "Won't Fix". As Juergen described in #9713, and has been discussed in other issues linked here, the desired functionality can be acheived in other ways. (1) and (2) below describe the two primary approaches:
(1)
@javax.annotation.Resourcecan be used to inject Collection-typed beans by name:(2) Spring 3.0's
@Valueannotation can be used to reference Collection-typed beans by name using Spring Expression Language (SpEL) #{...} syntax:spring-projects-issues commentedon Jul 7, 2011
Brandon M. commented
Is this similar to this issue with configuring a Map?
http://forum.springsource.org/showthread.php?111420-How-to-Create-a-Map-bean-using-Configuration&p=369398
spring-projects-issues commentedon Jul 7, 2011
Chris Beams commented
Hi Brandon. Yes, both resolutions (1) and (2) above should work to allow you to inject beans of type
Map. So to clarify, this issue addresses beans not only of typeCollection, but also of typeMap.