Skip to content

Different behavior when injecting beans from local @Configuration class vs. external @Configuration classes #23934

@aposcia

Description

@aposcia

Version: 5.2.1.RELEASE

Given the following configuration:

@Configuration
public class BaseTestConfig {

    @Autowired
    private Collection<String> strings;

    @Bean
    public String stringA() {
        return "A";
    }

    @Bean
    public String stringB() {
        return "B";
    }


    @Bean
    public DemoClass demoBean() {
        System.out.println("Base config autowired strings: " + strings.size());
        return new DemoClass(strings);
    }

    class DemoClass {
        private final Collection<String> strings;

        public DemoClass(Collection<String> strings) {
            this.strings = strings;
        }

        public Collection<String> getStrings() {
            return strings;
        }
    }

}

When demoBean is instantiated, it prints:

Base config autowired strings: 2

If we add another config file with content:

@Configuration
public class Extension1TestConfig {

    @Bean
    public String stringC() {
        return "C";
    }

}

when demoBean is instantiated, it prints:

Base config autowired strings: 1

We expect to have the 3 string beans injected into demoBean (2 beans of BaseTestConfig and 1 bean of Extension1TestConfig). However, as soon as another configuration file is declaring some String beans, the beans declared in BaseTestConfig are not injected anymore in the collection (strings) and only the beans of the other context(s) are injected.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: documentationA documentation task

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions