Introduce autowiring support for individual handler method parameters [SPR-14056] #18628
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: superseded
An issue that has been superseded by another
type: enhancement
A general enhancement
Sam Brannen opened SPR-14056 and commented
Status Quo
Spring supports autowiring of dependencies into fields, constructors, and methods; however, there is currently no first-class support for autowiring an individual parameter in a method or constructor.
Spring 5.0's JUnit 5 support demonstrates that it is already possible to implement such a feature with zero changes to core Spring; however, it would be very helpful if the necessary code were hidden behind first-class methods in Spring (see the isAutowirable and resolveDependency methods in
ParameterAutowireUtils
).Impetus for Change
For traditional dependency injection use cases, there is no need to inject an individual parameter; however, JUnit 5 introduces a
ParameterResolver
extension API (analogous to Spring MVC'sHandlerMethodArgumentResolver
) that allows multiple, competing extension providers to inject values into constructors or test methods. In such a scenario, Spring is not the sole entity performing injection, and Spring therefore cannot simply perform injection for the entire constructor or method (as would be the case for an@Autowired
method). Rather, the Spring TestContext Framework (TCF) needs a mechanism for resolving dependencies for individual method parameters (preferably via thejava.lang.reflect.Parameter
API).Although the primary impetus is proper JUnit 5 support in the TCF, such a feature could be applied to MVC handler methods, JMS listener methods, etc.
Working Solution
The following excerpt from
org.springframework.test.context.junit.jupiter.ParameterAutowireUtils
demonstrates how such a feature can be implemented.Deliverables
Issue Links:
@Autowired
to be declared on parameters ("depends on")@Autowired
with@Qualifier
@Qualifier
to be used in composed annotations with attribute overrides@Autowired
(required=false) at parameter level, as an alternative to java.util.OptionalReferenced from: commits d7fe92d, 35eb52e
3 votes, 7 watchers
The text was updated successfully, but these errors were encountered: