Skip to content

Commit a5ce965

Browse files
committed
ConfigurationClassPostProcessor programmatically registers unified ImportAwareBeanPostProcessor
Issue: SPR-14931 (cherry picked from commit f6b2a21)
1 parent 1e58c80 commit a5ce965

File tree

2 files changed

+21
-75
lines changed

2 files changed

+21
-75
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@
3636
import org.springframework.beans.factory.BeanClassLoaderAware;
3737
import org.springframework.beans.factory.BeanDefinitionStoreException;
3838
import org.springframework.beans.factory.BeanFactory;
39-
import org.springframework.beans.factory.BeanFactoryAware;
40-
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
4139
import org.springframework.beans.factory.config.BeanDefinition;
4240
import org.springframework.beans.factory.config.BeanDefinitionHolder;
4341
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
44-
import org.springframework.beans.factory.config.BeanPostProcessor;
4542
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4643
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
4744
import org.springframework.beans.factory.config.SingletonBeanRegistry;
@@ -53,7 +50,6 @@
5350
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
5451
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
5552
import org.springframework.beans.factory.support.BeanNameGenerator;
56-
import org.springframework.beans.factory.support.RootBeanDefinition;
5753
import org.springframework.context.EnvironmentAware;
5854
import org.springframework.context.ResourceLoaderAware;
5955
import org.springframework.context.annotation.ConfigurationClassEnhancer.EnhancedConfiguration;
@@ -91,15 +87,9 @@
9187
public 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
}

spring-context/src/test/java/org/springframework/context/annotation/configuration/Spr10668Tests.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,42 +38,32 @@ public class Spr10668Tests {
3838

3939
@Test
4040
public void testSelfInjectHierarchy() throws Exception {
41-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
42-
ChildConfig.class);
41+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ChildConfig.class);
4342
assertNotNull(context.getBean(MyComponent.class));
4443
context.close();
4544
}
4645

46+
4747
@Configuration
48-
public static class ParentConfig implements BeanFactoryAware {
48+
public static class ParentConfig {
4949

5050
@Autowired(required = false)
5151
MyComponent component;
52-
53-
public ParentConfig() {
54-
System.out.println("Parent " + getClass());
55-
}
56-
57-
@Override
58-
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
59-
System.out.println("BFA " + getClass());
60-
}
61-
6252
}
6353

54+
6455
@Configuration
6556
public static class ChildConfig extends ParentConfig {
6657

6758
@Bean
6859
public MyComponentImpl myComponent() {
6960
return new MyComponentImpl();
7061
}
71-
7262
}
7363

74-
public static interface MyComponent {
75-
}
7664

77-
public static class MyComponentImpl implements MyComponent {
78-
}
65+
public interface MyComponent {}
66+
67+
public static class MyComponentImpl implements MyComponent {}
68+
7969
}

0 commit comments

Comments
 (0)