Skip to content

Commit c11d941

Browse files
author
Phillip Webb
committed
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
1 parent b8858bd commit c11d941

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationPackages.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public static List<String> get(BeanFactory beanFactory) {
5656
// Currently we only store a single base package, but we return a list to
5757
// allow this to change in the future if needed
5858
try {
59-
return Collections.singletonList(beanFactory.getBean(BEAN, BasePackage.class)
60-
.toString());
59+
return beanFactory.getBean(BEAN, BasePackages.class).get();
6160
}
6261
catch (NoSuchBeanDefinitionException ex) {
6362
throw new IllegalStateException(
@@ -67,7 +66,7 @@ public static List<String> get(BeanFactory beanFactory) {
6766

6867
static void set(BeanDefinitionRegistry registry, String packageName) {
6968
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
70-
beanDefinition.setBeanClass(BasePackage.class);
69+
beanDefinition.setBeanClass(BasePackages.class);
7170
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0,
7271
packageName);
7372
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
@@ -81,40 +80,50 @@ static void set(BeanDefinitionRegistry registry, String packageName) {
8180
@Order(Ordered.HIGHEST_PRECEDENCE)
8281
static class Registrar implements ImportBeanDefinitionRegistrar {
8382

84-
private static final String NO_SUCH_PACKAGE = "not.scanning.root";
85-
8683
@Override
87-
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
84+
public void registerBeanDefinitions(AnnotationMetadata metadata,
8885
BeanDefinitionRegistry registry) {
89-
String packageName = ClassUtils.getPackageName(importingClassMetadata
90-
.getClassName());
91-
if (StringUtils.hasText(packageName)) {
92-
set(registry, packageName);
93-
logger.info("@EnableAutoConfiguration was declared on a class in the package '"
94-
+ packageName + "'. Automatic @Repository scanning is enabled.");
95-
}
96-
else {
97-
set(registry, NO_SUCH_PACKAGE);
98-
logger.warn("@EnableAutoConfiguration was declared on a class in the default package. "
99-
+ "Automatic @Repository scanning is not enabled.");
100-
}
86+
set(registry, ClassUtils.getPackageName(metadata.getClassName()));
10187
}
88+
10289
}
10390

10491
/**
105-
* Holder for the base package.
92+
* Holder for the base package (name may be null to indicate no scanning).
10693
*/
107-
final static class BasePackage {
94+
final static class BasePackages {
10895

109-
private final String name;
96+
private final List<String> packages;
11097

111-
public BasePackage(String name) {
112-
this.name = name;
98+
private boolean loggedBasePackageInfo;
99+
100+
public BasePackages(String name) {
101+
this.packages = (StringUtils.hasText(name) ? Collections.singletonList(name)
102+
: Collections.<String> emptyList());
113103
}
114104

115-
@Override
116-
public String toString() {
117-
return this.name;
105+
public List<String> get() {
106+
if (!this.loggedBasePackageInfo) {
107+
if (this.packages.isEmpty()) {
108+
if (logger.isWarnEnabled()) {
109+
logger.warn("@EnableAutoConfiguration was declared on a class "
110+
+ "in the default package. Automatic @Repository and "
111+
+ "@Entity scanning is not enabled.");
112+
}
113+
}
114+
else {
115+
if (logger.isDebugEnabled()) {
116+
String packageNames = StringUtils
117+
.collectionToCommaDelimitedString(this.packages);
118+
logger.debug("@EnableAutoConfiguration was declared on a class "
119+
+ "in the package '" + packageNames
120+
+ "'. Automatic @Repository and @Entity scanning is "
121+
+ "enabled.");
122+
}
123+
}
124+
this.loggedBasePackageInfo = true;
125+
}
126+
return this.packages;
118127
}
119128

120129
}

0 commit comments

Comments
 (0)