43
43
import org .springframework .beans .factory .parsing .ProblemReporter ;
44
44
import org .springframework .beans .factory .parsing .SourceExtractor ;
45
45
import org .springframework .beans .factory .support .AbstractBeanDefinition ;
46
- import org .springframework .beans .factory .support .BeanDefinitionReaderUtils ;
47
46
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
48
47
import org .springframework .beans .factory .support .BeanDefinitionRegistryPostProcessor ;
49
48
import org .springframework .beans .factory .support .BeanNameGenerator ;
88
87
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor ,
89
88
ResourceLoaderAware , BeanClassLoaderAware , EnvironmentAware {
90
89
90
+ private static final String IMPORT_AWARE_PROCESSOR_BEAN_NAME =
91
+ ConfigurationClassPostProcessor .class .getName () + ".importAwareProcessor" ;
92
+
93
+ private static final String IMPORT_REGISTRY_BEAN_NAME =
94
+ ConfigurationClassPostProcessor .class .getName () + ".importRegistry" ;
95
+
96
+
91
97
private final Log logger = LogFactory .getLog (getClass ());
92
98
93
99
private SourceExtractor sourceExtractor = new PassThroughSourceExtractor ();
@@ -110,7 +116,18 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
110
116
111
117
private ConfigurationClassBeanDefinitionReader reader ;
112
118
113
- private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator ();
119
+ private boolean localBeanNameGeneratorSet = false ;
120
+
121
+ /* using short class names as default bean names */
122
+ private BeanNameGenerator componentScanBeanNameGenerator = new AnnotationBeanNameGenerator ();
123
+
124
+ /* using fully qualified class names as default bean names */
125
+ private BeanNameGenerator importBeanNameGenerator = new AnnotationBeanNameGenerator () {
126
+ @ Override
127
+ protected String buildDefaultBeanName (BeanDefinition definition ) {
128
+ return definition .getBeanClassName ();
129
+ }
130
+ };
114
131
115
132
116
133
/**
@@ -143,23 +160,27 @@ public void setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory
143
160
}
144
161
145
162
/**
146
- * Set the {@link BeanNameGenerator} to be used when registering imported and nested
147
- * {@link Configuration} classes. The default is {@link AnnotationBeanNameGenerator}.
163
+ * Set the {@link BeanNameGenerator} to be used when triggering component scanning
164
+ * from {@link Configuration} classes and when registering {@link Import}'ed
165
+ * configuration classes. The default is a standard {@link AnnotationBeanNameGenerator}
166
+ * for scanned components (compatible with the default in {@link ClassPathBeanDefinitionScanner})
167
+ * and a variant thereof for imported configuration classes (using unique fully-qualified
168
+ * class names instead of standard component overriding).
148
169
* <p>Note that this strategy does <em>not</em> apply to {@link Bean} methods.
149
170
* <p>This setter is typically only appropriate when configuring the post-processor as
150
171
* a standalone bean definition in XML, e.g. not using the dedicated
151
172
* {@code AnnotationConfig*} application contexts or the {@code
152
173
* <context:annotation-config>} element. Any bean name generator specified against
153
174
* the application context will take precedence over any value set here.
154
- * @param beanNameGenerator the strategy to use when generating configuration class
155
- * bean names
156
175
* @since 3.1.1
157
176
* @see AnnotationConfigApplicationContext#setBeanNameGenerator(BeanNameGenerator)
158
177
* @see AnnotationConfigUtils#CONFIGURATION_BEAN_NAME_GENERATOR
159
178
*/
160
179
public void setBeanNameGenerator (BeanNameGenerator beanNameGenerator ) {
161
180
Assert .notNull (beanNameGenerator , "BeanNameGenerator must not be null" );
162
- this .beanNameGenerator = beanNameGenerator ;
181
+ this .localBeanNameGeneratorSet = true ;
182
+ this .componentScanBeanNameGenerator = beanNameGenerator ;
183
+ this .importBeanNameGenerator = beanNameGenerator ;
163
184
}
164
185
165
186
public void setEnvironment (Environment environment ) {
@@ -184,7 +205,8 @@ public void setBeanClassLoader(ClassLoader beanClassLoader) {
184
205
* Derive further bean definitions from the configuration classes in the registry.
185
206
*/
186
207
public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry ) {
187
- BeanDefinitionReaderUtils .registerWithGeneratedName (new RootBeanDefinition (ImportAwareBeanPostProcessor .class ), registry );
208
+ registry .registerBeanDefinition (IMPORT_AWARE_PROCESSOR_BEAN_NAME ,
209
+ new RootBeanDefinition (ImportAwareBeanPostProcessor .class ));
188
210
int registryId = System .identityHashCode (registry );
189
211
if (this .registriesPostProcessed .contains (registryId )) {
190
212
throw new IllegalStateException (
@@ -239,15 +261,17 @@ public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
239
261
SingletonBeanRegistry singletonRegistry = null ;
240
262
if (registry instanceof SingletonBeanRegistry ) {
241
263
singletonRegistry = (SingletonBeanRegistry ) registry ;
242
- if (singletonRegistry .containsSingleton (CONFIGURATION_BEAN_NAME_GENERATOR )) {
243
- this .beanNameGenerator = (BeanNameGenerator ) singletonRegistry .getSingleton (CONFIGURATION_BEAN_NAME_GENERATOR );
264
+ if (!this .localBeanNameGeneratorSet && singletonRegistry .containsSingleton (CONFIGURATION_BEAN_NAME_GENERATOR )) {
265
+ BeanNameGenerator generator = (BeanNameGenerator ) singletonRegistry .getSingleton (CONFIGURATION_BEAN_NAME_GENERATOR );
266
+ this .componentScanBeanNameGenerator = generator ;
267
+ this .importBeanNameGenerator = generator ;
244
268
}
245
269
}
246
270
247
271
// Parse each @Configuration class
248
272
ConfigurationClassParser parser = new ConfigurationClassParser (
249
273
this .metadataReaderFactory , this .problemReporter , this .environment ,
250
- this .resourceLoader , this .beanNameGenerator , registry );
274
+ this .resourceLoader , this .componentScanBeanNameGenerator , registry );
251
275
for (BeanDefinitionHolder holder : configCandidates ) {
252
276
BeanDefinition bd = holder .getBeanDefinition ();
253
277
try {
@@ -282,16 +306,15 @@ public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
282
306
// Read the model and create bean definitions based on its content
283
307
if (this .reader == null ) {
284
308
this .reader = new ConfigurationClassBeanDefinitionReader (
285
- registry , this .sourceExtractor , this .problemReporter ,
286
- this .metadataReaderFactory , this .resourceLoader , this .environment ,
287
- this .beanNameGenerator );
309
+ registry , this .sourceExtractor , this .problemReporter , this .metadataReaderFactory ,
310
+ this .resourceLoader , this .environment , this .importBeanNameGenerator );
288
311
}
289
312
this .reader .loadBeanDefinitions (parser .getConfigurationClasses ());
290
313
291
314
// Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
292
315
if (singletonRegistry != null ) {
293
- if (!singletonRegistry .containsSingleton ("importRegistry" )) {
294
- singletonRegistry .registerSingleton ("importRegistry" , parser .getImportRegistry ());
316
+ if (!singletonRegistry .containsSingleton (IMPORT_REGISTRY_BEAN_NAME )) {
317
+ singletonRegistry .registerSingleton (IMPORT_REGISTRY_BEAN_NAME , parser .getImportRegistry ());
295
318
}
296
319
}
297
320
}
@@ -349,7 +372,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
349
372
350
373
public Object postProcessBeforeInitialization (Object bean , String beanName ) throws BeansException {
351
374
if (bean instanceof ImportAware ) {
352
- ImportRegistry importRegistry = beanFactory .getBean (ImportRegistry .class );
375
+ ImportRegistry importRegistry = this . beanFactory .getBean (IMPORT_REGISTRY_BEAN_NAME , ImportRegistry .class );
353
376
String importingClass = importRegistry .getImportingClassFor (bean .getClass ().getSuperclass ().getName ());
354
377
if (importingClass != null ) {
355
378
try {
0 commit comments