Skip to content

Commit 6078fda

Browse files
committed
Polish "Use modifiable set for @ServletComponentScan with no packages"
Closes gh-12715
1 parent 7bec780 commit 6078fda

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 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.

spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -107,6 +107,37 @@ public void packagesFromMultipleAnnotationsAreMerged() {
107107
"com.example.bar", "com.example.baz");
108108
}
109109

110+
@Test
111+
public void withNoBasePackagesScanningUsesBasePackageOfAnnotatedClass() {
112+
this.context = new AnnotationConfigApplicationContext(NoBasePackages.class);
113+
ServletComponentRegisteringPostProcessor postProcessor = this.context
114+
.getBean(ServletComponentRegisteringPostProcessor.class);
115+
assertThat(postProcessor.getPackagesToScan())
116+
.containsExactly("org.springframework.boot.web.servlet");
117+
}
118+
119+
@Test
120+
public void noBasePackageAndBasePackageAreCombinedCorrectly() {
121+
this.context = new AnnotationConfigApplicationContext(NoBasePackages.class,
122+
BasePackages.class);
123+
ServletComponentRegisteringPostProcessor postProcessor = this.context
124+
.getBean(ServletComponentRegisteringPostProcessor.class);
125+
assertThat(postProcessor.getPackagesToScan()).containsExactlyInAnyOrder(
126+
"org.springframework.boot.web.servlet", "com.example.foo",
127+
"com.example.bar");
128+
}
129+
130+
@Test
131+
public void basePackageAndNoBasePackageAreCombinedCorrectly() {
132+
this.context = new AnnotationConfigApplicationContext(BasePackages.class,
133+
NoBasePackages.class);
134+
ServletComponentRegisteringPostProcessor postProcessor = this.context
135+
.getBean(ServletComponentRegisteringPostProcessor.class);
136+
assertThat(postProcessor.getPackagesToScan()).containsExactlyInAnyOrder(
137+
"org.springframework.boot.web.servlet", "com.example.foo",
138+
"com.example.bar");
139+
}
140+
110141
@Configuration
111142
@ServletComponentScan({ "com.example.foo", "com.example.bar" })
112143
static class ValuePackages {
@@ -137,4 +168,10 @@ static class ValueAndBasePackages {
137168

138169
}
139170

171+
@Configuration
172+
@ServletComponentScan
173+
static class NoBasePackages {
174+
175+
}
176+
140177
}

0 commit comments

Comments
 (0)