Use modifiable set for @ServletComponentScan with no packages

Previously, when a project contained multiple `@ServletComponentScan`
annotated classes in classpath, and at least one annotation don't
explicitly specify `basePackages` and `basePackageClass` attribute,
the application could fail to start with an
UnsupportedOperationException. The failure occurred due to the
creating of an unmodifiable set when no base packages are configured
and a subsequent attempt to add base packages to that sit.

This commit fixes the issue by removing the use of an unmodifiable set
when `@ServletComponentScan` with no base packages in processed before
any other `@ServletComponentScan` annotations.

See gh-12715
pull/12859/head
Wenwei Liao 7 years ago committed by Andy Wilkinson
parent 2fd177f923
commit 7bec780281

@ -17,7 +17,6 @@
package org.springframework.boot.web.servlet; package org.springframework.boot.web.servlet;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -84,8 +83,7 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
packagesToScan.add(ClassUtils.getPackageName(basePackageClass)); packagesToScan.add(ClassUtils.getPackageName(basePackageClass));
} }
if (packagesToScan.isEmpty()) { if (packagesToScan.isEmpty()) {
return Collections packagesToScan.add(ClassUtils.getPackageName(metadata.getClassName()));
.singleton(ClassUtils.getPackageName(metadata.getClassName()));
} }
return packagesToScan; return packagesToScan;
} }

Loading…
Cancel
Save