|
|
|
@ -16,7 +16,6 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.context.properties;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -36,13 +35,11 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
|
import org.springframework.boot.bind.PropertiesConfigurationFactory;
|
|
|
|
|
import org.springframework.boot.env.PropertySourcesLoader;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
import org.springframework.context.ApplicationListener;
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
|
import org.springframework.context.EnvironmentAware;
|
|
|
|
|
import org.springframework.context.ResourceLoaderAware;
|
|
|
|
|
import org.springframework.context.event.ContextRefreshedEvent;
|
|
|
|
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
@ -58,9 +55,6 @@ import org.springframework.core.env.MutablePropertySources;
|
|
|
|
|
import org.springframework.core.env.PropertySource;
|
|
|
|
|
import org.springframework.core.env.PropertySources;
|
|
|
|
|
import org.springframework.core.env.StandardEnvironment;
|
|
|
|
|
import org.springframework.core.io.DefaultResourceLoader;
|
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
|
import org.springframework.core.io.ResourceLoader;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
@ -77,10 +71,9 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
|
|
|
|
* @author Christian Dupuis
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
*/
|
|
|
|
|
public class ConfigurationPropertiesBindingPostProcessor
|
|
|
|
|
implements BeanPostProcessor, BeanFactoryAware, ResourceLoaderAware,
|
|
|
|
|
EnvironmentAware, ApplicationContextAware, InitializingBean, DisposableBean,
|
|
|
|
|
ApplicationListener<ContextRefreshedEvent>, PriorityOrdered {
|
|
|
|
|
public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProcessor,
|
|
|
|
|
BeanFactoryAware, EnvironmentAware, ApplicationContextAware, InitializingBean,
|
|
|
|
|
DisposableBean, ApplicationListener<ContextRefreshedEvent>, PriorityOrdered {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The bean name of the configuration properties validator.
|
|
|
|
@ -107,8 +100,6 @@ public class ConfigurationPropertiesBindingPostProcessor
|
|
|
|
|
|
|
|
|
|
private BeanFactory beanFactory;
|
|
|
|
|
|
|
|
|
|
private ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
|
|
|
|
|
|
|
|
private Environment environment = new StandardEnvironment();
|
|
|
|
|
|
|
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
@ -195,11 +186,6 @@ public class ConfigurationPropertiesBindingPostProcessor
|
|
|
|
|
this.beanFactory = beanFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setResourceLoader(ResourceLoader resourceLoader) {
|
|
|
|
|
this.resourceLoader = resourceLoader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setEnvironment(Environment environment) {
|
|
|
|
|
this.environment = environment;
|
|
|
|
@ -315,19 +301,12 @@ public class ConfigurationPropertiesBindingPostProcessor
|
|
|
|
|
return bean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
|
private void postProcessBeforeInitialization(Object bean, String beanName,
|
|
|
|
|
ConfigurationProperties annotation) {
|
|
|
|
|
Object target = bean;
|
|
|
|
|
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
|
|
|
|
|
target);
|
|
|
|
|
if (annotation != null && annotation.locations().length != 0) {
|
|
|
|
|
factory.setPropertySources(
|
|
|
|
|
loadPropertySources(annotation.locations(), annotation.merge()));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
factory.setPropertySources(this.propertySources);
|
|
|
|
|
}
|
|
|
|
|
factory.setPropertySources(this.propertySources);
|
|
|
|
|
factory.setValidator(determineValidator(bean));
|
|
|
|
|
// If no explicit conversion service is provided we add one so that (at least)
|
|
|
|
|
// comma-separated arrays of convertibles can be bound automatically
|
|
|
|
@ -398,33 +377,6 @@ public class ConfigurationPropertiesBindingPostProcessor
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PropertySources loadPropertySources(String[] locations,
|
|
|
|
|
boolean mergeDefaultSources) {
|
|
|
|
|
try {
|
|
|
|
|
PropertySourcesLoader loader = new PropertySourcesLoader();
|
|
|
|
|
for (String location : locations) {
|
|
|
|
|
Resource resource = this.resourceLoader
|
|
|
|
|
.getResource(this.environment.resolvePlaceholders(location));
|
|
|
|
|
String[] profiles = this.environment.getActiveProfiles();
|
|
|
|
|
for (int i = profiles.length; i-- > 0;) {
|
|
|
|
|
String profile = profiles[i];
|
|
|
|
|
loader.load(resource, profile);
|
|
|
|
|
}
|
|
|
|
|
loader.load(resource);
|
|
|
|
|
}
|
|
|
|
|
MutablePropertySources loaded = loader.getPropertySources();
|
|
|
|
|
if (mergeDefaultSources) {
|
|
|
|
|
for (PropertySource<?> propertySource : this.propertySources) {
|
|
|
|
|
loaded.addLast(propertySource);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return loaded;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex) {
|
|
|
|
|
throw new IllegalStateException(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ConversionService getDefaultConversionService() {
|
|
|
|
|
if (this.defaultConversionService == null) {
|
|
|
|
|
DefaultConversionService conversionService = new DefaultConversionService();
|
|
|
|
|