Skip to content

Commit 1d48c79

Browse files
committed
Doesn't use SpringApplication to load bootstrap
1 parent 96c44d0 commit 1d48c79

File tree

5 files changed

+83
-7
lines changed

5 files changed

+83
-7
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapEnvironmentPostProcessor.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222
import java.util.LinkedHashMap;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.concurrent.atomic.AtomicBoolean;
2526

26-
import org.springframework.boot.Banner;
2727
import org.springframework.boot.SpringApplication;
28-
import org.springframework.boot.WebApplicationType;
29-
import org.springframework.boot.builder.SpringApplicationBuilder;
28+
import org.springframework.boot.context.config.ConfigFileApplicationListener;
3029
import org.springframework.boot.env.EnvironmentPostProcessor;
3130
import org.springframework.boot.env.OriginTrackedMapPropertySource;
3231
import org.springframework.boot.origin.Origin;
3332
import org.springframework.boot.origin.OriginLookup;
3433
import org.springframework.cloud.bootstrap.support.OriginTrackedCompositePropertySource;
3534
import org.springframework.context.ConfigurableApplicationContext;
35+
import org.springframework.context.Lifecycle;
36+
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
37+
import org.springframework.context.annotation.Bean;
38+
import org.springframework.context.annotation.Configuration;
39+
import org.springframework.context.support.StaticApplicationContext;
3640
import org.springframework.core.env.CompositePropertySource;
3741
import org.springframework.core.env.ConfigurableEnvironment;
3842
import org.springframework.core.env.MapPropertySource;
@@ -43,10 +47,12 @@
4347
import org.springframework.util.StringUtils;
4448

4549
public class BootstrapEnvironmentPostProcessor implements EnvironmentPostProcessor {
50+
4651
/**
4752
* Property source name for bootstrap.
4853
*/
4954
public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap";
55+
5056
/**
5157
* The name of the default properties.
5258
*/
@@ -105,6 +111,7 @@ private ConfigurableApplicationContext bootstrapServiceContext(
105111
bootstrapProperties.addLast(source);
106112
}
107113
// TODO: is it possible or sensible to share a ResourceLoader?
114+
/*
108115
SpringApplicationBuilder builder = new SpringApplicationBuilder()
109116
.profiles(environment.getActiveProfiles()).bannerMode(Banner.Mode.OFF)
110117
.environment(bootstrapEnvironment)
@@ -132,6 +139,18 @@ private ConfigurableApplicationContext bootstrapServiceContext(
132139
}
133140
builder.sources(BootstrapImportSelectorConfiguration.class);
134141
final ConfigurableApplicationContext context = builder.run();
142+
*/
143+
144+
final StaticApplicationContext context = new StaticApplicationContext();
145+
AnnotatedBeanDefinitionReader annotatedReader = new AnnotatedBeanDefinitionReader(
146+
context);
147+
annotatedReader.setEnvironment(bootstrapEnvironment);
148+
annotatedReader.register(BootstrapImportSelectorConfiguration.class,
149+
BootstrapConfigFileConfiguration.class);
150+
context.setEnvironment(bootstrapEnvironment);
151+
152+
context.refresh();
153+
135154
// gh-214 using spring.application.name=bootstrap to set the context id via
136155
// `ContextIdApplicationContextInitializer` prevents apps from getting the actual
137156
// spring.application.name
@@ -203,6 +222,58 @@ private void addOrReplace(MutablePropertySources environment,
203222
}
204223
}
205224

225+
@Configuration(proxyBeanMethods = false)
226+
private static class BootstrapConfigFileConfiguration {
227+
228+
@Bean
229+
public BootstrapConfigFileLoader bootstrapConfigFileLoader(
230+
ConfigurableEnvironment env) {
231+
return new BootstrapConfigFileLoader(env);
232+
}
233+
234+
}
235+
236+
private static class BootstrapConfigFileLoader implements Lifecycle {
237+
238+
private final AtomicBoolean started = new AtomicBoolean();
239+
240+
private final ConfigurableEnvironment environment;
241+
242+
BootstrapConfigFileLoader(ConfigurableEnvironment environment) {
243+
this.environment = environment;
244+
ConfigFileApplicationListener listener = new ConfigFileApplicationListener();
245+
listener.postProcessEnvironment(this.environment, new BootstrapApplication());
246+
}
247+
248+
@Override
249+
public void start() {
250+
if (started.compareAndSet(false, true)) {
251+
ConfigFileApplicationListener listener = new ConfigFileApplicationListener();
252+
listener.postProcessEnvironment(this.environment,
253+
new BootstrapApplication());
254+
}
255+
}
256+
257+
@Override
258+
public void stop() {
259+
started.compareAndSet(true, false);
260+
}
261+
262+
@Override
263+
public boolean isRunning() {
264+
return started.get();
265+
}
266+
267+
}
268+
269+
private static class BootstrapApplication extends SpringApplication {
270+
271+
BootstrapApplication() {
272+
// setResourceLoader(new DefaultResourceLoader(getClassLoader()));
273+
}
274+
275+
}
276+
206277
private static class ExtendedDefaultPropertySource
207278
extends SystemEnvironmentPropertySource implements OriginLookup<String> {
208279

spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapEnvironmentPostProcessorIntegrationTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
import org.springframework.boot.test.context.SpringBootTest;
3030
import org.springframework.core.env.ConfigurableEnvironment;
3131
import org.springframework.core.env.MapPropertySource;
32+
import org.springframework.test.context.ActiveProfiles;
3233
import org.springframework.test.context.junit4.SpringRunner;
3334

3435
import static org.assertj.core.api.Assertions.assertThat;
3536

3637
// see https://github.com/spring-cloud/spring-cloud-commons/issues/476
3738
@RunWith(SpringRunner.class)
39+
@ActiveProfiles("epptests")
3840
@SpringBootTest
3941
public class BootstrapEnvironmentPostProcessorIntegrationTests {
4042

@@ -53,6 +55,8 @@ public void conditionalValuesFromMapProperySourceCreatedByEPPExist() {
5355
assertThat(this.env.getProperty("conditional.property"))
5456
.as("Environment has wrong value for conditional.property")
5557
.isEqualTo("conditional.value");
58+
assertThat(this.env.getProperty("info.name"))
59+
.isEqualTo("from bootstrap-epptests");
5660
}
5761

5862
@EnableAutoConfiguration

spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapEnvironmentPostProcessorTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
import org.junit.Test;
2020
import org.junit.runner.RunWith;
2121

22-
import org.springframework.beans.BeansException;
2322
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.boot.SpringBootConfiguration;
2625
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2726
import org.springframework.boot.test.context.SpringBootTest;
28-
import org.springframework.context.ApplicationContext;
2927
import org.springframework.context.ConfigurableApplicationContext;
3028
import org.springframework.core.env.ConfigurableEnvironment;
3129
import org.springframework.test.context.junit4.SpringRunner;
@@ -46,7 +44,8 @@ public class BootstrapEnvironmentPostProcessorTests {
4644
@Test
4745
public void contextLoads() {
4846
// from bootstrap.properties
49-
assertThat(env.getProperty("test.property")).isEqualTo("from bootstrap.properties");
47+
assertThat(env.getProperty("test.property"))
48+
.isEqualTo("from bootstrap.properties");
5049

5150
try {
5251
assertThat(context.getBean("foo-during-bootstrap"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
info.name=from bootstrap-epptests
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
spring.main.sources:org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.PropertySourceConfiguration,org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.CompositePropertySourceConfiguration
22
info.name:child
33
info.desc: defaultPropertiesInfoDesc
4-
test.property: from bootstrap.properties
4+
test.property: from bootstrap.properties
5+

0 commit comments

Comments
 (0)