Skip to content

Commit 8689395

Browse files
committed
Add tests for multiple composed @ComponentScan/@propertysource annotations
See spring-projectsgh-30941
1 parent 967a05e commit 8689395

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.beans.factory.config.BeanDefinition;
4545
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
4646
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
47+
import org.springframework.context.ApplicationContext;
4748
import org.springframework.context.EnvironmentAware;
4849
import org.springframework.context.ResourceLoaderAware;
4950
import org.springframework.context.annotation.ComponentScan.Filter;
@@ -143,6 +144,15 @@ public void viaContextRegistration_WithComposedAnnotation() {
143144
.isTrue();
144145
}
145146

147+
@Test
148+
void multipleComposedComponentScanAnnotations() { // gh-30941
149+
ApplicationContext ctx = new AnnotationConfigApplicationContext(MultipleComposedAnnotationsConfig.class);
150+
ctx.getBean(MultipleComposedAnnotationsConfig.class);
151+
assertContextContainsBean(ctx, "componentScanAnnotationIntegrationTests.MultipleComposedAnnotationsConfig");
152+
assertContextContainsBean(ctx, "simpleComponent");
153+
assertContextContainsBean(ctx, "barComponent");
154+
}
155+
146156
@Test
147157
public void viaBeanRegistration() {
148158
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -267,6 +277,10 @@ public void withBasePackagesAndValueAlias() {
267277
assertThat(ctx.containsBean("fooServiceImpl")).isTrue();
268278
}
269279

280+
private static void assertContextContainsBean(ApplicationContext ctx, String beanName) {
281+
assertThat(ctx.containsBean(beanName)).as("context contains bean " + beanName).isTrue();
282+
}
283+
270284

271285
@Configuration
272286
@ComponentScan
@@ -278,10 +292,25 @@ public void withBasePackagesAndValueAlias() {
278292
String[] basePackages() default {};
279293
}
280294

295+
@Configuration
296+
@ComponentScan
297+
@Retention(RetentionPolicy.RUNTIME)
298+
@Target(ElementType.TYPE)
299+
public @interface ComposedConfiguration2 {
300+
301+
@AliasFor(annotation = ComponentScan.class)
302+
String[] basePackages() default {};
303+
}
304+
281305
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
282306
public static class ComposedAnnotationConfig {
283307
}
284308

309+
@ComposedConfiguration(basePackages = "org.springframework.context.annotation.componentscan.simple")
310+
@ComposedConfiguration2(basePackages = "example.scannable.sub")
311+
static class MultipleComposedAnnotationsConfig {
312+
}
313+
285314
public static class AwareTypeFilter implements TypeFilter, EnvironmentAware,
286315
ResourceLoaderAware, BeanClassLoaderAware, BeanFactoryAware {
287316

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -32,6 +32,7 @@
3232
import org.springframework.beans.factory.BeanDefinitionStoreException;
3333
import org.springframework.beans.factory.FactoryBean;
3434
import org.springframework.beans.testfixture.beans.TestBean;
35+
import org.springframework.context.ApplicationContext;
3536
import org.springframework.context.ConfigurableApplicationContext;
3637
import org.springframework.core.annotation.AliasFor;
3738
import org.springframework.core.env.Environment;
@@ -239,6 +240,15 @@ void withRepeatedPropertySourcesOnComposedAnnotation() {
239240
}
240241
}
241242

243+
@Test
244+
void multipleComposedPropertySourceAnnotations() { // gh-30941
245+
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MultipleComposedAnnotationsConfig.class);
246+
ctx.getBean(MultipleComposedAnnotationsConfig.class);
247+
assertEnvironmentContainsProperty(ctx, "from.p1");
248+
assertEnvironmentContainsProperty(ctx, "from.p2");
249+
ctx.close();
250+
}
251+
242252
@Test
243253
void withNamedPropertySources() {
244254
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNamedPropertySources.class);
@@ -304,6 +314,10 @@ void orderingDoesntReplaceExisting() throws Exception {
304314
ctxWithoutName.close();
305315
}
306316

317+
private static void assertEnvironmentContainsProperty(ApplicationContext ctx, String name) {
318+
assertThat(ctx.getEnvironment().containsProperty(name)).as("environment contains property " + name).isTrue();
319+
}
320+
307321

308322
@Configuration
309323
@PropertySource("classpath:${unresolvable}/p1.properties")
@@ -496,6 +510,21 @@ static class ConfigWithRepeatedPropertySourceAnnotations {
496510
static class ConfigWithRepeatedPropertySourceAnnotationsOnComposedAnnotation {
497511
}
498512

513+
@Retention(RetentionPolicy.RUNTIME)
514+
@PropertySource("classpath:org/springframework/context/annotation/p1.properties")
515+
@interface PropertySource1 {
516+
}
517+
518+
@Retention(RetentionPolicy.RUNTIME)
519+
@PropertySource("classpath:org/springframework/context/annotation/p2.properties")
520+
@interface PropertySource2 {
521+
}
522+
523+
@Configuration
524+
@PropertySource1
525+
@PropertySource2
526+
static class MultipleComposedAnnotationsConfig {
527+
}
499528

500529
@Configuration
501530
@PropertySources({

0 commit comments

Comments
 (0)