Method-based autowiring fails in certain contexts [SPR-9685] #14319
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: declined
A suggestion or change that we don't feel we should currently apply
Steve van Loben Sels opened SPR-9685 and commented
If an application context contains more than one instance of a bean whose class has an
@Autowired
setter method, then it's possible that some of the beans' dependencies do not get injected.The attached TestNG unit test demonstrates the bug. It creates two singletons of type AutowiredMethodBean...one from XML and the other from a
@Configuration
. When the bean defined in the XML is initialized first, then the JavaConfig's bean will not be autowired and the test will fail. The order in which the beans are instantiated is crucial...the test passes if the JavaConfig bean is created first.This problem looks to be due to the cached metadata in AutowiredAnnotationBeanPostProcessor. If the metadata is lazily created when initializing the XML bean with its explicit property declaration, then the field is marked as skipped and injection won't be attempted on the JavaConfig bean.
This unit test uses a mixed XML/JavaConfig application context because that's the configuration of the application I was working on when I discovered the issue. I suspect that it's possible to manifest with XML-only configuration if the first bean has the property explicitly set and the second uses autowiring.
Affects: 3.1.2
Attachments:
The text was updated successfully, but these errors were encountered: