Autowiring a collection via the constructor with Spring

后端 未结 4 1265
栀梦
栀梦 2021-02-07 01:31

I have what seems to be a simple problem, as stated in the title. Here is the kind of class I have :

public class Foo {
    @Autowired
    public Foo(@Qualifier(         


        
4条回答
  •  死守一世寂寞
    2021-02-07 01:48

    Autowiring collections is not possible using the @Autowired annotation. An autowired collection means "to provide all beans of a particular type". Using the JSR-250 @Resource annotation, you can declare that you want a resource injected by its name, not its type. Or you inject the dependency explicitly.

    [...] beans which are themselves defined as a collection or map type cannot be injected via @Autowired since type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection/map bean by unique name.

    See the Spring documentation for more details.

提交回复
热议问题