diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationUtils.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationUtils.java index 0358e14bc9a..c22c748d9f7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationUtils.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationUtils.java @@ -18,7 +18,9 @@ package org.springframework.boot.autoconfigure; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; @@ -36,6 +38,13 @@ public abstract class AutoConfigurationUtils { private static final String BASE_PACKAGES_BEAN = AutoConfigurationUtils.class .getName() + ".basePackages"; + private static Set EXCLUDED_PACKAGES; + static { + Set exclude = new HashSet(); + exclude.add("org.springframework.data.rest.webmvc"); + EXCLUDED_PACKAGES = Collections.unmodifiableSet(exclude); + } + @SuppressWarnings("unchecked") public static List getBasePackages(BeanFactory beanFactory) { try { @@ -49,17 +58,14 @@ public abstract class AutoConfigurationUtils { public static void storeBasePackages(ConfigurableListableBeanFactory beanFactory, List basePackages) { if (!beanFactory.containsBean(BASE_PACKAGES_BEAN)) { - beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList( - basePackages)); + beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList()); } - else { - List packages = getBasePackages(beanFactory); - for (String pkg : basePackages) { - if (packages.contains(pkg)) { - packages.add(pkg); - } + List storePackages = getBasePackages(beanFactory); + for (String basePackage : basePackages) { + if (!EXCLUDED_PACKAGES.contains(basePackage) + && !storePackages.contains(basePackage)) { + storePackages.add(basePackage); } } } - } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationUtilsTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationUtilsTests.java new file mode 100644 index 00000000000..9b3cf38fa23 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationUtilsTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link AutoConfigurationUtils}. + * + * @author Phillip Webb + */ +public class AutoConfigurationUtilsTests { + + private ConfigurableListableBeanFactory beanFactory; + + @Before + public void setup() { + this.beanFactory = new DefaultListableBeanFactory(); + } + + @Test + public void storeAndGetBasePackages() throws Exception { + List packageList = Arrays.asList("com.mycorp.test1", "com.mycorp.test2"); + AutoConfigurationUtils.storeBasePackages(this.beanFactory, packageList); + List actual = AutoConfigurationUtils.getBasePackages(this.beanFactory); + assertThat(actual, equalTo(packageList)); + } + + @Test + public void doubleAdd() throws Exception { + List list1 = Arrays.asList("com.mycorp.test1", "com.mycorp.test2"); + List list2 = Arrays.asList("com.mycorp.test2", "com.mycorp.test3"); + AutoConfigurationUtils.storeBasePackages(this.beanFactory, list1); + AutoConfigurationUtils.storeBasePackages(this.beanFactory, list2); + List actual = AutoConfigurationUtils.getBasePackages(this.beanFactory); + assertThat(actual, equalTo(Arrays.asList("com.mycorp.test1", "com.mycorp.test2", + "com.mycorp.test3"))); + } + + @Test + public void excludedPackages() throws Exception { + List packageList = Arrays.asList("com.mycorp.test1", + "org.springframework.data.rest.webmvc"); + AutoConfigurationUtils.storeBasePackages(this.beanFactory, packageList); + List actual = AutoConfigurationUtils.getBasePackages(this.beanFactory); + assertThat(actual, equalTo(Arrays.asList("com.mycorp.test1"))); + } +}