Deprecate use of several bean factory methods for the same bean
See gh-31073
This commit is contained in:
@@ -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");
|
||||
* 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
|
||||
* according to those semantics, accepting the risk for accidental overlaps.
|
||||
* @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;
|
||||
|
||||
}
|
||||
|
||||
@@ -292,6 +292,7 @@ class ConfigurationClassBeanDefinitionReader {
|
||||
this.registry.registerBeanDefinition(beanName, beanDefToRegister);
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullAway")
|
||||
protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) {
|
||||
if (!this.registry.containsBeanDefinition(beanName)) {
|
||||
return false;
|
||||
@@ -302,21 +303,23 @@ class ConfigurationClassBeanDefinitionReader {
|
||||
// If the bean method is an overloaded case on the same configuration class,
|
||||
// preserve the existing bean definition and mark it as overloaded.
|
||||
if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) {
|
||||
if (ccbd.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
|
||||
if (ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) {
|
||||
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
|
||||
}
|
||||
else if (!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;
|
||||
}
|
||||
else {
|
||||
if (!ccbd.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
|
||||
return false;
|
||||
}
|
||||
if (ccbd.getFactoryMethodMetadata().getMethodName().equals(beanMethod.getMetadata().getMethodName())) {
|
||||
ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
|
||||
return true;
|
||||
}
|
||||
Map<String, Object> attributes =
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user