Always store @ComponentScan details

Refactor JpaComponentScanDetector to a more general use utility and
ensure that details are always stored.
This commit is contained in:
Phillip Webb 2013-06-12 12:31:09 -07:00
parent b572d98cbf
commit 2f84df66b6
3 changed files with 98 additions and 5 deletions

View File

@ -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<String> 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<String> basePackages) {
if (!beanFactory.containsBean(BASE_PACKAGES_BEAN)) {
beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList<String>(
basePackages));
} else {
List<String> packages = getBasePackages(beanFactory);
for (String pkg : basePackages) {
if (packages.contains(pkg)) {
packages.add(pkg);
}
}
}
}
}

View File

@ -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());

View File

@ -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 {
}