Skip to content

Regression in @ConditionalOnMissingBean behavior between Spring Boot 3.4.6 and 3.5.0 when using duplicate bean names #45709

Closed as not planned
@oburgosm

Description

@oburgosm

Summary

After upgrading from Spring Boot 3.4.6 to 3.5.0, a configuration class that previously worked now fails with a BeanDefinitionOverrideException due to a duplicate bean definition. The issue appears to be related to how @ConditionalOnMissingBean is evaluated when multiple @Bean methods with the same name exist in the same configuration class.

Sample Configuration

@Configuration(proxyBeanMethods = false)
@EnableMongoAuditing(auditorAwareRef = "mongoAuditorAware")
public class AuditConfiguration {

  @Bean(name = "mongoAuditorAware")
  @ConditionalOnClass(name = "org.springframework.security.core.context.SecurityContextHolder")
  public AuditorAware<?> mongoAuditorAware(...) {
    ...
  }

  @Bean(name = "mongoAuditorAware")
  @ConditionalOnMissingBean
  public AuditorAware<String> mongoWithoutSpringSecurityAuditorAware(...) {
    ...
  }
}

Behavior

✅ In 3.4.6: Only one bean is registered depending on the presence of the class and the condition.
❌ In 3.5.0: Both methods are evaluated, and a mongoAuditorAware bean is defined twice, causing a failure.

Expected Behavior

@ConditionalOnMissingBean should prevent the second bean from being registered if one with the same name already exists or is about to be registered.

Workaround

Changing the annotation to:

@ConditionalOnMissingBean(name = "mongoAuditorAware")

resolves the issue, but this was not required in previous versions.

Analysis

This change in behavior is not documented in the 3.5.0 release notes.
There is no related issue or pull request indicating this was an intentional change.
It may be a regression or an unintended side effect of internal changes to condition evaluation.

Request

Please clarify whether this is an intentional change in Spring Boot 3.5.0. If so, it would be helpful to document it as a breaking change. Otherwise, it may be worth investigating as a regression.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions