diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java index cb1cae713b..b1ff89c0c3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrar.java @@ -57,12 +57,11 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar { Set packagesToScan) { BeanDefinition definition = registry.getBeanDefinition(BEAN_NAME); ValueHolder constructorArguments = definition.getConstructorArgumentValues() - .getGenericArgumentValue(String[].class); + .getGenericArgumentValue(Set.class); @SuppressWarnings("unchecked") - Set mergedPackages = new LinkedHashSet( - (Set) constructorArguments.getValue()); + Set mergedPackages = (Set) constructorArguments.getValue(); mergedPackages.addAll(packagesToScan); - constructorArguments.setValue(packagesToScan); + constructorArguments.setValue(mergedPackages); } private void addPostProcessor(BeanDefinitionRegistry registry, diff --git a/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java b/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java index 3723411057..40b04cce01 100644 --- a/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletComponentScanRegistrarTests.java @@ -84,6 +84,18 @@ public class ServletComponentScanRegistrarTests { .getPackage().getName())); } + @Test + public void packagesFromMultipleAnnotationsAreMerged() { + this.context = new AnnotationConfigApplicationContext(BasePackages.class, + AdditionalPackages.class); + ServletComponentRegisteringPostProcessor postProcessor = this.context + .getBean(ServletComponentRegisteringPostProcessor.class); + assertThat( + postProcessor.getPackagesToScan(), + containsInAnyOrder("com.example.foo", "com.example.bar", + "com.example.baz")); + } + @Configuration @ServletComponentScan({ "com.example.foo", "com.example.bar" }) static class ValuePackages { @@ -96,6 +108,12 @@ public class ServletComponentScanRegistrarTests { } + @Configuration + @ServletComponentScan(basePackages = "com.example.baz") + static class AdditionalPackages { + + } + @Configuration @ServletComponentScan(basePackageClasses = ServletComponentScanRegistrarTests.class) static class BasePackageClasses {