|
22 | 22 | import java.util.LinkedHashMap;
|
23 | 23 | import java.util.List;
|
24 | 24 | import java.util.Map;
|
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
25 | 26 |
|
26 |
| -import org.springframework.boot.Banner; |
27 | 27 | 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; |
30 | 29 | import org.springframework.boot.env.EnvironmentPostProcessor;
|
31 | 30 | import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
32 | 31 | import org.springframework.boot.origin.Origin;
|
33 | 32 | import org.springframework.boot.origin.OriginLookup;
|
34 | 33 | import org.springframework.cloud.bootstrap.support.OriginTrackedCompositePropertySource;
|
35 | 34 | 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; |
36 | 40 | import org.springframework.core.env.CompositePropertySource;
|
37 | 41 | import org.springframework.core.env.ConfigurableEnvironment;
|
38 | 42 | import org.springframework.core.env.MapPropertySource;
|
|
43 | 47 | import org.springframework.util.StringUtils;
|
44 | 48 |
|
45 | 49 | public class BootstrapEnvironmentPostProcessor implements EnvironmentPostProcessor {
|
| 50 | + |
46 | 51 | /**
|
47 | 52 | * Property source name for bootstrap.
|
48 | 53 | */
|
49 | 54 | public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap";
|
| 55 | + |
50 | 56 | /**
|
51 | 57 | * The name of the default properties.
|
52 | 58 | */
|
@@ -105,6 +111,7 @@ private ConfigurableApplicationContext bootstrapServiceContext(
|
105 | 111 | bootstrapProperties.addLast(source);
|
106 | 112 | }
|
107 | 113 | // TODO: is it possible or sensible to share a ResourceLoader?
|
| 114 | + /* |
108 | 115 | SpringApplicationBuilder builder = new SpringApplicationBuilder()
|
109 | 116 | .profiles(environment.getActiveProfiles()).bannerMode(Banner.Mode.OFF)
|
110 | 117 | .environment(bootstrapEnvironment)
|
@@ -132,6 +139,18 @@ private ConfigurableApplicationContext bootstrapServiceContext(
|
132 | 139 | }
|
133 | 140 | builder.sources(BootstrapImportSelectorConfiguration.class);
|
134 | 141 | 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 | + |
135 | 154 | // gh-214 using spring.application.name=bootstrap to set the context id via
|
136 | 155 | // `ContextIdApplicationContextInitializer` prevents apps from getting the actual
|
137 | 156 | // spring.application.name
|
@@ -203,6 +222,58 @@ private void addOrReplace(MutablePropertySources environment,
|
203 | 222 | }
|
204 | 223 | }
|
205 | 224 |
|
| 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 | + |
206 | 277 | private static class ExtendedDefaultPropertySource
|
207 | 278 | extends SystemEnvironmentPropertySource implements OriginLookup<String> {
|
208 | 279 |
|
|
0 commit comments