|
|
@ -1,5 +1,5 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright 2012-2019 the original author or authors.
|
|
|
|
* Copyright 2012-2020 the original author or authors.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@ -16,12 +16,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.autoconfigure.context;
|
|
|
|
package org.springframework.boot.autoconfigure.context;
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.boot.test.util.TestPropertyValues;
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
|
|
|
|
|
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
|
|
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
|
|
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
|
|
@ -33,43 +35,75 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
* Tests for {@link PropertyPlaceholderAutoConfiguration}.
|
|
|
|
* Tests for {@link PropertyPlaceholderAutoConfiguration}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Dave Syer
|
|
|
|
* @author Dave Syer
|
|
|
|
|
|
|
|
* @author Andy Wilkinson
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class PropertyPlaceholderAutoConfigurationTests {
|
|
|
|
class PropertyPlaceholderAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
|
|
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
|
|
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
|
|
|
|
|
|
|
|
|
|
|
@AfterEach
|
|
|
|
@Test
|
|
|
|
void close() {
|
|
|
|
void whenTheAutoConfigurationIsNotUsedThenBeanDefinitionPlaceholdersAreNotResolved() {
|
|
|
|
if (this.context != null) {
|
|
|
|
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
|
|
|
|
this.context.close();
|
|
|
|
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("${fruit:apple}"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void whenTheAutoConfigurationIsUsedThenBeanDefinitionPlaceholdersAreResolved() {
|
|
|
|
|
|
|
|
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
|
|
|
|
|
|
|
|
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
|
|
|
|
|
|
|
|
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("banana"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void whenTheAutoConfigurationIsNotUsedThenValuePlaceholdersAreResolved() {
|
|
|
|
|
|
|
|
this.contextRunner.withPropertyValues("fruit:banana").withUserConfiguration(PlaceholderConfig.class)
|
|
|
|
|
|
|
|
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("banana"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
void propertyPlaceholders() {
|
|
|
|
void whenTheAutoConfigurationIsUsedThenValuePlaceholdersAreResolved() {
|
|
|
|
this.context.register(PropertyPlaceholderAutoConfiguration.class, PlaceholderConfig.class);
|
|
|
|
this.contextRunner.withPropertyValues("fruit:banana")
|
|
|
|
TestPropertyValues.of("foo:two").applyTo(this.context);
|
|
|
|
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
|
|
|
|
this.context.refresh();
|
|
|
|
.withUserConfiguration(PlaceholderConfig.class)
|
|
|
|
assertThat(this.context.getBean(PlaceholderConfig.class).getFoo()).isEqualTo("two");
|
|
|
|
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("banana"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
void propertyPlaceholdersOverride() {
|
|
|
|
void whenThereIsAUserDefinedPropertySourcesPlaceholderConfigurerThenItIsUsedForBeanDefinitionPlaceholderResolution() {
|
|
|
|
this.context.register(PropertyPlaceholderAutoConfiguration.class, PlaceholderConfig.class,
|
|
|
|
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
|
|
|
|
PlaceholdersOverride.class);
|
|
|
|
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
|
|
|
|
TestPropertyValues.of("foo:two").applyTo(this.context);
|
|
|
|
.withUserConfiguration(PlaceholdersOverride.class)
|
|
|
|
this.context.refresh();
|
|
|
|
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("orange"));
|
|
|
|
assertThat(this.context.getBean(PlaceholderConfig.class).getFoo()).isEqualTo("spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void whenThereIsAUserDefinedPropertySourcesPlaceholderConfigurerThenItIsUsedForValuePlaceholderResolution() {
|
|
|
|
|
|
|
|
this.contextRunner.withPropertyValues("fruit:banana")
|
|
|
|
|
|
|
|
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
|
|
|
|
|
|
|
|
.withUserConfiguration(PlaceholderConfig.class, PlaceholdersOverride.class)
|
|
|
|
|
|
|
|
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("orange"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void definePlaceholderBean(ConfigurableApplicationContext context) {
|
|
|
|
|
|
|
|
((BeanDefinitionRegistry) context.getBeanFactory()).registerBeanDefinition("placeholderBean",
|
|
|
|
|
|
|
|
BeanDefinitionBuilder.genericBeanDefinition(PlaceholderBean.class)
|
|
|
|
|
|
|
|
.addConstructorArgValue("${fruit:apple}").getBeanDefinition());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
static class PlaceholderConfig {
|
|
|
|
static class PlaceholderConfig {
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${foo:bar}")
|
|
|
|
@Value("${fruit:apple}")
|
|
|
|
private String foo;
|
|
|
|
private String fruit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class PlaceholderBean {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String fruit;
|
|
|
|
|
|
|
|
|
|
|
|
String getFoo() {
|
|
|
|
PlaceholderBean(String fruit) {
|
|
|
|
return this.foo;
|
|
|
|
this.fruit = fruit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -80,7 +114,8 @@ class PropertyPlaceholderAutoConfigurationTests {
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
static PropertySourcesPlaceholderConfigurer morePlaceholders() {
|
|
|
|
static PropertySourcesPlaceholderConfigurer morePlaceholders() {
|
|
|
|
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
|
|
|
|
configurer.setProperties(StringUtils.splitArrayElementsIntoProperties(new String[] { "foo=spam" }, "="));
|
|
|
|
configurer
|
|
|
|
|
|
|
|
.setProperties(StringUtils.splitArrayElementsIntoProperties(new String[] { "fruit=orange" }, "="));
|
|
|
|
configurer.setLocalOverride(true);
|
|
|
|
configurer.setLocalOverride(true);
|
|
|
|
configurer.setOrder(0);
|
|
|
|
configurer.setOrder(0);
|
|
|
|
return configurer;
|
|
|
|
return configurer;
|
|
|
|