Overloaded @Bean method with name mismatch causes bean to be created twice (in case of ASM processing)
See original GitHub issueReproduced in Spring 5.2.7.RELEASE (Spring Boot 2.3.1.RELEASE):
@Configuration
public class SomeConfig {
@Bean(name = "other")
SomeOtherBean foo() {
System.out.println("constructing SomeOtherBean");
return new SomeOtherBean();
}
@Bean(name = "foo")
SomeBean foo(@Qualifier("other") SomeOtherBean other) {
System.out.println("constructing SomeBean");
return new SomeBean(other);
}
}
With the above configuration class, SomeOtherBean
is constructed twice, and SomeBean
is never constructed. Why? Because the two methods have the same name. If you rename the second method to bar
, everything is constructed once, as expected.
Admittedly, using the same method name is not a good idea. I just happened to have done it by accident.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
The BeanDefinitionOverrideException in Spring Boot - Baeldung
Bean Overriding Spring beans are identified by their names within an ApplicationContext. Therefore, bean overriding is a default behavior that ...
Read more >Spring bean getting initialised twice in java configuration
Beans will be configured and created twice because both application context scans the same package "com.mypackage".
Read more >Jersey 2.37 User Guide - GitHub Pages
This is user guide for Jersey 2.37. We are trying to keep it up to date as we add new features. When reading...
Read more >Learning the Basics - Gradle User Manual
Gradle represents the scope of a dependency with the help of a Configuration. Every configuration can be identified by a unique name. Many...
Read more >2.4. Bean Integration - Red Hat JBoss Fuse
If a bean defines overloaded methods, you can choose which of the overloaded methods to invoke by specifying the method name along with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lifejwang11 not sure exactly what you mean by “the relationship between the two”. If you’re talking about the two beans
SomeBean
andSomeOtherBean
, they’re irrelevant. Spring should construct an instance of each of them by calling each of the@Bean
methods once, but does not, and instead calls the first one twice, and never calls the second one.But anyway, here’s a complete demo if you want to try it by yourself: https://github.com/jnizet/spring-overload-issue
I’ve tracked this down to a difference between Class-based processing of the configuration class versus ASM-based processing against a registered configuration class name. A variant of the unit test passes with Class registration (similar to the
@SpringJUnitConfig
scenario above) but fails with name-based registration (similar to component scanning).