Skip to content

Honor @Autowired(required=false) at parameter level, as an alternative to java.util.Optional [SPR-15268] #19833

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Referenced from: commits d74542e, beac891

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions