Improve configuration properties logging

Update ConfigurationPropertiesBindingPostProcessor with improved logging
when multiple PropertySourcesPlaceholderConfigurer beans are found.

See gh-6457
This commit is contained in:
Phillip Webb 2016-07-27 18:12:29 -07:00
parent 5d9836b3b1
commit d7e1ccc690
2 changed files with 42 additions and 0 deletions

View File

@ -22,6 +22,9 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
@ -87,6 +90,9 @@ public class ConfigurationPropertiesBindingPostProcessor
private static final String[] VALIDATOR_CLASSES = { "javax.validation.Validator",
"javax.validation.ValidatorFactory" };
private static final Log logger = LogFactory
.getLog(ConfigurationPropertiesBindingPostProcessor.class);
private ConfigurationBeanFactoryMetaData beans = new ConfigurationBeanFactoryMetaData();
private PropertySources propertySources;
@ -254,6 +260,8 @@ public class ConfigurationPropertiesBindingPostProcessor
return new FlatPropertySources(propertySources);
}
// empty, so not very useful, but fulfils the contract
logger.warn("Unable to obtain PropertySources from "
+ "PropertySourcesPlaceholderConfigurer or Environment");
return new MutablePropertySources();
}
@ -267,6 +275,11 @@ public class ConfigurationPropertiesBindingPostProcessor
if (beans.size() == 1) {
return beans.values().iterator().next();
}
if (beans.size() > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple PropertySourcesPlaceholderConfigurer "
+ "beans registered " + beans.keySet()
+ ", falling back to Environment");
}
}
return null;
}

View File

@ -34,6 +34,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.bind.RelaxedBindingNotWritablePropertyException;
import org.springframework.boot.testutil.InternalOutputCapture;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -61,6 +62,9 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public InternalOutputCapture output = new InternalOutputCapture();
private AnnotationConfigApplicationContext context;
@After
@ -336,6 +340,15 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
this.context.refresh();
}
@Test
public void multiplePropertySourcesPlaceholderConfigurer() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MultiplePropertySourcesPlaceholderConfigurer.class);
this.context.refresh();
assertThat(this.output.toString()).contains(
"Multiple PropertySourcesPlaceholderConfigurer beans registered");
}
private void assertBindingFailure(int errorCount) {
try {
this.context.refresh();
@ -732,6 +745,22 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
}
@Configuration
@EnableConfigurationProperties
public static class MultiplePropertySourcesPlaceholderConfigurer {
@Bean
public static PropertySourcesPlaceholderConfigurer configurer1() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public static PropertySourcesPlaceholderConfigurer configurer2() {
return new PropertySourcesPlaceholderConfigurer();
}
}
public static class PropertyWithoutConfigurationPropertiesAnnotation {
private String name;