Skip to content

Commit d7e1ccc

Browse files
committed
Improve configuration properties logging
Update ConfigurationPropertiesBindingPostProcessor with improved logging when multiple PropertySourcesPlaceholderConfigurer beans are found. See gh-6457
1 parent 5d9836b commit d7e1ccc

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
2528
import org.springframework.beans.BeansException;
2629
import org.springframework.beans.factory.BeanCreationException;
2730
import org.springframework.beans.factory.BeanFactory;
@@ -87,6 +90,9 @@ public class ConfigurationPropertiesBindingPostProcessor
8790
private static final String[] VALIDATOR_CLASSES = { "javax.validation.Validator",
8891
"javax.validation.ValidatorFactory" };
8992

93+
private static final Log logger = LogFactory
94+
.getLog(ConfigurationPropertiesBindingPostProcessor.class);
95+
9096
private ConfigurationBeanFactoryMetaData beans = new ConfigurationBeanFactoryMetaData();
9197

9298
private PropertySources propertySources;
@@ -254,6 +260,8 @@ private PropertySources deducePropertySources() {
254260
return new FlatPropertySources(propertySources);
255261
}
256262
// empty, so not very useful, but fulfils the contract
263+
logger.warn("Unable to obtain PropertySources from "
264+
+ "PropertySourcesPlaceholderConfigurer or Environment");
257265
return new MutablePropertySources();
258266
}
259267

@@ -267,6 +275,11 @@ private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholder
267275
if (beans.size() == 1) {
268276
return beans.values().iterator().next();
269277
}
278+
if (beans.size() > 1 && logger.isWarnEnabled()) {
279+
logger.warn("Multiple PropertySourcesPlaceholderConfigurer "
280+
+ "beans registered " + beans.keySet()
281+
+ ", falling back to Environment");
282+
}
270283
}
271284
return null;
272285
}

spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.beans.factory.support.AbstractBeanDefinition;
3535
import org.springframework.beans.factory.support.GenericBeanDefinition;
3636
import org.springframework.boot.bind.RelaxedBindingNotWritablePropertyException;
37+
import org.springframework.boot.testutil.InternalOutputCapture;
3738
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3839
import org.springframework.context.annotation.Bean;
3940
import org.springframework.context.annotation.Configuration;
@@ -61,6 +62,9 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
6162
@Rule
6263
public ExpectedException thrown = ExpectedException.none();
6364

65+
@Rule
66+
public InternalOutputCapture output = new InternalOutputCapture();
67+
6468
private AnnotationConfigApplicationContext context;
6569

6670
@After
@@ -336,6 +340,15 @@ public void bindWithoutConfigurationPropertiesAnnotation() {
336340
this.context.refresh();
337341
}
338342

343+
@Test
344+
public void multiplePropertySourcesPlaceholderConfigurer() throws Exception {
345+
this.context = new AnnotationConfigApplicationContext();
346+
this.context.register(MultiplePropertySourcesPlaceholderConfigurer.class);
347+
this.context.refresh();
348+
assertThat(this.output.toString()).contains(
349+
"Multiple PropertySourcesPlaceholderConfigurer beans registered");
350+
}
351+
339352
private void assertBindingFailure(int errorCount) {
340353
try {
341354
this.context.refresh();
@@ -732,6 +745,22 @@ public static class ConfigurationPropertiesWithoutAnnotation {
732745

733746
}
734747

748+
@Configuration
749+
@EnableConfigurationProperties
750+
public static class MultiplePropertySourcesPlaceholderConfigurer {
751+
752+
@Bean
753+
public static PropertySourcesPlaceholderConfigurer configurer1() {
754+
return new PropertySourcesPlaceholderConfigurer();
755+
}
756+
757+
@Bean
758+
public static PropertySourcesPlaceholderConfigurer configurer2() {
759+
return new PropertySourcesPlaceholderConfigurer();
760+
}
761+
762+
}
763+
735764
public static class PropertyWithoutConfigurationPropertiesAnnotation {
736765

737766
private String name;

0 commit comments

Comments
 (0)