Merge branch '1.5.x'
This commit is contained in:
commit
1d1714ee8b
|
@ -87,28 +87,4 @@ public @interface ConfigurationProperties {
|
||||||
*/
|
*/
|
||||||
boolean exceptionIfInvalid() default true;
|
boolean exceptionIfInvalid() default true;
|
||||||
|
|
||||||
/**
|
|
||||||
* Optionally provide explicit resource locations to bind to. By default the
|
|
||||||
* configuration at these specified locations will be merged with the default
|
|
||||||
* configuration. These resources take precedence over any other property sources
|
|
||||||
* defined in the environment.
|
|
||||||
* @return the path (or paths) of resources to bind to
|
|
||||||
* @see #merge()
|
|
||||||
* @deprecated as of 1.4 in favor of configuring the environment directly with
|
|
||||||
* additional locations
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
String[] locations() default {};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flag to indicate that configuration loaded from the specified locations should be
|
|
||||||
* merged with the default configuration.
|
|
||||||
* @return the flag value (default true)
|
|
||||||
* @see #locations()
|
|
||||||
* @deprecated as of 1.4 along with {@link #locations()} in favor of configuring the
|
|
||||||
* environment directly with additional locations
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
boolean merge() default true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.boot.context.properties;
|
package org.springframework.boot.context.properties;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
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.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
import org.springframework.boot.bind.PropertiesConfigurationFactory;
|
import org.springframework.boot.bind.PropertiesConfigurationFactory;
|
||||||
import org.springframework.boot.env.PropertySourcesLoader;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.EnvironmentAware;
|
import org.springframework.context.EnvironmentAware;
|
||||||
import org.springframework.context.ResourceLoaderAware;
|
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||||
import org.springframework.core.Ordered;
|
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.PropertySource;
|
||||||
import org.springframework.core.env.PropertySources;
|
import org.springframework.core.env.PropertySources;
|
||||||
import org.springframework.core.env.StandardEnvironment;
|
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.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
@ -77,10 +71,9 @@ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
* @author Christian Dupuis
|
* @author Christian Dupuis
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class ConfigurationPropertiesBindingPostProcessor
|
public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProcessor,
|
||||||
implements BeanPostProcessor, BeanFactoryAware, ResourceLoaderAware,
|
BeanFactoryAware, EnvironmentAware, ApplicationContextAware, InitializingBean,
|
||||||
EnvironmentAware, ApplicationContextAware, InitializingBean, DisposableBean,
|
DisposableBean, ApplicationListener<ContextRefreshedEvent>, PriorityOrdered {
|
||||||
ApplicationListener<ContextRefreshedEvent>, PriorityOrdered {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The bean name of the configuration properties validator.
|
* The bean name of the configuration properties validator.
|
||||||
|
@ -107,8 +100,6 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
|
|
||||||
private BeanFactory beanFactory;
|
private BeanFactory beanFactory;
|
||||||
|
|
||||||
private ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
||||||
|
|
||||||
private Environment environment = new StandardEnvironment();
|
private Environment environment = new StandardEnvironment();
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
|
@ -195,11 +186,6 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
this.beanFactory = beanFactory;
|
this.beanFactory = beanFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
|
||||||
this.resourceLoader = resourceLoader;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEnvironment(Environment environment) {
|
public void setEnvironment(Environment environment) {
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
|
@ -315,19 +301,12 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
private void postProcessBeforeInitialization(Object bean, String beanName,
|
private void postProcessBeforeInitialization(Object bean, String beanName,
|
||||||
ConfigurationProperties annotation) {
|
ConfigurationProperties annotation) {
|
||||||
Object target = bean;
|
Object target = bean;
|
||||||
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
|
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
|
||||||
target);
|
target);
|
||||||
if (annotation != null && annotation.locations().length != 0) {
|
factory.setPropertySources(this.propertySources);
|
||||||
factory.setPropertySources(
|
|
||||||
loadPropertySources(annotation.locations(), annotation.merge()));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
factory.setPropertySources(this.propertySources);
|
|
||||||
}
|
|
||||||
factory.setValidator(determineValidator(bean));
|
factory.setValidator(determineValidator(bean));
|
||||||
// If no explicit conversion service is provided we add one so that (at least)
|
// If no explicit conversion service is provided we add one so that (at least)
|
||||||
// comma-separated arrays of convertibles can be bound automatically
|
// comma-separated arrays of convertibles can be bound automatically
|
||||||
|
@ -398,33 +377,6 @@ public class ConfigurationPropertiesBindingPostProcessor
|
||||||
return true;
|
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() {
|
private ConversionService getDefaultConversionService() {
|
||||||
if (this.defaultConversionService == null) {
|
if (this.defaultConversionService == null) {
|
||||||
DefaultConversionService conversionService = new DefaultConversionService();
|
DefaultConversionService conversionService = new DefaultConversionService();
|
||||||
|
|
|
@ -219,29 +219,6 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||||
.isEqualTo("foo");
|
.isEqualTo("foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void placeholderResolutionWithCustomLocation() throws Exception {
|
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
|
||||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
|
|
||||||
"fooValue=bar");
|
|
||||||
this.context.register(CustomConfigurationLocation.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBean(CustomConfigurationLocation.class).getFoo())
|
|
||||||
.isEqualTo("bar");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void placeholderResolutionWithUnmergedCustomLocation() throws Exception {
|
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
|
||||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
|
|
||||||
"fooValue:bar");
|
|
||||||
this.context.register(UnmergedCustomConfigurationLocation.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(
|
|
||||||
this.context.getBean(UnmergedCustomConfigurationLocation.class).getFoo())
|
|
||||||
.isEqualTo("${fooValue}");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void configurationPropertiesWithFactoryBean() throws Exception {
|
public void configurationPropertiesWithFactoryBean() throws Exception {
|
||||||
ConfigurationPropertiesWithFactoryBean.factoryBeanInit = false;
|
ConfigurationPropertiesWithFactoryBean.factoryBeanInit = false;
|
||||||
|
@ -626,38 +603,6 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableConfigurationProperties
|
|
||||||
@ConfigurationProperties(locations = "custom-location.yml")
|
|
||||||
public static class CustomConfigurationLocation {
|
|
||||||
|
|
||||||
private String foo;
|
|
||||||
|
|
||||||
public String getFoo() {
|
|
||||||
return this.foo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFoo(String foo) {
|
|
||||||
this.foo = foo;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@EnableConfigurationProperties
|
|
||||||
@ConfigurationProperties(locations = "custom-location.yml", merge = false)
|
|
||||||
public static class UnmergedCustomConfigurationLocation {
|
|
||||||
|
|
||||||
private String foo;
|
|
||||||
|
|
||||||
public String getFoo() {
|
|
||||||
return this.foo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFoo(String foo) {
|
|
||||||
this.foo = foo;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableConfigurationProperties
|
@EnableConfigurationProperties
|
||||||
public static class ConfigurationPropertiesWithFactoryBean {
|
public static class ConfigurationPropertiesWithFactoryBean {
|
||||||
|
|
|
@ -278,61 +278,6 @@ public class EnableConfigurationPropertiesTests {
|
||||||
assertThat(this.context.getBean(TestProperties.class).name).isEqualTo("bar");
|
assertThat(this.context.getBean(TestProperties.class).name).isEqualTo("bar");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBindingDirectlyToFile() {
|
|
||||||
this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class))
|
|
||||||
.hasSize(1);
|
|
||||||
assertThat(this.context.getBean(ResourceBindingProperties.class).name)
|
|
||||||
.isEqualTo("foo");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBindingDirectlyToFileResolvedFromEnvironment() {
|
|
||||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
|
|
||||||
"binding.location=classpath:other.yml");
|
|
||||||
this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class))
|
|
||||||
.hasSize(1);
|
|
||||||
assertThat(this.context.getBean(ResourceBindingProperties.class).name)
|
|
||||||
.isEqualTo("other");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBindingDirectlyToFileWithDefaultsWhenProfileNotFound() {
|
|
||||||
this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
|
|
||||||
this.context.getEnvironment().addActiveProfile("nonexistent");
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class))
|
|
||||||
.hasSize(1);
|
|
||||||
assertThat(this.context.getBean(ResourceBindingProperties.class).name)
|
|
||||||
.isEqualTo("foo");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBindingDirectlyToFileWithExplicitSpringProfile() {
|
|
||||||
this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
|
|
||||||
this.context.getEnvironment().addActiveProfile("super");
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class))
|
|
||||||
.hasSize(1);
|
|
||||||
assertThat(this.context.getBean(ResourceBindingProperties.class).name)
|
|
||||||
.isEqualTo("bar");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBindingDirectlyToFileWithTwoExplicitSpringProfiles() {
|
|
||||||
this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
|
|
||||||
this.context.getEnvironment().setActiveProfiles("super", "other");
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBeanNamesForType(ResourceBindingProperties.class))
|
|
||||||
.hasSize(1);
|
|
||||||
assertThat(this.context.getBean(ResourceBindingProperties.class).name)
|
|
||||||
.isEqualTo("spam");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindingWithTwoBeans() {
|
public void testBindingWithTwoBeans() {
|
||||||
this.context.register(MoreConfiguration.class, TestConfiguration.class);
|
this.context.register(MoreConfiguration.class, TestConfiguration.class);
|
||||||
|
@ -415,14 +360,13 @@ public class EnableConfigurationPropertiesTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindingWithMapKeyWithPeriod() {
|
public void testBindingWithMapKeyWithPeriod() {
|
||||||
|
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
|
||||||
|
"mymap.key1.key2:value12", "mymap.key3:value3");
|
||||||
this.context.register(ResourceBindingPropertiesWithMap.class);
|
this.context.register(ResourceBindingPropertiesWithMap.class);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
|
|
||||||
ResourceBindingPropertiesWithMap bean = this.context
|
ResourceBindingPropertiesWithMap bean = this.context
|
||||||
.getBean(ResourceBindingPropertiesWithMap.class);
|
.getBean(ResourceBindingPropertiesWithMap.class);
|
||||||
assertThat(bean.mymap.get("key3")).isEqualTo("value3");
|
assertThat(bean.mymap.get("key3")).isEqualTo("value3");
|
||||||
// this should not fail!!!
|
|
||||||
// mymap looks to contain - {key1=, key3=value3}
|
|
||||||
assertThat(bean.mymap.get("key1.key2")).isEqualTo("value12");
|
assertThat(bean.mymap.get("key1.key2")).isEqualTo("value12");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -772,20 +716,8 @@ public class EnableConfigurationPropertiesTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConfigurationProperties(locations = "${binding.location:classpath:name.yml}")
|
|
||||||
protected static class ResourceBindingProperties {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No getter - you should be able to bind to a write-only bean
|
|
||||||
}
|
|
||||||
|
|
||||||
@EnableConfigurationProperties
|
@EnableConfigurationProperties
|
||||||
@ConfigurationProperties(locations = "${binding.location:classpath:map.yml}")
|
@ConfigurationProperties
|
||||||
protected static class ResourceBindingPropertiesWithMap {
|
protected static class ResourceBindingPropertiesWithMap {
|
||||||
|
|
||||||
private Map<String, String> mymap;
|
private Map<String, String> mymap;
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
mymap:
|
|
||||||
? key1.key2
|
|
||||||
: value12
|
|
||||||
? key3
|
|
||||||
: value3
|
|
|
@ -1,10 +0,0 @@
|
||||||
---
|
|
||||||
name: foo
|
|
||||||
|
|
||||||
---
|
|
||||||
spring.profiles: super
|
|
||||||
name: bar
|
|
||||||
|
|
||||||
---
|
|
||||||
spring.profiles: other
|
|
||||||
name: spam
|
|
Loading…
Reference in New Issue