From 2f84df66b66567b242be46578f09ca1743888e7b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 12 Jun 2013 12:31:09 -0700 Subject: [PATCH] Always store @ComponentScan details Refactor JpaComponentScanDetector to a more general use utility and ensure that details are always stored. --- .../autoconfigure/AutoConfigurationUtils.java | 63 +++++++++++++++++++ .../annotation/ComponentScanDetector.java} | 11 ++-- .../ComponentScanDetectorConfiguration.java | 29 +++++++++ 3 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/AutoConfigurationUtils.java rename spring-bootstrap/src/main/java/org/springframework/bootstrap/{autoconfigure/orm/jpa/JpaComponentScanDetector.java => context/annotation/ComponentScanDetector.java} (93%) create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/ComponentScanDetectorConfiguration.java diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/AutoConfigurationUtils.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/AutoConfigurationUtils.java new file mode 100644 index 0000000000..567fec748e --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/AutoConfigurationUtils.java @@ -0,0 +1,63 @@ +/* + * 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.bootstrap.autoconfigure; + +import java.util.ArrayList; +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.config.ConfigurableListableBeanFactory; + +/** + * Convenience class for storing base packages during component scan, for reference later + * (e.g. by JPA entity scanner). + * + * @author Phil Webb + * @author Dave Syer + */ +public abstract class AutoConfigurationUtils { + + private static String BASE_PACKAGES_BEAN = AutoConfigurationUtils.class.getName() + + ".basePackages"; + + @SuppressWarnings("unchecked") + public static List getBasePackages(BeanFactory beanFactory) { + try { + return beanFactory.getBean(BASE_PACKAGES_BEAN, List.class); + } catch (NoSuchBeanDefinitionException e) { + return Collections.emptyList(); + } + } + + public static void storeBasePackages(ConfigurableListableBeanFactory beanFactory, + List basePackages) { + if (!beanFactory.containsBean(BASE_PACKAGES_BEAN)) { + beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList( + basePackages)); + } else { + List packages = getBasePackages(beanFactory); + for (String pkg : basePackages) { + if (packages.contains(pkg)) { + packages.add(pkg); + } + } + } + } + +} diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/orm/jpa/JpaComponentScanDetector.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ComponentScanDetector.java similarity index 93% rename from spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/orm/jpa/JpaComponentScanDetector.java rename to spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ComponentScanDetector.java index fc4210b677..35eab37d5a 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/orm/jpa/JpaComponentScanDetector.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ComponentScanDetector.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.bootstrap.autoconfigure.orm.jpa; +package org.springframework.bootstrap.context.annotation; import java.io.IOException; import java.util.ArrayList; @@ -31,8 +31,7 @@ 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.bootstrap.autoconfigure.data.JpaRepositoriesAutoConfiguration; -import org.springframework.bootstrap.context.annotation.AutoConfigurationUtils; +import org.springframework.bootstrap.autoconfigure.AutoConfigurationUtils; import org.springframework.cglib.proxy.Enhancer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; @@ -48,11 +47,13 @@ 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 by the {@link JpaRepositoriesAutoConfiguration} . + * are stored for retrieval later. * * @author Dave Syer + * @author Phillip Webb + * @see AutoConfigurationUtils */ -class JpaComponentScanDetector implements ImportBeanDefinitionRegistrar, BeanFactoryAware { +class ComponentScanDetector implements ImportBeanDefinitionRegistrar, BeanFactoryAware { private final Log logger = LogFactory.getLog(getClass()); diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/ComponentScanDetectorConfiguration.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/ComponentScanDetectorConfiguration.java new file mode 100644 index 0000000000..b01272650b --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/ComponentScanDetectorConfiguration.java @@ -0,0 +1,29 @@ +/* + * 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.bootstrap.context.annotation; + +import org.springframework.context.annotation.Import; + +/** + * Simple configuration to import {@link ComponentScanDetector} for tests. + * + * @author Phillip Webb + */ +@Import(ComponentScanDetector.class) +public class ComponentScanDetectorConfiguration { + +}