Skip to content

BeanDefinitionOverrideException not thrown as expected #28970

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

Closed
ipavkovic opened this issue Aug 16, 2022 · 1 comment
Closed

BeanDefinitionOverrideException not thrown as expected #28970

ipavkovic opened this issue Aug 16, 2022 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@ipavkovic
Copy link

ipavkovic commented Aug 16, 2022

environment: spring boot 2.6.10

define a demo app with following configuration class:

@Configuration
public class DemoConfig {

    @Bean("aaa")
    public String foo() {
        return "foo";
    }

    @Bean("aaa")
    public String bar() {
        return "bar";
    }
}

should be: BeanDefinitionOverrideException
is: bean named "aaa" added to bean context (with value "foo"), method bar() never added to bean definition

debugging shows a return statement here, that seems to the cuprit.

if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) {
throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(),
beanName, "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName() +
"' clashes with bean name for containing configuration class; please make those names unique!");
}
return;
}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 16, 2022
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 24, 2023
@snicoll
Copy link
Member

snicoll commented Oct 2, 2023

Thanks for the report and sorry it took so long.

Your example shows that the methods have the bean same name in the same configuration class so the container considers them to be two factory methods for the same bean where the container should choose which one to pick based on the arguments.

As you've noticed, this is really confusing and we've decided to deprecate this behavior, see #31073

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants