Skip to content

Commit f1fbe85

Browse files
committed
Investigate issue with @componentscan as meta-annotation
This commit introduces unit and integration tests that attempt to reproduce the issue claimed by the reporter in SPR-11557. However, the tests pass without any problems. Issue: SPR-11557
1 parent 3d506eb commit f1fbe85

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java

Lines changed: 39 additions & 2 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-2014 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.
@@ -17,6 +17,10 @@
1717
package org.springframework.context.annotation;
1818

1919
import java.io.IOException;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
2024
import java.util.HashSet;
2125

2226
import example.scannable.CustomComponent;
@@ -28,15 +32,16 @@
2832
import example.scannable_implicitbasepackage.ComponentScanAnnotatedConfigWithImplicitBasePackage;
2933
import example.scannable_scoped.CustomScopeAnnotationBean;
3034
import example.scannable_scoped.MyScope;
31-
import org.junit.Test;
3235

36+
import org.junit.Test;
3337
import org.springframework.aop.support.AopUtils;
3438
import org.springframework.beans.factory.annotation.CustomAutowireConfigurer;
3539
import org.springframework.beans.factory.config.BeanDefinition;
3640
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3741
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3842
import org.springframework.context.annotation.ComponentScan.Filter;
3943
import org.springframework.context.annotation.ComponentScanParserTests.CustomAnnotationAutowiredBean;
44+
import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
4045
import org.springframework.context.support.GenericApplicationContext;
4146
import org.springframework.tests.context.SimpleMapScope;
4247
import org.springframework.util.SerializationTestUtils;
@@ -50,8 +55,10 @@
5055
*
5156
* @author Chris Beams
5257
* @author Juergen Hoeller
58+
* @author Sam Brannen
5359
* @since 3.1
5460
*/
61+
@SuppressWarnings("resource")
5562
public class ComponentScanAnnotationIntegrationTests {
5663

5764
@Test
@@ -101,6 +108,20 @@ public void viaContextRegistration_FromPackageOfConfigClass() {
101108
ctx.containsBean("scannedComponent"), is(true));
102109
}
103110

111+
@Test
112+
public void viaContextRegistration_WithComposedAnnotation() {
113+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
114+
ctx.register(ComposedAnnotationConfig.class);
115+
ctx.refresh();
116+
ctx.getBean(ComposedAnnotationConfig.class);
117+
ctx.getBean(SimpleComponent.class);
118+
assertThat("config class bean not found",
119+
ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig"), is(true));
120+
assertThat("@ComponentScan annotated @Configuration class registered directly against "
121+
+ "AnnotationConfigApplicationContext did not trigger component scanning as expected",
122+
ctx.containsBean("simpleComponent"), is(true));
123+
}
124+
104125
@Test
105126
public void viaBeanRegistration() {
106127
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -209,6 +230,22 @@ public void withBasePackagesAndValueAlias() {
209230
assertThat(ctx.containsBean("fooServiceImpl"), is(true));
210231
}
211232

233+
234+
@Configuration
235+
@ComponentScan
236+
@Retention(RetentionPolicy.RUNTIME)
237+
@Target(ElementType.TYPE)
238+
public static @interface ComposedConfiguration {
239+
240+
String[] basePackages() default {};
241+
242+
String[] bundles() default {};
243+
}
244+
245+
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
246+
public static class ComposedAnnotationConfig {
247+
}
248+
212249
}
213250

214251

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package org.springframework.context.annotation;
1818

19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
1924
import org.junit.Before;
2025
import org.junit.Test;
2126
import org.springframework.aop.scope.ScopedObject;
@@ -29,6 +34,8 @@
2934
import org.springframework.beans.factory.support.ChildBeanDefinition;
3035
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3136
import org.springframework.beans.factory.support.RootBeanDefinition;
37+
import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
38+
import org.springframework.core.env.StandardEnvironment;
3239
import org.springframework.core.io.DescriptiveResource;
3340
import org.springframework.tests.sample.beans.ITestBean;
3441
import org.springframework.tests.sample.beans.TestBean;
@@ -103,6 +110,16 @@ public void postProcessorIntrospectsInheritedDefinitionsCorrectly() {
103110
assertSame(foo, bar.foo);
104111
}
105112

113+
@Test
114+
public void postProcessorWorksWithComposedAnnotations() {
115+
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ComposedAnnotationConfig.class));
116+
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
117+
pp.setEnvironment(new StandardEnvironment());
118+
pp.postProcessBeanFactory(beanFactory);
119+
SimpleComponent simpleComponent = beanFactory.getBean(SimpleComponent.class);
120+
assertNotNull(simpleComponent);
121+
}
122+
106123
@Test
107124
public void postProcessorOverridesNonApplicationBeanDefinitions() {
108125
RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
@@ -610,4 +627,19 @@ public Object repoConsumer(Repository<String> repo) {
610627
}
611628
}
612629

630+
@Configuration
631+
@ComponentScan
632+
@Retention(RetentionPolicy.RUNTIME)
633+
@Target(ElementType.TYPE)
634+
public static @interface ComposedConfiguration {
635+
636+
String[] basePackages() default {};
637+
638+
String[] bundles() default {};
639+
}
640+
641+
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
642+
public static class ComposedAnnotationConfig {
643+
}
644+
613645
}

0 commit comments

Comments
 (0)