Closed
Description
Sam Brannen opened SPR-14060 and commented
Overview
Once #18630 is resolved it will be possible to compose @Autowired
with @Qualifier
.
Consequently, the following common combination of @Autowired
and @Qualifier
:
@Autowired
@Qualifier("holidayDateService")
private DateService dateService;
... can be replaced with:
@Autowired("holidayDateService")
private DateService dateService;
Although this may not appear immediately beneficial for DI for fields or setter methods, it becomes much more appealing once @Autowired
can be applied to parameters (see #18629).
For example, Spring's testing support for JUnit 5 would then be able to support parameter injection succinctly in test methods as follows.
@Test
void businessHolidays(@Autowired("holidayDateService") DateService dateService) {
}
Proposal
Implement @Autowired
as follows.
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Qualifier
public @interface Autowired {
@AliasFor(annotation = Qualifier.class, attribute = "value")
String value() default "";
@AliasFor(annotation = Qualifier.class, attribute = "value")
String qualifier() default "";
boolean required() default true;
}
Issue Links:
- Allow @Autowired to be declared on parameters [SPR-14057] #18629 Allow
@Autowired
to be declared on parameters - Introduce autowiring support for individual handler method parameters [SPR-14056] #18628 Introduce autowiring support for individual handler method parameters