Honor @Autowired(required=false) at parameter level, as an alternative to java.util.Optional [SPR-15268] #19833
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
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:
@Autowired
to be declared on parameters ("depends on")Referenced from: commits d74542e, beac891
The text was updated successfully, but these errors were encountered: