Skip to content

Commit d5c0ef6

Browse files
author
Phillip Webb
committed
Add ConfigFileApplicationContextInitializer
Reintroduce ConfigFileApplicationContextInitializer for tests that wish to reuse 'application.properties' configuration. Fixes spring-projectsgh-344
1 parent 4a58171 commit d5c0ef6

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ private void onApplicationEnvironmentPreparedEvent(
131131

132132
private void onApplicationEnvironmentPreparedEvent(
133133
ConfigurableEnvironment environment, SpringApplication application) {
134+
addProperySources(environment);
135+
bindToSpringApplication(environment, application);
136+
}
137+
138+
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
139+
addPostProcessors(event.getApplicationContext());
140+
}
141+
142+
/**
143+
* Add config file property sources to the specified environment.
144+
* @param environment the environment to add source to
145+
* @see #addPostProcessors(ConfigurableApplicationContext)
146+
*/
147+
protected void addProperySources(ConfigurableEnvironment environment) {
134148
RandomValuePropertySource.addToEnvironment(environment);
135149
try {
136150
PropertySource<?> defaultProperties = environment.getPropertySources()
@@ -143,18 +157,25 @@ private void onApplicationEnvironmentPreparedEvent(
143157
catch (IOException ex) {
144158
throw new IllegalStateException("Unable to load configuration files", ex);
145159
}
146-
bindToSpringApplication(application, environment);
147160
}
148161

149-
private void bindToSpringApplication(SpringApplication application,
150-
ConfigurableEnvironment environment) {
162+
/**
163+
* Bind the environment to the {@link SpringApplication}.
164+
* @param environment the environment to bind
165+
* @param application the application to bind to
166+
*/
167+
protected void bindToSpringApplication(ConfigurableEnvironment environment,
168+
SpringApplication application) {
151169
RelaxedDataBinder binder = new RelaxedDataBinder(application, "spring.main");
152170
binder.setConversionService(this.conversionService);
153171
binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources()));
154172
}
155173

156-
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
157-
ConfigurableApplicationContext context = event.getApplicationContext();
174+
/**
175+
* Add appropriate post-processors to post-configure the property-sources.
176+
* @param context the context to configure
177+
*/
178+
protected void addPostProcessors(ConfigurableApplicationContext context) {
158179
context.addBeanFactoryPostProcessor(new PropertySourceOrderingPostProcessor(
159180
context));
160181
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test;
18+
19+
import org.springframework.boot.context.config.ConfigFileApplicationListener;
20+
import org.springframework.context.ApplicationContextInitializer;
21+
import org.springframework.context.ConfigurableApplicationContext;
22+
import org.springframework.test.context.ContextConfiguration;
23+
24+
/**
25+
* {@link ApplicationContextInitializer} that can be used with the
26+
* {@link ContextConfiguration#initializers()} to trigger loading of
27+
* {@literal application.properties}.
28+
*
29+
* @author Phillip Webb
30+
* @see ConfigFileApplicationListener
31+
*/
32+
public class ConfigFileApplicationContextInitializer implements
33+
ApplicationContextInitializer<ConfigurableApplicationContext> {
34+
35+
@Override
36+
public void initialize(final ConfigurableApplicationContext applicationContext) {
37+
new ConfigFileApplicationListener() {
38+
public void apply() {
39+
addProperySources(applicationContext.getEnvironment());
40+
addPostProcessors(applicationContext);
41+
}
42+
}.apply();
43+
}
44+
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.core.env.Environment;
24+
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26+
27+
import static org.hamcrest.Matchers.equalTo;
28+
import static org.junit.Assert.assertThat;
29+
30+
/**
31+
* Tests for {@link ConfigFileApplicationContextInitializer}.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
@RunWith(SpringJUnit4ClassRunner.class)
36+
@ContextConfiguration(classes = ConfigFileApplicationContextInitializerTests.Config.class, initializers = ConfigFileApplicationContextInitializer.class)
37+
public class ConfigFileApplicationContextInitializerTests {
38+
39+
@Autowired
40+
private Environment environment;
41+
42+
@Test
43+
public void test() {
44+
assertThat(this.environment.getProperty("foo"), equalTo("bucket"));
45+
}
46+
47+
@Configuration
48+
public static class Config {
49+
50+
}
51+
}

0 commit comments

Comments
 (0)