18
18
19
19
import java .lang .reflect .Constructor ;
20
20
import java .lang .reflect .Method ;
21
- import java .util .ArrayList ;
22
- import java .util .Arrays ;
23
21
import java .util .HashMap ;
24
- import java .util .List ;
25
22
import java .util .Map ;
26
23
import java .util .Set ;
27
24
@@ -199,16 +196,30 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
199
196
Assert .state (bean != null , "No @Bean annotation attributes" );
200
197
201
198
// Consider name and any aliases
202
- List <String > names = new ArrayList <>(Arrays .asList (bean .getStringArray ("name" )));
203
- String beanName = (!names .isEmpty () ? names .remove (0 ) : methodName );
204
-
205
- // Register aliases even when overridden
206
- for (String alias : names ) {
207
- this .registry .registerAlias (beanName , alias );
199
+ String [] explicitNames = bean .getStringArray ("name" );
200
+ String beanName ;
201
+ String localBeanName ;
202
+ if (explicitNames .length > 0 && StringUtils .hasText (explicitNames [0 ])) {
203
+ beanName = explicitNames [0 ];
204
+ localBeanName = beanName ;
205
+ // Register aliases even when overridden below
206
+ for (int i = 1 ; i < explicitNames .length ; i ++) {
207
+ this .registry .registerAlias (beanName , explicitNames [i ]);
208
+ }
209
+ }
210
+ else {
211
+ // Default bean name derived from method name.
212
+ beanName = (this .importBeanNameGenerator instanceof ConfigurationBeanNameGenerator cbng ?
213
+ cbng .deriveBeanName (metadata ) : methodName );
214
+ localBeanName = methodName ;
208
215
}
209
216
217
+ ConfigurationClassBeanDefinition beanDef =
218
+ new ConfigurationClassBeanDefinition (configClass , metadata , localBeanName );
219
+ beanDef .setSource (this .sourceExtractor .extractSource (metadata , configClass .getResource ()));
220
+
210
221
// Has this effectively been overridden before (for example, via XML)?
211
- if (isOverriddenByExistingDefinition (beanMethod , beanName )) {
222
+ if (isOverriddenByExistingDefinition (beanMethod , beanName , beanDef )) {
212
223
if (beanName .equals (beanMethod .getConfigurationClass ().getBeanName ())) {
213
224
throw new BeanDefinitionStoreException (beanMethod .getConfigurationClass ().getResource ().getDescription (),
214
225
beanName , "Bean name derived from @Bean method '" + beanMethod .getMetadata ().getMethodName () +
@@ -217,9 +228,6 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
217
228
return ;
218
229
}
219
230
220
- ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition (configClass , metadata , beanName );
221
- beanDef .setSource (this .sourceExtractor .extractSource (metadata , configClass .getResource ()));
222
-
223
231
if (metadata .isStatic ()) {
224
232
// static @Bean method
225
233
if (configClass .getMetadata () instanceof StandardAnnotationMetadata sam ) {
@@ -288,7 +296,7 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
288
296
new BeanDefinitionHolder (beanDef , beanName ), this .registry ,
289
297
proxyMode == ScopedProxyMode .TARGET_CLASS );
290
298
beanDefToRegister = new ConfigurationClassBeanDefinition (
291
- (RootBeanDefinition ) proxyDef .getBeanDefinition (), configClass , metadata , beanName );
299
+ (RootBeanDefinition ) proxyDef .getBeanDefinition (), configClass , metadata , localBeanName );
292
300
}
293
301
294
302
if (logger .isTraceEnabled ()) {
@@ -299,7 +307,9 @@ private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
299
307
}
300
308
301
309
@ SuppressWarnings ("NullAway" ) // Reflection
302
- protected boolean isOverriddenByExistingDefinition (BeanMethod beanMethod , String beanName ) {
310
+ private boolean isOverriddenByExistingDefinition (
311
+ BeanMethod beanMethod , String beanName , ConfigurationClassBeanDefinition newBeanDef ) {
312
+
303
313
if (!this .registry .containsBeanDefinition (beanName )) {
304
314
return false ;
305
315
}
@@ -320,9 +330,7 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
320
330
configClass .getMetadata ().getAnnotationAttributes (Configuration .class .getName ());
321
331
if ((attributes != null && (Boolean ) attributes .get ("enforceUniqueMethods" )) ||
322
332
!this .registry .isBeanDefinitionOverridable (beanName )) {
323
- throw new BeanDefinitionOverrideException (beanName ,
324
- new ConfigurationClassBeanDefinition (configClass , beanMethod .getMetadata (), beanName ),
325
- existingBeanDef ,
333
+ throw new BeanDefinitionOverrideException (beanName , newBeanDef , existingBeanDef ,
326
334
"@Bean method override with same bean name but different method name: " + existingBeanDef );
327
335
}
328
336
return true ;
@@ -400,17 +408,20 @@ private void loadBeanDefinitionsFromImportedResources(
400
408
});
401
409
}
402
410
403
- private void loadBeanDefinitionsFromImportBeanDefinitionRegistrars (Map <ImportBeanDefinitionRegistrar , AnnotationMetadata > registrars ) {
411
+ private void loadBeanDefinitionsFromImportBeanDefinitionRegistrars (
412
+ Map <ImportBeanDefinitionRegistrar , AnnotationMetadata > registrars ) {
413
+
404
414
registrars .forEach ((registrar , metadata ) ->
405
415
registrar .registerBeanDefinitions (metadata , this .registry , this .importBeanNameGenerator ));
406
416
}
407
417
408
418
private void loadBeanDefinitionsFromBeanRegistrars (Map <String , BeanRegistrar > registrars ) {
409
- Assert .isInstanceOf (ListableBeanFactory .class , this .registry ,
410
- "Cannot support bean registrars since " + this .registry .getClass ().getName () +
411
- " does not implement BeanDefinitionRegistry" );
412
- registrars .values ().forEach (registrar -> registrar .register (new BeanRegistryAdapter (this .registry ,
413
- (ListableBeanFactory ) this .registry , this .environment , registrar .getClass ()), this .environment ));
419
+ if (!(this .registry instanceof ListableBeanFactory beanFactory )) {
420
+ throw new IllegalStateException ("Cannot support bean registrars since " +
421
+ this .registry .getClass ().getName () + " does not implement ListableBeanFactory" );
422
+ }
423
+ registrars .values ().forEach (registrar -> registrar .register (new BeanRegistryAdapter (
424
+ this .registry , beanFactory , this .environment , registrar .getClass ()), this .environment ));
414
425
}
415
426
416
427
@@ -427,32 +438,32 @@ private static class ConfigurationClassBeanDefinition extends RootBeanDefinition
427
438
428
439
private final MethodMetadata factoryMethodMetadata ;
429
440
430
- private final String derivedBeanName ;
441
+ private final String localBeanName ;
431
442
432
443
public ConfigurationClassBeanDefinition (
433
- ConfigurationClass configClass , MethodMetadata beanMethodMetadata , String derivedBeanName ) {
444
+ ConfigurationClass configClass , MethodMetadata beanMethodMetadata , String localBeanName ) {
434
445
435
446
this .annotationMetadata = configClass .getMetadata ();
436
447
this .factoryMethodMetadata = beanMethodMetadata ;
437
- this .derivedBeanName = derivedBeanName ;
448
+ this .localBeanName = localBeanName ;
438
449
setResource (configClass .getResource ());
439
450
setLenientConstructorResolution (false );
440
451
}
441
452
442
453
public ConfigurationClassBeanDefinition (RootBeanDefinition original ,
443
- ConfigurationClass configClass , MethodMetadata beanMethodMetadata , String derivedBeanName ) {
454
+ ConfigurationClass configClass , MethodMetadata beanMethodMetadata , String localBeanName ) {
444
455
445
456
super (original );
446
457
this .annotationMetadata = configClass .getMetadata ();
447
458
this .factoryMethodMetadata = beanMethodMetadata ;
448
- this .derivedBeanName = derivedBeanName ;
459
+ this .localBeanName = localBeanName ;
449
460
}
450
461
451
462
private ConfigurationClassBeanDefinition (ConfigurationClassBeanDefinition original ) {
452
463
super (original );
453
464
this .annotationMetadata = original .annotationMetadata ;
454
465
this .factoryMethodMetadata = original .factoryMethodMetadata ;
455
- this .derivedBeanName = original .derivedBeanName ;
466
+ this .localBeanName = original .localBeanName ;
456
467
}
457
468
458
469
@ Override
@@ -468,7 +479,7 @@ public MethodMetadata getFactoryMethodMetadata() {
468
479
@ Override
469
480
public boolean isFactoryMethod (Method candidate ) {
470
481
return (super .isFactoryMethod (candidate ) && BeanAnnotationHelper .isBeanAnnotated (candidate ) &&
471
- BeanAnnotationHelper .determineBeanNameFor (candidate ).equals (this .derivedBeanName ));
482
+ BeanAnnotationHelper .determineBeanNameFor (candidate ).equals (this .localBeanName ));
472
483
}
473
484
474
485
@ Override
0 commit comments