Skip to content

Commit 6698ed8

Browse files
committed
IN PROGRESS - issue SPR-5599: Hook @configuration class processing into <context:component-scan/> and <context:annotation-config/> through AnnotationConfigUtils#registerAnnotationConfigProcessors
1 parent 826c733 commit 6698ed8

File tree

6 files changed

+49
-39
lines changed

6 files changed

+49
-39
lines changed

org.springframework.context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@
2525
import org.springframework.beans.factory.config.BeanDefinitionHolder;
2626
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2727
import org.springframework.beans.factory.support.RootBeanDefinition;
28+
import org.springframework.context.annotation.support.ConfigurationClassPostProcessor;
2829
import org.springframework.util.ClassUtils;
2930

3031
/**
3132
* Utility class that allows for convenient registration of common
32-
* {@link org.springframework.beans.factory.config.BeanPostProcessor}
33+
* {@link org.springframework.beans.factory.config.BeanPostProcessor} and
34+
* {@link org.springframework.beans.factory.config.BeanFactoryPostProcessor}
3335
* definitions for annotation-based configuration.
3436
*
3537
* @author Mark Fisher
3638
* @author Juergen Hoeller
39+
* @author Chris Beams
3740
* @since 2.5
3841
* @see CommonAnnotationBeanPostProcessor
39-
* @see org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
42+
* @see org.springframework.context.annotation.support.ConfigurationClassPostProcessor;
4043
* @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
4144
* @see org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
45+
* @see org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
4246
*/
4347
public class AnnotationConfigUtils {
4448

@@ -48,6 +52,12 @@ public class AnnotationConfigUtils {
4852
public static final String AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME =
4953
"org.springframework.context.annotation.internalAutowiredAnnotationProcessor";
5054

55+
/**
56+
* The bean name of the internally managed Configuration annotation processor.
57+
*/
58+
public static final String CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME =
59+
"org.springframework.context.annotation.configurationAnnotationProcessor";
60+
5161
/**
5262
* The bean name of the internally managed Required annotation processor.
5363
*/
@@ -103,20 +113,26 @@ public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
103113
if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
104114
RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
105115
def.setSource(source);
106-
beanDefs.add(registerBeanPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
116+
beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
117+
}
118+
119+
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
120+
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
121+
def.setSource(source);
122+
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
107123
}
108124

109125
if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
110126
RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
111127
def.setSource(source);
112-
beanDefs.add(registerBeanPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
128+
beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
113129
}
114130

115131
// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
116132
if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
117133
RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
118134
def.setSource(source);
119-
beanDefs.add(registerBeanPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
135+
beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
120136
}
121137

122138
// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
@@ -131,13 +147,13 @@ public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
131147
"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
132148
}
133149
def.setSource(source);
134-
beanDefs.add(registerBeanPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
150+
beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
135151
}
136152

137153
return beanDefs;
138154
}
139155

140-
private static BeanDefinitionHolder registerBeanPostProcessor(
156+
private static BeanDefinitionHolder registerPostProcessor(
141157
BeanDefinitionRegistry registry, RootBeanDefinition definition, String beanName) {
142158

143159
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

org.springframework.context/src/test/java/org/springframework/context/annotation/ClassPathBeanDefinitionScannerTests.java

+20-19
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616

1717
package org.springframework.context.annotation;
1818

19-
import example.scannable.CustomComponent;
20-
import example.scannable.FooService;
21-
import example.scannable.FooServiceImpl;
22-
import example.scannable.NamedStubDao;
23-
import example.scannable.StubFooDao;
24-
import org.aspectj.lang.annotation.Aspect;
2519
import static org.junit.Assert.*;
26-
import org.junit.Test;
2720

21+
import org.aspectj.lang.annotation.Aspect;
22+
import org.junit.Test;
2823
import org.springframework.beans.TestBean;
2924
import org.springframework.beans.factory.BeanCreationException;
3025
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -39,6 +34,12 @@
3934
import org.springframework.core.type.filter.AssignableTypeFilter;
4035
import org.springframework.stereotype.Component;
4136

37+
import example.scannable.CustomComponent;
38+
import example.scannable.FooService;
39+
import example.scannable.FooServiceImpl;
40+
import example.scannable.NamedStubDao;
41+
import example.scannable.StubFooDao;
42+
4243
/**
4344
* @author Mark Fisher
4445
* @author Juergen Hoeller
@@ -54,7 +55,7 @@ public void testSimpleScanWithDefaultFiltersAndPostProcessors() {
5455
GenericApplicationContext context = new GenericApplicationContext();
5556
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
5657
int beanCount = scanner.scan(BASE_PACKAGE);
57-
assertEquals(9, beanCount);
58+
assertEquals(10, beanCount);
5859
assertTrue(context.containsBean("serviceInvocationCounter"));
5960
assertTrue(context.containsBean("fooServiceImpl"));
6061
assertTrue(context.containsBean("stubFooDao"));
@@ -71,7 +72,7 @@ public void testDoubleScan() {
7172
GenericApplicationContext context = new GenericApplicationContext();
7273
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
7374
int beanCount = scanner.scan(BASE_PACKAGE);
74-
assertEquals(9, beanCount);
75+
assertEquals(10, beanCount);
7576
scanner.scan(BASE_PACKAGE);
7677
assertTrue(context.containsBean("serviceInvocationCounter"));
7778
assertTrue(context.containsBean("fooServiceImpl"));
@@ -189,7 +190,7 @@ public void testCustomIncludeFilterWithoutDefaultsButIncludingPostProcessors() {
189190
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false);
190191
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
191192
int beanCount = scanner.scan(BASE_PACKAGE);
192-
assertEquals(4, beanCount);
193+
assertEquals(5, beanCount);
193194
assertTrue(context.containsBean("messageBean"));
194195
assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
195196
assertTrue(context.containsBean(AnnotationConfigUtils.REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
@@ -202,7 +203,7 @@ public void testCustomIncludeFilterWithoutDefaultsAndNoPostProcessors() {
202203
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false);
203204
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
204205
int beanCount = scanner.scan(BASE_PACKAGE);
205-
assertEquals(4, beanCount);
206+
assertEquals(5, beanCount);
206207
assertTrue(context.containsBean("messageBean"));
207208
assertFalse(context.containsBean("serviceInvocationCounter"));
208209
assertFalse(context.containsBean("fooServiceImpl"));
@@ -220,7 +221,7 @@ public void testCustomIncludeFilterAndDefaults() {
220221
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
221222
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
222223
int beanCount = scanner.scan(BASE_PACKAGE);
223-
assertEquals(10, beanCount);
224+
assertEquals(11, beanCount);
224225
assertTrue(context.containsBean("messageBean"));
225226
assertTrue(context.containsBean("serviceInvocationCounter"));
226227
assertTrue(context.containsBean("fooServiceImpl"));
@@ -238,7 +239,7 @@ public void testCustomAnnotationExcludeFilterAndDefaults() {
238239
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
239240
scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
240241
int beanCount = scanner.scan(BASE_PACKAGE);
241-
assertEquals(8, beanCount);
242+
assertEquals(9, beanCount);
242243
assertFalse(context.containsBean("serviceInvocationCounter"));
243244
assertTrue(context.containsBean("fooServiceImpl"));
244245
assertTrue(context.containsBean("stubFooDao"));
@@ -255,7 +256,7 @@ public void testCustomAssignableTypeExcludeFilterAndDefaults() {
255256
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
256257
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class));
257258
int beanCount = scanner.scan(BASE_PACKAGE);
258-
assertEquals(8, beanCount);
259+
assertEquals(9, beanCount);
259260
assertFalse(context.containsBean("fooServiceImpl"));
260261
assertTrue(context.containsBean("serviceInvocationCounter"));
261262
assertTrue(context.containsBean("stubFooDao"));
@@ -291,7 +292,7 @@ public void testMultipleCustomExcludeFiltersAndDefaults() {
291292
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class));
292293
scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
293294
int beanCount = scanner.scan(BASE_PACKAGE);
294-
assertEquals(7, beanCount);
295+
assertEquals(8, beanCount);
295296
assertFalse(context.containsBean("fooServiceImpl"));
296297
assertFalse(context.containsBean("serviceInvocationCounter"));
297298
assertTrue(context.containsBean("stubFooDao"));
@@ -308,7 +309,7 @@ public void testCustomBeanNameGenerator() {
308309
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
309310
scanner.setBeanNameGenerator(new TestBeanNameGenerator());
310311
int beanCount = scanner.scan(BASE_PACKAGE);
311-
assertEquals(9, beanCount);
312+
assertEquals(10, beanCount);
312313
assertFalse(context.containsBean("fooServiceImpl"));
313314
assertTrue(context.containsBean("fooService"));
314315
assertTrue(context.containsBean("serviceInvocationCounter"));
@@ -327,7 +328,7 @@ public void testMultipleBasePackagesWithDefaultsOnly() {
327328
GenericApplicationContext multiPackageContext = new GenericApplicationContext();
328329
ClassPathBeanDefinitionScanner multiPackageScanner = new ClassPathBeanDefinitionScanner(multiPackageContext);
329330
int singlePackageBeanCount = singlePackageScanner.scan(BASE_PACKAGE);
330-
assertEquals(9, singlePackageBeanCount);
331+
assertEquals(10, singlePackageBeanCount);
331332
multiPackageScanner.scan(BASE_PACKAGE, "org.springframework.dao.annotation");
332333
// assertTrue(multiPackageBeanCount > singlePackageBeanCount);
333334
}
@@ -337,7 +338,7 @@ public void testMultipleScanCalls() {
337338
GenericApplicationContext context = new GenericApplicationContext();
338339
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
339340
int beanCount = scanner.scan(BASE_PACKAGE);
340-
assertEquals(9, beanCount);
341+
assertEquals(10, beanCount);
341342
assertEquals(beanCount, context.getBeanDefinitionCount());
342343
int addedBeanCount = scanner.scan("org.springframework.aop.aspectj.annotation");
343344
assertEquals(beanCount + addedBeanCount, context.getBeanDefinitionCount());
@@ -350,7 +351,7 @@ public void testBeanAutowiredWithAnnotationConfigEnabled() {
350351
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
351352
scanner.setBeanNameGenerator(new TestBeanNameGenerator());
352353
int beanCount = scanner.scan(BASE_PACKAGE);
353-
assertEquals(9, beanCount);
354+
assertEquals(10, beanCount);
354355
context.refresh();
355356

356357
FooServiceImpl fooService = (FooServiceImpl) context.getBean("fooService");

org.springframework.context/src/test/java/org/springframework/context/annotation/ClassPathFactoryBeanDefinitionScannerTests.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.context.annotation;
1818

19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertTrue;
21-
2219
import java.util.Set;
2320

2421
import junit.framework.TestCase;
@@ -38,7 +35,7 @@ public class ClassPathFactoryBeanDefinitionScannerTests extends TestCase {
3835

3936
private static final String BASE_PACKAGE = FactoryMethodComponent.class.getPackage().getName();
4037

41-
private static final int NUM_DEFAULT_BEAN_DEFS = 4;
38+
private static final int NUM_DEFAULT_BEAN_DEFS = 5;
4239

4340
private static final int NUM_FACTORY_METHODS = 5; // @ScopedProxy creates another
4441

org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
xmlns:context="http://www.springframework.org/schema/context">
77

88
<context:annotation-config/>
9-
10-
<bean class="org.springframework.context.annotation.support.ConfigurationClassPostProcessor"/>
11-
9+
1210
<bean class="org.springframework.context.annotation.configuration.AutowiredConfigurationTests$AutowiredConfig"/>
1311
<bean class="org.springframework.context.annotation.configuration.AutowiredConfigurationTests$ColorConfig"/>
14-
12+
1513
</beans>

org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ValueInjectionTests.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
xmlns:context="http://www.springframework.org/schema/context">
77

88
<context:annotation-config/>
9-
10-
<bean class="org.springframework.context.annotation.support.ConfigurationClassPostProcessor"/>
11-
9+
1210
<bean class="org.springframework.context.annotation.configuration.AutowiredConfigurationTests$ValueConfig"/>
13-
11+
1412
<!--
1513
<context:component-scan base-package="test.basic.value"/>
1614
-->

org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/aspectj-autoproxy-config.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
77
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
88
xmlns:context="http://www.springframework.org/schema/context">
9-
9+
1010
<aop:aspectj-autoproxy proxy-target-class="true"/>
1111
</beans>

0 commit comments

Comments
 (0)