Improve configuration properties logging
Update ConfigurationPropertiesBindingPostProcessor with improved logging when multiple PropertySourcesPlaceholderConfigurer beans are found. See gh-6457
This commit is contained in:
parent
5d9836b3b1
commit
d7e1ccc690
|
|
@ -22,6 +22,9 @@ import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.BeansException;
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
|
@ -87,6 +90,9 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
private static final String[] VALIDATOR_CLASSES = { "javax.validation.Validator",
|
private static final String[] VALIDATOR_CLASSES = { "javax.validation.Validator",
|
||||||
"javax.validation.ValidatorFactory" };
|
"javax.validation.ValidatorFactory" };
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory
|
||||||
|
.getLog(ConfigurationPropertiesBindingPostProcessor.class);
|
||||||
|
|
||||||
private ConfigurationBeanFactoryMetaData beans = new ConfigurationBeanFactoryMetaData();
|
private ConfigurationBeanFactoryMetaData beans = new ConfigurationBeanFactoryMetaData();
|
||||||
|
|
||||||
private PropertySources propertySources;
|
private PropertySources propertySources;
|
||||||
|
|
@ -254,6 +260,8 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
return new FlatPropertySources(propertySources);
|
return new FlatPropertySources(propertySources);
|
||||||
}
|
}
|
||||||
// empty, so not very useful, but fulfils the contract
|
// empty, so not very useful, but fulfils the contract
|
||||||
|
logger.warn("Unable to obtain PropertySources from "
|
||||||
|
+ "PropertySourcesPlaceholderConfigurer or Environment");
|
||||||
return new MutablePropertySources();
|
return new MutablePropertySources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -267,6 +275,11 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
if (beans.size() == 1) {
|
if (beans.size() == 1) {
|
||||||
return beans.values().iterator().next();
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||||
import org.springframework.boot.bind.RelaxedBindingNotWritablePropertyException;
|
import org.springframework.boot.bind.RelaxedBindingNotWritablePropertyException;
|
||||||
|
import org.springframework.boot.testutil.InternalOutputCapture;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -61,6 +62,9 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public InternalOutputCapture output = new InternalOutputCapture();
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private AnnotationConfigApplicationContext context;
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
@ -336,6 +340,15 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||||
this.context.refresh();
|
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) {
|
private void assertBindingFailure(int errorCount) {
|
||||||
try {
|
try {
|
||||||
this.context.refresh();
|
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 {
|
public static class PropertyWithoutConfigurationPropertiesAnnotation {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue