Skip to content
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

Regression in duplicate beans with different method names #33920

Closed
iparadiso opened this issue Nov 19, 2024 · 2 comments
Closed

Regression in duplicate beans with different method names #33920

iparadiso opened this issue Nov 19, 2024 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Milestone

Comments

@iparadiso
Copy link

iparadiso commented Nov 19, 2024

Observed Issue

In Spring Boot 3.3, duplicate named beans can be defined where @Primary can be used as the default. In Spring Boot 3.4 RC1, this behavior is no longer the same.

Consider the following example of two named beans with different behavior.

@TestConfiguration
public class TestConfig {

    @Bean(name = "foo")
    @Primary
    @ConditionalOnProperty(name = "foo.truthy", havingValue = "true", matchIfMissing = true)
    Boolean fooIsTrue() {
        return true;
    }

    @Bean(name = "foo", defaultCandidate = false)
    Boolean fooIsFalse() {
        return false;
    }
}

When validated in the following test, SB 3.3 expectedly injects the "truthful" bean version marked as @Primary.

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { TestConfig.class })
public class ConditionalTest {
    @Autowired
    @Qualifier("foo")
    Boolean foo;

    @Test
    void checkFoo() {
        assertTrue(foo);
    }
}

When this same test is applied in Spring Boot 3.4 RC1, the @Primary "foo" bean is no longer created, causing this test evaluation to fail.

Is this an exepected change in behavior?

Reproducer

This test is available here

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 19, 2024
@bclozel bclozel transferred this issue from spring-projects/spring-boot Nov 19, 2024
@bclozel bclozel added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 19, 2024
@philwebb
Copy link
Member

philwebb commented Nov 19, 2024

I think 5529366 is the commit that changed things. Issue #33330

Add a breakpoint in ConfigurationClassBeanDefinitionReader.isOverriddenByExistingDefinition. In the previous version we return true here on the second call. In the newer version, we return false.

It's interesting that we don't get a hard failure.

@jhoeller jhoeller added type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 20, 2024
@jhoeller jhoeller self-assigned this Nov 20, 2024
@jhoeller jhoeller added this to the 6.2.1 milestone Nov 20, 2024
@sbrannen sbrannen changed the title Regression in duplicate beans with @Primary in Spring Boot 3.4 RC Regression in duplicate beans with @Primary in 6.2.0 Nov 20, 2024
@jhoeller
Copy link
Contributor

I've revised the ConfigurationClassBeanDefinitionReader.isOverriddenByExistingDefinition algorithm to explicitly raise a BeanDefinitionOverrideException in case of an unexpected override when allowBeanDefinitionOverriding=false, leading to a consistent failure - while preserving the original override behavior in case of allowBeanDefinitionOverriding=true. This should keep #33300 happy while also addressing this regression here, restoring compatibility with Framework 6.1.x. The reproducer seems to pass so far.

Note that bean definition overriding needs to be explicitly allowed for such an arrangement to be accepted on 6.2. This may have to be activated explicitly for production setups since we generally prefer to keep it disallowed, and the definition of what constitutes an override is somewhat broader in 6.2 now.

@jhoeller jhoeller changed the title Regression in duplicate beans with @Primary in 6.2.0 Regression in duplicate beans with different method names Dec 10, 2024
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) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

5 participants