Skip to content

Compose @Autowired with @Qualifier [SPR-14060] #18632

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions