Skip to content

Commit 43ff6d9

Browse files
committed
Deprecate use of several bean factory methods for the same bean
See gh-31073
1 parent 4773ffc commit 43ff6d9

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

spring-context/src/main/java/org/springframework/context/annotation/Configuration.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -471,7 +471,10 @@
471471
* Switch this flag to {@code false} in order to allow for method overloading
472472
* according to those semantics, accepting the risk for accidental overlaps.
473473
* @since 6.0
474+
* @deprecated as of 7.0, always relying on {@code @Bean} unique methods,
475+
* just possibly with {@code Optional}/{@code ObjectProvider} arguments
474476
*/
477+
@Deprecated(since = "7.0")
475478
boolean enforceUniqueMethods() default true;
476479

477480
}

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
292292
this.registry.registerBeanDefinition(beanName, beanDefToRegister);
293293
}
294294

295+
@SuppressWarnings("NullAway")
295296
protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) {
296297
if (!this.registry.containsBeanDefinition(beanName)) {
297298
return false;
@@ -302,21 +303,23 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
302303
// If the bean method is an overloaded case on the same configuration class,
303304
// preserve the existing bean definition and mark it as overloaded.
304305
if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) {
305-
if (ccbd.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
306-
if (ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) {
307-
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
308-
}
309-
else if (!this.registry.isBeanDefinitionOverridable(beanName)) {
310-
throw new BeanDefinitionOverrideException(beanName,
311-
new ConfigurationClassBeanDefinition(configClass, beanMethod.getMetadata(), beanName),
312-
existingBeanDef,
313-
"@Bean method override with same bean name but different method name: " + existingBeanDef);
314-
}
306+
if (!ccbd.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
307+
return false;
308+
}
309+
if (ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) {
310+
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
315311
return true;
316312
}
317-
else {
318-
return false;
313+
Map<String, Object> attributes =
314+
configClass.getMetadata().getAnnotationAttributes(Configuration.class.getName());
315+
if ((attributes != null && (Boolean) attributes.get("enforceUniqueMethods")) ||
316+
!this.registry.isBeanDefinitionOverridable(beanName)) {
317+
throw new BeanDefinitionOverrideException(beanName,
318+
new ConfigurationClassBeanDefinition(configClass, beanMethod.getMetadata(), beanName),
319+
existingBeanDef,
320+
"@Bean method override with same bean name but different method name: " + existingBeanDef);
319321
}
322+
return true;
320323
}
321324

322325
// A bean definition resulting from a component scan can be silently overridden

spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ public TestBean bar() {
542542
}
543543

544544

545-
@Configuration
545+
@Configuration(enforceUniqueMethods = false)
546546
static class ConfigWithMethodNameMismatch {
547547

548548
@Bean(name = "foo") public TestBean foo1() {

0 commit comments

Comments
 (0)