Skip to content

fix: Report duplicate @Bean names on Spring 7 configuration classes even when overriding is allowed (#686) - #692

Merged
imuromtsev merged 4 commits into
mainfrom
imuromtsev/spring7-bean-override-rule
Oct 14, 2026
Merged

imuromtsev merged 4 commits into
mainfrom
imuromtsev/spring7-bean-override-rule

Conversation

@imuromtsev

Copy link
Copy Markdown
Collaborator

Purpose

The duplicate @Bean name inspection (#597) stayed silent when spring.main.allow-bean-definition-overriding=true. On Spring Framework 7 that is wrong for a full @Configuration class: ConfigurationClassBeanDefinitionReader.isOverriddenByExistingDefinition (spring-context 7.0.9 :337-342) throws BeanDefinitionOverrideException when the class's @Configuration has enforceUniqueMethods = true (the default) or overriding is disallowed, so the property does not rescue it.

Change

  • When the class enforces unique methods and the Spring Framework major is 7 or later, the problem is an error whatever spring.main.allow-bean-definition-overriding says (missing, false, true or unresolvable), with a message that names the Spring 7 rule.
  • enforceUniqueMethods is read with merged values, as AnnotatedTypeMetadata.getAnnotationAttributes(Configuration) does: a directly declared @Configuration first, else the first annotation composed with it; a meta-fixed @Configuration(enforceUniqueMethods = false) and an @AliasFor default or use-site value count, in Java and Kotlin.
  • A lite class (no @Configuration) and @Configuration(enforceUniqueMethods = false) keep the property rule. When the Spring Framework version cannot be determined and the property is true, the problem is a warning.
  • Inspection: two @Bean methods with the same bean name in one configuration class fail Spring Boot startup #597 is not released yet, so its [Unreleased] line now states the Spring 7 rule and ends (#597, #686); the inspection description says the same.

Framework evidence

Probe on real jars, one @Configuration class with @Bean A a() + @Bean("a") A b() (and the same across a class hierarchy):

Class 6.2.19, overriding allowed 7.0.9, overriding allowed
@Configuration starts fails
lite (@Component) starts starts
@Configuration(enforceUniqueMethods = false) starts starts
composed, meta @Configuration(enforceUniqueMethods = false) starts starts
composed, @AliasFor to enforceUniqueMethods, default false starts starts
composed, same alias, use site unique = true (Java and Kotlin) starts fails

With overriding disallowed every case fails on both versions.

Not in this PR

Verification

  • Red commits 900388a (Spring 7 + allowed overriding, unknown version) and bfd59bb (review: every property value on Spring 7, composed enforceUniqueMethods), each red failing on the problem or message assertion; Java and Kotlin twins; Spring 7 via the Boot 4.1 test library, unknown version via a spring-core jar without coordinates.
  • After the fixes (ee651bc, d1af96b): 54 tests in the SpringDuplicateBeanName* classes, MetaAnnotationsHolderMergedValues* 45/45.
  • All modules (:spring-bootstrap:test --rerun-tasks on d1af96b, fresh JUnit XML): spring-core 2413, spring-web 905 (6 skipped), spring-ai 776, jpa 138, spring-data 140, base 35, aop 6, security 6, initializr 5; 0 failures, 0 errors.

Main only: the inspection (#669) and MergedAnnotationAttributes (#673) are not on the release branches.

Closes #686

@imuromtsev imuromtsev added the in:spring-core Spring Core or Spring Boot Core issues label Oct 13, 2026
@github-actions

Copy link
Copy Markdown
TestsPassed ✅Skipped ⚠️Failed
JUnit Test Report4424 ran4418 passed6 skipped0 failed

@imuromtsev
imuromtsev added this pull request to stack #699 October 14, 2026 06:45
@imuromtsev
imuromtsev merged commit 4d439d4 into main Oct 14, 2026
2 checks passed
@imuromtsev
imuromtsev deleted the imuromtsev/spring7-bean-override-rule branch October 14, 2026 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:spring-core Spring Core or Spring Boot Core issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate @Bean name inspection is silent on Spring 7 when bean definition overriding is allowed

1 participant