@@ -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
0 commit comments