Skip to content

Support injection of collection-typed beans using @Autowired+@Qualifier or @Inject+@Named [SPR-8519] #13164

Closed

Description

@spring-projects-issues

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:

Activity

  1. spring-projects-issues commented on Jul 7, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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.Resource can be used to inject Collection-typed beans by name:

    @Component
    public class FooService {
        private List<Foo> fooList;
    
        @Resource("fooList")
        public setFooList(List<Foo> fooList) {
            this.fooList = fooList;
        }
    }
    

    (2) Spring 3.0's @Value annotation can be used to reference Collection-typed beans by name using Spring Expression Language (SpEL) #{...} syntax:

    @Component
    public class FooService {
        private List<Foo> fooList;
    
        @Inject // or @Autowired
        public setFooList(@Value("#{fooList}") List<Foo> fooList) {
            this.fooList = fooList;
        }
    }
    
  2. spring-projects-issues commented on Jul 7, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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 type Collection, but also of type Map.

  3. added
    status: declined
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: declinedtype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions