Skip to content

Commit 4cc3aec

Browse files
committed
Consider @componentscan in imports context cache key
Fixes spring-projectsgh-31577
1 parent 1f0e311 commit 4cc3aec

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashSet;
2424
import java.util.LinkedHashSet;
2525
import java.util.Set;
26+
import java.util.stream.Collectors;
2627

2728
import org.springframework.beans.BeansException;
2829
import org.springframework.beans.factory.BeanFactory;
@@ -36,6 +37,7 @@
3637
import org.springframework.context.ApplicationContext;
3738
import org.springframework.context.ConfigurableApplicationContext;
3839
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
40+
import org.springframework.context.annotation.ComponentScan;
3941
import org.springframework.context.annotation.Configuration;
4042
import org.springframework.context.annotation.Import;
4143
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
@@ -237,7 +239,18 @@ static class ContextCustomizerKey {
237239
Set<Class<?>> seen = new HashSet<>();
238240
collectClassAnnotations(testClass, annotations, seen);
239241
Set<Object> determinedImports = determineImports(annotations, testClass);
240-
this.key = Collections.unmodifiableSet((determinedImports != null) ? determinedImports : annotations);
242+
if (determinedImports == null) {
243+
this.key = Collections.unmodifiableSet(annotations);
244+
}
245+
else {
246+
Set<Object> key = new HashSet<>();
247+
key.addAll(determinedImports);
248+
Set<Annotation> componentScanning = annotations.stream()
249+
.filter(ComponentScan.class::isInstance)
250+
.collect(Collectors.toSet());
251+
key.addAll(componentScanning);
252+
this.key = Collections.unmodifiableSet(key);
253+
}
241254
}
242255

243256
private void collectClassAnnotations(Class<?> classType, Set<Annotation> annotations, Set<Class<?>> seen) {

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerFactoryTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2525
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.ComponentScan;
2627
import org.springframework.context.annotation.Configuration;
2728
import org.springframework.context.annotation.Import;
2829
import org.springframework.stereotype.Component;
@@ -74,6 +75,20 @@ void contextCustomizerEqualsAndHashCode() {
7475
assertThat(customizer3).isEqualTo(customizer4);
7576
}
7677

78+
@Test
79+
void contextCustomizerEqualsAndHashCodeConsidersComponentScan() {
80+
ContextCustomizer customizer1 = this.factory
81+
.createContextCustomizer(TestWithImportAndComponentScanOfSomePackage.class, null);
82+
ContextCustomizer customizer2 = this.factory
83+
.createContextCustomizer(TestWithImportAndComponentScanOfSomePackage.class, null);
84+
ContextCustomizer customizer3 = this.factory
85+
.createContextCustomizer(TestWithImportAndComponentScanOfAnotherPackage.class, null);
86+
assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode());
87+
assertThat(customizer1).isEqualTo(customizer2);
88+
assertThat(customizer3.hashCode()).isNotEqualTo(customizer2.hashCode()).isNotEqualTo(customizer1.hashCode());
89+
assertThat(customizer3).isNotEqualTo(customizer2).isNotEqualTo(customizer1);
90+
}
91+
7792
@Test
7893
void getContextCustomizerWhenClassHasBeanMethodsShouldThrowException() {
7994
assertThatIllegalStateException()
@@ -105,6 +120,18 @@ static class TestWithImport {
105120

106121
}
107122

123+
@Import(ImportedBean.class)
124+
@ComponentScan("some.package")
125+
static class TestWithImportAndComponentScanOfSomePackage {
126+
127+
}
128+
129+
@Import(ImportedBean.class)
130+
@ComponentScan("another.package")
131+
static class TestWithImportAndComponentScanOfAnotherPackage {
132+
133+
}
134+
108135
@MetaImport
109136
static class TestWithMetaImport {
110137

0 commit comments

Comments
 (0)