question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Overloaded @Bean method with name mismatch causes bean to be created twice (in case of ASM processing)

See original GitHub issue

Reproduced 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:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jnizetcommented, Jun 17, 2020

@lifejwang11 not sure exactly what you mean by “the relationship between the two”. If you’re talking about the two beans SomeBean and SomeOtherBean, 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

0reactions
jhoellercommented, Aug 27, 2020

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found