Skip to content

javaconf: beans with same name in independent conf files should cause an error [SPR-8525] #13169

Closed
@spring-projects-issues

Description

@spring-projects-issues

Stepan Koltsov opened SPR-8525 and commented

public class QuietlyOverrides {

@Configuration
public static class Conf1 {

    @Bean
    public Inflater inflater() {
        System.out.println("1");
        return new Inflater();
    }

}

@Configuration
public static class Conf2 {

    @Bean
    public Inflater inflater() {
        System.out.println("2");
        return new Inflater();
    }

}

@Configuration
@Import({ Conf1.class, Conf2.class })
public static class Conf {
}

public static void main(String[] args) {
    // expecting an exception        // Spring quietly creates only second bean        new AnnotationConfigApplicationContext(Conf.class);
    System.out.println("$");
}

} //~

We have large application, and one developer accidentally created a bean with name same as name of bean in another part of application. Problem search was not easy.

I think Spring should throw an error if independent configurations declare beans with same name. Configurations are independent if neither of which include another configuration directly or indirectly.

However, IF Conf2 includes Conf1, Spring should quietly override bean declared in Conf1 with bean declared if Conf2, as it does now. It is convenient to "inherit" configuration and override some bean definitions.


Affects: 3.1 M2

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions