Description
Rafael Winterhalter opened SPR-15268 and commented
When declaring a dependency on a list of beans in a Java config, the @Autowired
(required = false) annotation is not respected when no suitable bean exists such as:
@Bean
public Bar bar(@Autowired
(required = false) List<Foo> foos);
The exception states:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bar' defined in dep.DepConfig: Unsatisfied dependency expressed through method 'bar' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<foo.Foo>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org
.springframework.beans.factory.annotation.Autowired(required=false)}
Expressing the constraint on a constructor or field does however work. Also, it is possible to declare the parameter as Optional where an empty optional is provided.
Affects: 4.3.6
Issue Links:
- Allow @Autowired to be declared on parameters [SPR-14057] #18629 Allow
@Autowired
to be declared on parameters ("depends on") - Introduce autowiring support for individual handler method parameters [SPR-14056] #18628 Introduce autowiring support for individual handler method parameters
- Support for List<Provider> injection [SPR-15373] #19937 Support for List injection
- Accept empty Collection<Component> injection for single constructor scenarios [SPR-15338] #19901 Accept empty Collection injection for single constructor scenarios