@@ -292,6 +292,7 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
292
292
this .registry .registerBeanDefinition (beanName , beanDefToRegister );
293
293
}
294
294
295
+ @ SuppressWarnings ("NullAway" )
295
296
protected boolean isOverriddenByExistingDefinition (BeanMethod beanMethod , String beanName ) {
296
297
if (!this .registry .containsBeanDefinition (beanName )) {
297
298
return false ;
@@ -302,21 +303,23 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
302
303
// If the bean method is an overloaded case on the same configuration class,
303
304
// preserve the existing bean definition and mark it as overloaded.
304
305
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 ());
315
311
return true ;
316
312
}
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 );
319
321
}
322
+ return true ;
320
323
}
321
324
322
325
// A bean definition resulting from a component scan can be silently overridden
0 commit comments