Browse Source

Deprecate use of several bean factory methods for the same bean

See gh-31073
pull/34110/head
Juergen Hoeller 1 year ago
parent
commit
43ff6d9711
  1. 5
      spring-context/src/main/java/org/springframework/context/annotation/Configuration.java
  2. 27
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java
  3. 2
      spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -471,7 +471,10 @@ public @interface Configuration {
* Switch this flag to {@code false} in order to allow for method overloading * Switch this flag to {@code false} in order to allow for method overloading
* according to those semantics, accepting the risk for accidental overlaps. * according to those semantics, accepting the risk for accidental overlaps.
* @since 6.0 * @since 6.0
* @deprecated as of 7.0, always relying on {@code @Bean} unique methods,
* just possibly with {@code Optional}/{@code ObjectProvider} arguments
*/ */
@Deprecated(since = "7.0")
boolean enforceUniqueMethods() default true; boolean enforceUniqueMethods() default true;
} }

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

@ -292,6 +292,7 @@ class ConfigurationClassBeanDefinitionReader {
this.registry.registerBeanDefinition(beanName, beanDefToRegister); this.registry.registerBeanDefinition(beanName, beanDefToRegister);
} }
@SuppressWarnings("NullAway")
protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) { protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) {
if (!this.registry.containsBeanDefinition(beanName)) { if (!this.registry.containsBeanDefinition(beanName)) {
return false; return false;
@ -302,21 +303,23 @@ class ConfigurationClassBeanDefinitionReader {
// If the bean method is an overloaded case on the same configuration class, // If the bean method is an overloaded case on the same configuration class,
// preserve the existing bean definition and mark it as overloaded. // preserve the existing bean definition and mark it as overloaded.
if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) { if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) {
if (ccbd.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) { if (!ccbd.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
if (ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) { return false;
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName()); }
} if (ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) {
else if (!this.registry.isBeanDefinitionOverridable(beanName)) { ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
throw new BeanDefinitionOverrideException(beanName,
new ConfigurationClassBeanDefinition(configClass, beanMethod.getMetadata(), beanName),
existingBeanDef,
"@Bean method override with same bean name but different method name: " + existingBeanDef);
}
return true; return true;
} }
else { Map<String, Object> attributes =
return false; configClass.getMetadata().getAnnotationAttributes(Configuration.class.getName());
if ((attributes != null && (Boolean) attributes.get("enforceUniqueMethods")) ||
!this.registry.isBeanDefinitionOverridable(beanName)) {
throw new BeanDefinitionOverrideException(beanName,
new ConfigurationClassBeanDefinition(configClass, beanMethod.getMetadata(), beanName),
existingBeanDef,
"@Bean method override with same bean name but different method name: " + existingBeanDef);
} }
return true;
} }
// A bean definition resulting from a component scan can be silently overridden // A bean definition resulting from a component scan can be silently overridden

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

@ -542,7 +542,7 @@ class ConfigurationClassProcessingTests {
} }
@Configuration @Configuration(enforceUniqueMethods = false)
static class ConfigWithMethodNameMismatch { static class ConfigWithMethodNameMismatch {
@Bean(name = "foo") public TestBean foo1() { @Bean(name = "foo") public TestBean foo1() {

Loading…
Cancel
Save