Skip to content

Commit 399455f

Browse files
committed
Merge pull request #12715 from Wenwei Liao
* gh-12715: Polish "Use modifiable set for @ServletComponentScan with no packages" Use modifiable set for @ServletComponentScan with no packages
2 parents 2fd177f + 6078fda commit 399455f

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

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

Lines changed: 2 additions & 4 deletions
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.
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.web.servlet;
1818

1919
import java.util.Arrays;
20-
import java.util.Collections;
2120
import java.util.LinkedHashSet;
2221
import java.util.Set;
2322

@@ -84,8 +83,7 @@ private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
8483
packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
8584
}
8685
if (packagesToScan.isEmpty()) {
87-
return Collections
88-
.singleton(ClassUtils.getPackageName(metadata.getClassName()));
86+
packagesToScan.add(ClassUtils.getPackageName(metadata.getClassName()));
8987
}
9088
return packagesToScan;
9189
}

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)