Don't use @ComponentScan for default packages
Update the 'default' package used for @Entity and Repository scanning to be the package of the class annotated with @EnableAutoConfiguration rather than using @ComponentScan. This allows JPA and Spring Data to be used without requiring component scanning and also removes the confusion that could arise from reusing the @ComponentScan annotation, which already has well defined semantics. Fixes gh-200pull/234/head
parent
6c255e5785
commit
f103a976f8
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Class for storing auto-configuration packages for reference later (e.g. by JPA entity
|
||||
* scanner).
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public abstract class AutoConfigurationPackages {
|
||||
|
||||
private static final String BEAN = AutoConfigurationPackages.class.getName();
|
||||
|
||||
/**
|
||||
* Return the auto-configuration base packages for the given bean factory
|
||||
* @param beanFactory the source bean factory
|
||||
* @return a list of auto-configuration packages
|
||||
* @throws IllegalStateException if auto-configuration is not enabled
|
||||
*/
|
||||
public static List<String> get(BeanFactory beanFactory) {
|
||||
// Currently we only store a single base package, but we return a list to
|
||||
// allow this to change in the future if needed
|
||||
try {
|
||||
return Collections.singletonList(beanFactory.getBean(BEAN, BasePackage.class)
|
||||
.toString());
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
throw new IllegalStateException(
|
||||
"Unable to retrieve @EnableAutoConfiguration base packages");
|
||||
}
|
||||
}
|
||||
|
||||
static void set(BeanDefinitionRegistry registry, String packageName) {
|
||||
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
|
||||
beanDefinition.setBeanClass(BasePackage.class);
|
||||
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0,
|
||||
packageName);
|
||||
registry.registerBeanDefinition(BEAN, beanDefinition);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link ImportBeanDefinitionRegistrar} to store the base package from the importing
|
||||
* configuration.
|
||||
*/
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
static class Registrar implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
||||
BeanDefinitionRegistry registry) {
|
||||
set(registry,
|
||||
ClassUtils.getPackageName(importingClassMetadata.getClassName()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Holder for the base package.
|
||||
*/
|
||||
final static class BasePackage {
|
||||
|
||||
private final String name;
|
||||
|
||||
public BasePackage(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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.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;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
|
||||
/**
|
||||
* Convenience class for storing base packages during component scan, for reference later
|
||||
* (e.g. by JPA entity scanner).
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
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 {
|
||||
return beanFactory.getBean(BASE_PACKAGES_BEAN, List.class);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public static void storeBasePackages(ConfigurableListableBeanFactory beanFactory,
|
||||
List<String> basePackages) {
|
||||
if (!beanFactory.containsBean(BASE_PACKAGES_BEAN)) {
|
||||
beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList<String>());
|
||||
}
|
||||
List<String> storePackages = getBasePackages(beanFactory);
|
||||
for (String basePackage : basePackages) {
|
||||
if (!EXCLUDED_PACKAGES.contains(basePackage)
|
||||
&& !storePackages.contains(basePackage)) {
|
||||
storePackages.add(basePackage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,156 +0,0 @@
|
||||
/*
|
||||
* 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.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Helper to detect a component scan declared in the enclosing context (normally on a
|
||||
* {@code @Configuration} class). Once the component scan is detected, the base packages
|
||||
* are stored for retrieval later.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
* @see AutoConfigurationUtils
|
||||
*/
|
||||
public class ComponentScanDetector implements ImportBeanDefinitionRegistrar,
|
||||
BeanFactoryAware {
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
||||
final BeanDefinitionRegistry registry) {
|
||||
storeComponentScanBasePackages();
|
||||
}
|
||||
|
||||
private void storeComponentScanBasePackages() {
|
||||
if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
|
||||
storeComponentScanBasePackages((ConfigurableListableBeanFactory) this.beanFactory);
|
||||
}
|
||||
else {
|
||||
if (this.logger.isWarnEnabled()) {
|
||||
this.logger
|
||||
.warn("Unable to read @ComponentScan annotations for auto-configure");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void storeComponentScanBasePackages(
|
||||
ConfigurableListableBeanFactory beanFactory) {
|
||||
List<String> basePackages = new ArrayList<String>();
|
||||
for (String beanName : beanFactory.getBeanDefinitionNames()) {
|
||||
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
|
||||
String[] basePackagesAttribute = (String[]) beanDefinition
|
||||
.getAttribute("componentScanBasePackages");
|
||||
if (basePackagesAttribute != null) {
|
||||
basePackages.addAll(Arrays.asList(basePackagesAttribute));
|
||||
}
|
||||
AnnotationMetadata metadata = getMetadata(beanDefinition);
|
||||
basePackages.addAll(getBasePackages(metadata));
|
||||
}
|
||||
AutoConfigurationUtils.storeBasePackages(beanFactory, basePackages);
|
||||
}
|
||||
|
||||
private AnnotationMetadata getMetadata(BeanDefinition beanDefinition) {
|
||||
if (beanDefinition instanceof AbstractBeanDefinition
|
||||
&& ((AbstractBeanDefinition) beanDefinition).hasBeanClass()) {
|
||||
Class<?> beanClass = ((AbstractBeanDefinition) beanDefinition).getBeanClass();
|
||||
if (Enhancer.isEnhanced(beanClass)) {
|
||||
beanClass = beanClass.getSuperclass();
|
||||
}
|
||||
return new StandardAnnotationMetadata(beanClass, true);
|
||||
}
|
||||
String className = beanDefinition.getBeanClassName();
|
||||
if (className != null) {
|
||||
try {
|
||||
MetadataReader metadataReader = this.metadataReaderFactory
|
||||
.getMetadataReader(className);
|
||||
return metadataReader.getAnnotationMetadata();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug(
|
||||
"Could not find class file for introspecting @ComponentScan classes: "
|
||||
+ className, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<String> getBasePackages(AnnotationMetadata metadata) {
|
||||
AnnotationAttributes attributes = AnnotationAttributes
|
||||
.fromMap((metadata == null ? null : metadata.getAnnotationAttributes(
|
||||
ComponentScan.class.getName(), true)));
|
||||
if (attributes != null) {
|
||||
List<String> basePackages = new ArrayList<String>();
|
||||
addAllHavingText(basePackages, attributes.getStringArray("value"));
|
||||
addAllHavingText(basePackages, attributes.getStringArray("basePackages"));
|
||||
for (String packageClass : attributes.getStringArray("basePackageClasses")) {
|
||||
basePackages.add(ClassUtils.getPackageName(packageClass));
|
||||
}
|
||||
if (basePackages.isEmpty()) {
|
||||
basePackages.add(ClassUtils.getPackageName(metadata.getClassName()));
|
||||
}
|
||||
return basePackages;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private void addAllHavingText(List<String> list, String[] strings) {
|
||||
for (String s : strings) {
|
||||
if (StringUtils.hasText(s)) {
|
||||
list.add(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.Collections;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AutoConfigurationPackages}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public class AutoConfigurationPackagesTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void setAndGet() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
ConfigWithRegistrar.class);
|
||||
assertThat(AutoConfigurationPackages.get(context.getBeanFactory()),
|
||||
equalTo(Collections.singletonList(getClass().getPackage().getName())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWithoutSet() throws Exception {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
EmptyConfig.class);
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown
|
||||
.expectMessage("Unable to retrieve @EnableAutoConfiguration base packages");
|
||||
AutoConfigurationPackages.get(context.getBeanFactory());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(AutoConfigurationPackages.Registrar.class)
|
||||
static class ConfigWithRegistrar {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class EmptyConfig {
|
||||
}
|
||||
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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")));
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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 org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* {@link ImportBeanDefinitionRegistrar} to store the base package for tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class TestAutoConfigurationPackageRegistrar implements
|
||||
ImportBeanDefinitionRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata metadata,
|
||||
BeanDefinitionRegistry registry) {
|
||||
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
|
||||
.getAnnotationAttributes(TestAutoConfigurationPackage.class.getName(),
|
||||
true));
|
||||
AutoConfigurationPackages.set(registry,
|
||||
ClassUtils.getPackageName(attributes.getString("value")));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue