mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Migrate config file initializer to a listener
This commit is contained in:
parent
3dacf4be17
commit
441572c61c
@ -22,7 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringJUnitTests.TestConfiguration;
|
||||
import org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer;
|
||||
import org.springframework.boot.context.listener.ConfigFileApplicationListener;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
@ -36,7 +36,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = TestConfiguration.class, initializers = ConfigFileApplicationContextInitializer.class)
|
||||
@ContextConfiguration(classes = TestConfiguration.class, initializers = ConfigFileApplicationListener.class)
|
||||
public class SpringJUnitTests {
|
||||
|
||||
@Autowired
|
||||
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.context.initializer;
|
||||
package org.springframework.boot.context.listener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -38,7 +38,6 @@ import org.springframework.boot.config.PropertySourceLoader;
|
||||
import org.springframework.boot.config.YamlPropertySourceLoader;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.PropertySources;
|
||||
import org.springframework.core.Ordered;
|
||||
@ -87,8 +86,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class ConfigFileApplicationContextInitializer implements
|
||||
ApplicationContextInitializer<ConfigurableApplicationContext>,
|
||||
public class ConfigFileApplicationListener implements
|
||||
ApplicationListener<SpringApplicationEnvironmentAvailableEvent>, Ordered {
|
||||
|
||||
private static final String LOCATION_VARIABLE = "${spring.config.location}";
|
||||
@ -140,11 +138,6 @@ public class ConfigFileApplicationContextInitializer implements
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
load(applicationContext.getEnvironment(), applicationContext);
|
||||
}
|
||||
|
||||
private void extractPropertySources(Set<Object> sources) {
|
||||
for (Object source : sources) {
|
||||
if (source instanceof Class) {
|
@ -1,6 +1,5 @@
|
||||
# Application Context Initializers
|
||||
org.springframework.context.ApplicationContextInitializer=\
|
||||
org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer,\
|
||||
org.springframework.boot.context.initializer.FileEncodingApplicationContextInitializer,\
|
||||
org.springframework.boot.context.initializer.ContextIdApplicationContextInitializer,\
|
||||
org.springframework.boot.context.initializer.EnvironmentDelegateApplicationContextInitializer,\
|
||||
@ -8,5 +7,6 @@ org.springframework.boot.context.initializer.VcapApplicationContextInitializer
|
||||
|
||||
# Application Listeners
|
||||
org.springframework.context.ApplicationListener=\
|
||||
org.springframework.boot.context.listener.ConfigFileApplicationListener,\
|
||||
org.springframework.boot.context.listener.LoggingApplicationListener,\
|
||||
org.springframework.boot.liquibase.LiquibaseServiceLocatorInitializer
|
||||
|
@ -176,7 +176,7 @@ public class SpringApplicationBuilderTests {
|
||||
SpringApplicationBuilder application = new SpringApplicationBuilder(
|
||||
ExampleConfig.class).web(false);
|
||||
this.context = application.run();
|
||||
assertEquals(5, application.application().getInitializers().size());
|
||||
assertEquals(4, application.application().getInitializers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -184,7 +184,7 @@ public class SpringApplicationBuilderTests {
|
||||
SpringApplicationBuilder application = new SpringApplicationBuilder(
|
||||
ExampleConfig.class).child(ChildConfig.class).web(false);
|
||||
this.context = application.run();
|
||||
assertEquals(6, application.application().getInitializers().size());
|
||||
assertEquals(5, application.application().getInitializers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -198,7 +198,7 @@ public class SpringApplicationBuilderTests {
|
||||
}
|
||||
});
|
||||
this.context = application.run();
|
||||
assertEquals(6, application.application().getInitializers().size());
|
||||
assertEquals(5, application.application().getInitializers().size());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.context.initializer;
|
||||
package org.springframework.boot.context.listener;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -23,11 +23,11 @@ import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringApplicationEnvironmentAvailableEvent;
|
||||
import org.springframework.boot.context.listener.ConfigFileApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.SimpleCommandLinePropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
@ -40,16 +40,19 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConfigFileApplicationContextInitializer}.
|
||||
* Tests for {@link ConfigFileApplicationListener}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class ConfigFileApplicationContextInitializerTests {
|
||||
public class ConfigFileApplicationListenerTests {
|
||||
|
||||
private StaticApplicationContext context = new StaticApplicationContext();
|
||||
private StandardEnvironment environment = new StandardEnvironment();
|
||||
|
||||
private ConfigFileApplicationContextInitializer initializer = new ConfigFileApplicationContextInitializer();
|
||||
private SpringApplicationEnvironmentAvailableEvent event = new SpringApplicationEnvironmentAvailableEvent(
|
||||
new SpringApplication(), this.environment, new String[0]);
|
||||
|
||||
private ConfigFileApplicationListener initializer = new ConfigFileApplicationListener();
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
@ -59,50 +62,43 @@ public class ConfigFileApplicationContextInitializerTests {
|
||||
@Test
|
||||
public void loadPropertiesFile() throws Exception {
|
||||
this.initializer.setNames("testproperties");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("frompropertiesfile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomValue() throws Exception {
|
||||
StandardEnvironment environment = new StandardEnvironment();
|
||||
this.initializer.onApplicationEvent(new SpringApplicationEnvironmentAvailableEvent(
|
||||
new SpringApplication(), environment, new String[0]));
|
||||
String property = environment.getProperty("random.value");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("random.value");
|
||||
assertThat(property, notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadTwoPropertiesFiles() throws Exception {
|
||||
this.initializer.setNames("testproperties,moreproperties");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("frommorepropertiesfile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadYamlFile() throws Exception {
|
||||
this.initializer.setNames("testyaml");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromyamlfile"));
|
||||
assertThat(this.context.getEnvironment().getProperty("my.array[0]"), equalTo("1"));
|
||||
assertThat(this.context.getEnvironment().getProperty("my.array"),
|
||||
nullValue(String.class));
|
||||
assertThat(this.environment.getProperty("my.array[0]"), equalTo("1"));
|
||||
assertThat(this.environment.getProperty("my.array"), nullValue(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commandLineWins() throws Exception {
|
||||
this.context
|
||||
.getEnvironment()
|
||||
.getPropertySources()
|
||||
.addFirst(
|
||||
new SimpleCommandLinePropertySource(
|
||||
"--my.property=fromcommandline"));
|
||||
this.environment.getPropertySources().addFirst(
|
||||
new SimpleCommandLinePropertySource("--my.property=fromcommandline"));
|
||||
this.initializer.setNames("testproperties");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromcommandline"));
|
||||
}
|
||||
|
||||
@ -110,43 +106,43 @@ public class ConfigFileApplicationContextInitializerTests {
|
||||
public void systemPropertyWins() throws Exception {
|
||||
System.setProperty("my.property", "fromsystem");
|
||||
this.initializer.setNames("testproperties");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromsystem"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadPropertiesThenProfileProperties() throws Exception {
|
||||
this.initializer.setNames("enableprofile");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromprofilepropertiesfile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void profilePropertiesUsedInPlaceholders() throws Exception {
|
||||
this.initializer.setNames("enableprofile");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("one.more");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("one.more");
|
||||
assertThat(property, equalTo("fromprofilepropertiesfile"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void yamlProfiles() throws Exception {
|
||||
this.initializer.setNames("testprofiles");
|
||||
this.context.getEnvironment().setActiveProfiles("dev");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.environment.setActiveProfiles("dev");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromdevprofile"));
|
||||
property = this.context.getEnvironment().getProperty("my.other");
|
||||
property = this.environment.getProperty("my.other");
|
||||
assertThat(property, equalTo("notempty"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void yamlSetsProfiles() throws Exception {
|
||||
this.initializer.setNames("testsetprofiles");
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromdevprofile"));
|
||||
}
|
||||
|
||||
@ -156,9 +152,9 @@ public class ConfigFileApplicationContextInitializerTests {
|
||||
map.put("spring.profiles.active", "specificprofile");
|
||||
map.put("spring.config.name", "specificfile");
|
||||
MapPropertySource source = new MapPropertySource("map", map);
|
||||
this.context.getEnvironment().getPropertySources().addFirst(source);
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.environment.getPropertySources().addFirst(source);
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromspecificpropertiesfile"));
|
||||
}
|
||||
|
||||
@ -167,12 +163,12 @@ public class ConfigFileApplicationContextInitializerTests {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("spring.config.location", "classpath:/specificlocation.properties");
|
||||
MapPropertySource source = new MapPropertySource("map", map);
|
||||
this.context.getEnvironment().getPropertySources().addFirst(source);
|
||||
this.initializer.initialize(this.context);
|
||||
String property = this.context.getEnvironment().getProperty("my.property");
|
||||
this.environment.getPropertySources().addFirst(source);
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
String property = this.environment.getProperty("my.property");
|
||||
assertThat(property, equalTo("fromspecificlocation"));
|
||||
// The default property source is still there
|
||||
assertThat(this.context.getEnvironment().getProperty("foo"), equalTo("bucket"));
|
||||
assertThat(this.environment.getProperty("foo"), equalTo("bucket"));
|
||||
}
|
||||
|
||||
@Test
|
Loading…
Reference in New Issue
Block a user