24
24
import java .util .function .Function ;
25
25
import java .util .stream .Stream ;
26
26
27
+ import org .springframework .beans .BeanUtils ;
27
28
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
28
29
import org .springframework .beans .factory .support .BeanNameGenerator ;
29
30
import org .springframework .context .annotation .AnnotationBeanNameGenerator ;
@@ -64,6 +65,7 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
64
65
private static final String REPOSITORY_BASE_CLASS = "repositoryBaseClass" ;
65
66
private static final String CONSIDER_NESTED_REPOSITORIES = "considerNestedRepositories" ;
66
67
private static final String BOOTSTRAP_MODE = "bootstrapMode" ;
68
+ private static final String BEAN_NAME_GENERATOR = "nameGenerator" ;
67
69
68
70
private final AnnotationMetadata configMetadata ;
69
71
private final AnnotationMetadata enableAnnotationMetadata ;
@@ -97,14 +99,15 @@ public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Clas
97
99
* @param resourceLoader must not be {@literal null}.
98
100
* @param environment must not be {@literal null}.
99
101
* @param registry must not be {@literal null}.
100
- * @param generator can be {@literal null}.
102
+ * @param importBeanNameGenerator can be {@literal null}.
101
103
*/
102
104
public AnnotationRepositoryConfigurationSource (AnnotationMetadata metadata , Class <? extends Annotation > annotation ,
103
105
ResourceLoader resourceLoader , Environment environment , BeanDefinitionRegistry registry ,
104
- @ Nullable BeanNameGenerator generator ) {
106
+ @ Nullable BeanNameGenerator importBeanNameGenerator ) {
105
107
106
108
super (environment , ConfigurationUtils .getRequiredClassLoader (resourceLoader ), registry ,
107
- defaultBeanNameGenerator (generator ));
109
+ configuredOrDefaultBeanNameGenerator (metadata , annotation ,
110
+ ConfigurationUtils .getRequiredClassLoader (resourceLoader ), importBeanNameGenerator ));
108
111
109
112
Assert .notNull (metadata , "Metadata must not be null" );
110
113
Assert .notNull (annotation , "Annotation must not be null" );
@@ -305,6 +308,24 @@ private static boolean hasExplicitFilters(AnnotationAttributes attributes) {
305
308
.anyMatch (it -> attributes .getAnnotationArray (it ).length > 0 );
306
309
}
307
310
311
+ private static BeanNameGenerator configuredOrDefaultBeanNameGenerator (AnnotationMetadata metadata ,
312
+ Class <? extends Annotation > annotation , ClassLoader beanClassLoader ,
313
+ @ Nullable BeanNameGenerator importBeanNameGenerator ) {
314
+
315
+ Map <String , Object > annotationAttributes = metadata .getAnnotationAttributes (annotation .getName ());
316
+
317
+ if (annotationAttributes != null ) {
318
+
319
+ BeanNameGenerator beanNameGenerator = getBeanNameGenerator (annotationAttributes , beanClassLoader );
320
+
321
+ if (beanNameGenerator != null ) {
322
+ return beanNameGenerator ;
323
+ }
324
+ }
325
+
326
+ return defaultBeanNameGenerator (importBeanNameGenerator );
327
+ }
328
+
308
329
/**
309
330
* Returns the {@link BeanNameGenerator} to use falling back to an {@link AnnotationBeanNameGenerator} if either the
310
331
* given generator is {@literal null} or it's the one locally declared in {@link ConfigurationClassPostProcessor}'s
@@ -321,4 +342,37 @@ private static BeanNameGenerator defaultBeanNameGenerator(@Nullable BeanNameGene
321
342
? new AnnotationBeanNameGenerator () //
322
343
: generator ;
323
344
}
345
+
346
+ /**
347
+ * Obtain a configured {@link BeanNameGenerator}.
348
+ *
349
+ * @param beanClassLoader a class loader to load the configured {@link BeanNameGenerator} class in case it was
350
+ * configured as String instead of a Class instance.
351
+ * @return the bean name generator.
352
+ */
353
+ @ Nullable
354
+ @ SuppressWarnings ("unchecked" )
355
+ private static BeanNameGenerator getBeanNameGenerator (Map <String , Object > annotationAttributes ,
356
+ ClassLoader beanClassLoader ) {
357
+
358
+ Object configuredBeanNameGenerator = annotationAttributes .get (BEAN_NAME_GENERATOR );
359
+
360
+ if (configuredBeanNameGenerator == null ) {
361
+ return null ;
362
+ }
363
+
364
+ if (configuredBeanNameGenerator instanceof String cbng ) {
365
+ try {
366
+ configuredBeanNameGenerator = ClassUtils .forName (cbng , beanClassLoader );
367
+ } catch (Exception o_O ) {
368
+ throw new RuntimeException (o_O );
369
+ }
370
+ }
371
+
372
+ if (configuredBeanNameGenerator != BeanNameGenerator .class ) {
373
+ return BeanUtils .instantiateClass ((Class <? extends BeanNameGenerator >) configuredBeanNameGenerator );
374
+ }
375
+
376
+ return null ;
377
+ }
324
378
}
0 commit comments