Commit 441572c6 authored by Dave Syer's avatar Dave Syer

Migrate config file initializer to a listener

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