Log AutoConfigurationPackages warnings just once

Update AutoConfigurationPackages to log warnings on the first access,
rather than during setup. This works around the fact that the CLI
currently add multiple @EnableAutoConfiguration annotations.

Fixes gh-579
This commit is contained in:
Phillip Webb 2014-03-25 14:21:14 -07:00
parent b8858bdb8f
commit c11d94105a

View File

@ -56,8 +56,7 @@ public abstract class AutoConfigurationPackages {
// Currently we only store a single base package, but we return a list to // Currently we only store a single base package, but we return a list to
// allow this to change in the future if needed // allow this to change in the future if needed
try { try {
return Collections.singletonList(beanFactory.getBean(BEAN, BasePackage.class) return beanFactory.getBean(BEAN, BasePackages.class).get();
.toString());
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ex) {
throw new IllegalStateException( throw new IllegalStateException(
@ -67,7 +66,7 @@ public abstract class AutoConfigurationPackages {
static void set(BeanDefinitionRegistry registry, String packageName) { static void set(BeanDefinitionRegistry registry, String packageName) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(BasePackage.class); beanDefinition.setBeanClass(BasePackages.class);
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0,
packageName); packageName);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
@ -81,40 +80,50 @@ public abstract class AutoConfigurationPackages {
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
static class Registrar implements ImportBeanDefinitionRegistrar { static class Registrar implements ImportBeanDefinitionRegistrar {
private static final String NO_SUCH_PACKAGE = "not.scanning.root";
@Override @Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) { BeanDefinitionRegistry registry) {
String packageName = ClassUtils.getPackageName(importingClassMetadata set(registry, ClassUtils.getPackageName(metadata.getClassName()));
.getClassName());
if (StringUtils.hasText(packageName)) {
set(registry, packageName);
logger.info("@EnableAutoConfiguration was declared on a class in the package '"
+ packageName + "'. Automatic @Repository scanning is enabled.");
}
else {
set(registry, NO_SUCH_PACKAGE);
logger.warn("@EnableAutoConfiguration was declared on a class in the default package. "
+ "Automatic @Repository scanning is not enabled.");
}
} }
} }
/** /**
* Holder for the base package. * Holder for the base package (name may be null to indicate no scanning).
*/ */
final static class BasePackage { final static class BasePackages {
private final String name; private final List<String> packages;
public BasePackage(String name) { private boolean loggedBasePackageInfo;
this.name = name;
public BasePackages(String name) {
this.packages = (StringUtils.hasText(name) ? Collections.singletonList(name)
: Collections.<String> emptyList());
} }
@Override public List<String> get() {
public String toString() { if (!this.loggedBasePackageInfo) {
return this.name; if (this.packages.isEmpty()) {
if (logger.isWarnEnabled()) {
logger.warn("@EnableAutoConfiguration was declared on a class "
+ "in the default package. Automatic @Repository and "
+ "@Entity scanning is not enabled.");
}
}
else {
if (logger.isDebugEnabled()) {
String packageNames = StringUtils
.collectionToCommaDelimitedString(this.packages);
logger.debug("@EnableAutoConfiguration was declared on a class "
+ "in the package '" + packageNames
+ "'. Automatic @Repository and @Entity scanning is "
+ "enabled.");
}
}
this.loggedBasePackageInfo = true;
}
return this.packages;
} }
} }