Exclude spring-data-rest package from being stored
Update AutoConfigurationUtils to specifically exclude spring-data-rest packages from being stored. This prevent missing class errors caused when Spring Boot attempts to use @ComponentScan packages as the source for JPA entities and reads annotations that are not on the classpath. Issue: #55489346
This commit is contained in:
parent
fc841e0269
commit
021fe45d83
|
|
@ -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<String> EXCLUDED_PACKAGES;
|
||||
static {
|
||||
Set<String> exclude = new HashSet<String>();
|
||||
exclude.add("org.springframework.data.rest.webmvc");
|
||||
EXCLUDED_PACKAGES = Collections.unmodifiableSet(exclude);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<String> getBasePackages(BeanFactory beanFactory) {
|
||||
try {
|
||||
|
|
@ -49,17 +58,14 @@ public abstract class AutoConfigurationUtils {
|
|||
public static void storeBasePackages(ConfigurableListableBeanFactory beanFactory,
|
||||
List<String> basePackages) {
|
||||
if (!beanFactory.containsBean(BASE_PACKAGES_BEAN)) {
|
||||
beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList<String>(
|
||||
basePackages));
|
||||
beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList<String>());
|
||||
}
|
||||
else {
|
||||
List<String> packages = getBasePackages(beanFactory);
|
||||
for (String pkg : basePackages) {
|
||||
if (packages.contains(pkg)) {
|
||||
packages.add(pkg);
|
||||
}
|
||||
List<String> storePackages = getBasePackages(beanFactory);
|
||||
for (String basePackage : basePackages) {
|
||||
if (!EXCLUDED_PACKAGES.contains(basePackage)
|
||||
&& !storePackages.contains(basePackage)) {
|
||||
storePackages.add(basePackage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String> packageList = Arrays.asList("com.mycorp.test1", "com.mycorp.test2");
|
||||
AutoConfigurationUtils.storeBasePackages(this.beanFactory, packageList);
|
||||
List<String> actual = AutoConfigurationUtils.getBasePackages(this.beanFactory);
|
||||
assertThat(actual, equalTo(packageList));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doubleAdd() throws Exception {
|
||||
List<String> list1 = Arrays.asList("com.mycorp.test1", "com.mycorp.test2");
|
||||
List<String> list2 = Arrays.asList("com.mycorp.test2", "com.mycorp.test3");
|
||||
AutoConfigurationUtils.storeBasePackages(this.beanFactory, list1);
|
||||
AutoConfigurationUtils.storeBasePackages(this.beanFactory, list2);
|
||||
List<String> 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<String> packageList = Arrays.asList("com.mycorp.test1",
|
||||
"org.springframework.data.rest.webmvc");
|
||||
AutoConfigurationUtils.storeBasePackages(this.beanFactory, packageList);
|
||||
List<String> actual = AutoConfigurationUtils.getBasePackages(this.beanFactory);
|
||||
assertThat(actual, equalTo(Arrays.asList("com.mycorp.test1")));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue