3636import org .springframework .beans .factory .BeanClassLoaderAware ;
3737import org .springframework .beans .factory .BeanDefinitionStoreException ;
3838import org .springframework .beans .factory .BeanFactory ;
39- import org .springframework .beans .factory .BeanFactoryAware ;
40- import org .springframework .beans .factory .annotation .AutowiredAnnotationBeanPostProcessor ;
4139import org .springframework .beans .factory .config .BeanDefinition ;
4240import org .springframework .beans .factory .config .BeanDefinitionHolder ;
4341import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
44- import org .springframework .beans .factory .config .BeanPostProcessor ;
4542import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
4643import org .springframework .beans .factory .config .InstantiationAwareBeanPostProcessorAdapter ;
4744import org .springframework .beans .factory .config .SingletonBeanRegistry ;
5350import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
5451import org .springframework .beans .factory .support .BeanDefinitionRegistryPostProcessor ;
5552import org .springframework .beans .factory .support .BeanNameGenerator ;
56- import org .springframework .beans .factory .support .RootBeanDefinition ;
5753import org .springframework .context .EnvironmentAware ;
5854import org .springframework .context .ResourceLoaderAware ;
5955import org .springframework .context .annotation .ConfigurationClassEnhancer .EnhancedConfiguration ;
9187public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor ,
9288 PriorityOrdered , ResourceLoaderAware , BeanClassLoaderAware , EnvironmentAware {
9389
94- private static final String IMPORT_AWARE_PROCESSOR_BEAN_NAME =
95- ConfigurationClassPostProcessor .class .getName () + ".importAwareProcessor" ;
96-
9790 private static final String IMPORT_REGISTRY_BEAN_NAME =
9891 ConfigurationClassPostProcessor .class .getName () + ".importRegistry" ;
9992
100- private static final String ENHANCED_CONFIGURATION_PROCESSOR_BEAN_NAME =
101- ConfigurationClassPostProcessor .class .getName () + ".enhancedConfigurationProcessor" ;
102-
10393
10494 private final Log logger = LogFactory .getLog (getClass ());
10595
@@ -224,14 +214,6 @@ public void setBeanClassLoader(ClassLoader beanClassLoader) {
224214 */
225215 @ Override
226216 public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry ) {
227- RootBeanDefinition iabpp = new RootBeanDefinition (ImportAwareBeanPostProcessor .class );
228- iabpp .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
229- registry .registerBeanDefinition (IMPORT_AWARE_PROCESSOR_BEAN_NAME , iabpp );
230-
231- RootBeanDefinition ecbpp = new RootBeanDefinition (EnhancedConfigurationBeanPostProcessor .class );
232- ecbpp .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
233- registry .registerBeanDefinition (ENHANCED_CONFIGURATION_PROCESSOR_BEAN_NAME , ecbpp );
234-
235217 int registryId = System .identityHashCode (registry );
236218 if (this .registriesPostProcessed .contains (registryId )) {
237219 throw new IllegalStateException (
@@ -263,7 +245,9 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
263245 // Simply call processConfigurationClasses lazily at this point then.
264246 processConfigBeanDefinitions ((BeanDefinitionRegistry ) beanFactory );
265247 }
248+
266249 enhanceConfigurationClasses (beanFactory );
250+ beanFactory .addBeanPostProcessor (new ImportAwareBeanPostProcessor (beanFactory ));
267251 }
268252
269253 /**
@@ -422,18 +406,22 @@ else if (logger.isWarnEnabled() && beanFactory.containsSingleton(beanName)) {
422406 }
423407
424408
425- private static class ImportAwareBeanPostProcessor implements BeanPostProcessor , BeanFactoryAware , PriorityOrdered {
409+ private static class ImportAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
426410
427- private BeanFactory beanFactory ;
411+ private final BeanFactory beanFactory ;
428412
429- @ Override
430- public void setBeanFactory (BeanFactory beanFactory ) {
413+ public ImportAwareBeanPostProcessor (BeanFactory beanFactory ) {
431414 this .beanFactory = beanFactory ;
432415 }
433416
434417 @ Override
435- public int getOrder () {
436- return Ordered .HIGHEST_PRECEDENCE ;
418+ public PropertyValues postProcessPropertyValues (PropertyValues pvs , PropertyDescriptor [] pds , Object bean , String beanName ) {
419+ // Inject the BeanFactory before AutowiredAnnotationBeanPostProcessor's
420+ // postProcessPropertyValues method attempts to autowire other configuration beans.
421+ if (bean instanceof EnhancedConfiguration ) {
422+ ((EnhancedConfiguration ) bean ).setBeanFactory (this .beanFactory );
423+ }
424+ return pvs ;
437425 }
438426
439427 @ Override
@@ -454,36 +442,4 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
454442 }
455443 }
456444
457-
458- /**
459- * {@link InstantiationAwareBeanPostProcessorAdapter} that ensures
460- * {@link EnhancedConfiguration} beans are injected with the {@link BeanFactory}
461- * before the {@link AutowiredAnnotationBeanPostProcessor} runs (SPR-10668).
462- */
463- private static class EnhancedConfigurationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
464- implements PriorityOrdered , BeanFactoryAware {
465-
466- private BeanFactory beanFactory ;
467-
468- @ Override
469- public int getOrder () {
470- return Ordered .HIGHEST_PRECEDENCE ;
471- }
472-
473- @ Override
474- public void setBeanFactory (BeanFactory beanFactory ) {
475- this .beanFactory = beanFactory ;
476- }
477-
478- @ Override
479- public PropertyValues postProcessPropertyValues (PropertyValues pvs , PropertyDescriptor [] pds , Object bean , String beanName ) {
480- // Inject the BeanFactory before AutowiredAnnotationBeanPostProcessor's
481- // postProcessPropertyValues method attempts to auto-wire other configuration beans.
482- if (bean instanceof EnhancedConfiguration ) {
483- ((EnhancedConfiguration ) bean ).setBeanFactory (this .beanFactory );
484- }
485- return pvs ;
486- }
487- }
488-
489445}
0 commit comments