Skip to content

Commit

Permalink
Deprecate use of several bean factory methods for the same bean
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Dec 12, 2024
1 parent 4773ffc commit 43ff6d9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -471,7 +471,10 @@
* 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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}

@SuppressWarnings("NullAway")
protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) {
if (!this.registry.containsBeanDefinition(beanName)) {
return false;
Expand All @@ -302,21 +303,23 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
// 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);
}
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;
}
else {
return false;
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public TestBean bar() {
}


@Configuration
@Configuration(enforceUniqueMethods = false)
static class ConfigWithMethodNameMismatch {

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

0 comments on commit 43ff6d9

Please sign in to comment.