Skip to content

Doc: Consistent @Profile declarations on overloaded @Bean methods [SPR-15266] #19831

Closed
@spring-projects-issues

Description

@spring-projects-issues

Georg Herdt opened SPR-15266 and commented

When having bean factory methods with the same name but different arguments used for creating instance for different profiles the bean is not recognized for some profiles.
See example below:

@Configuration
public class SomeConfiguration {

    @Bean(name = "something")
    @Profile("A")
    public String valueForTest() {
        return "aaa";
    }

    @Bean
    public String secondValue() {
        return "bbb";
    }

    @Bean(name = "something")
    @Profile("B")
    @Autowired
    public String valueForTest(final String secondValue) {
        return secondValue;
    }
}

Corresponding test will fail:

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("A")
@ContextConfiguration(classes = { SomeConfiguration.class })
public class SomeConfigurationTest_ProfileA {

    @Autowired
    @Qualifier("something")
    private String valueForTest;

    @Autowired
    private String secondValue;

    @Test
    public void test() {
        Assert.assertEquals("aaa", valueForTest);
    }

}

When using profile "B" dependencies for bean named "something" will be created successfully. Providing string value "bbb".
For profile "A" it is not working properly.
Behaviour depends on used Spring version. For releases 4.0.0. up to 4.1.4 outcome will be "bbb".
I would expect "aaa".

From release 4.1.5 on (including 5.x.x releases) a NoSuchBeanDefinitionException is thrown.

See the attached gradle project to evaluate the behaviour.


Affects: 4.3.6, 5.0 M4

Attachments:

Issue Links:

Referenced from: commits 5d3249f, 6c370ed, 022aefd

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions