-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Directly registered FactoryBeans are instantiated more aggressively than those defined via @Bean methods #22409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As an additional question to this issue I want to ask about So, as constructor args are added manually to the definition - can such approach ruin type checking when we should invoke constructor without any dependencies injection? I mean full instantiation and bean population will be launched instead. Just want to clarify typechecking process and it's limitations. |
While it is uncommon - and to some degree unintended - that FactoryBean implementations have non-default constructors, we should nevertheless not make hard assumptions about it. A defensive catch block like we have for the non-singleton FactoryBean case already (since SPR-12786 / #17383 in 4.1.6) seems to do the job here, so I'll roll that into 5.1.6 (possibly backporting it to 5.0.13 and 4.3.23 as well). |
Also, it's not recommended for a Nevertheless, the underlying inconsistency in our core container treatment needs to get resolved here. We just need to make sure that we're not swallowing error cases that are helpful in FactoryBean scenarios that are intended to match but fail due to incomplete configuration or the like. |
Side note: Component scanning is not really relevant here. The same effect appears when directly registering the FactoryBean implementation class a la |
I'll leave this as 5.1.6 only for the time being since our catch blocks consistently let |
…zers Until Spring Framework 5.1.15, a FactoryBean with a non-default constructor defined via component scanning would cause an error. This behavior has changed as of spring-projects/spring-framework#22409. Regardless of this change we want to ensure that we avoid triggering eager initialisation. `SimpleFactoryBean` has been written this way so that the tests fail if early initialization is triggered regardless of the Spring Framework version. Fixes gh-15898
Affects: 5.1.4.RELEASE
This relates to spring-projects/spring-boot#15898.
When a
FactoryBean
is defined via component scanning, it is instantiated more aggressively than when it is defined via a@Bean
method. The following tests should illustrate the differing behaviour:beanMethodFactoryBean
will successfully refresh the application context.componentScannedFactoryBean
fails as an attempt is made to createSimpleFactoryBean
using a non-existent default constructor:In the
@Bean
method case, the attempt to determine the type produced by theFactoryBean
is abandoned earlier. As far as I can tell that's due to the following logic:spring-framework/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
Lines 855 to 860 in 77e8151
The text was updated successfully, but these errors were encountered: