Investigating
This commit is contained in:
@@ -99,7 +99,7 @@ public class SpringApplicationBuilder {
|
||||
if (this.parent != null) {
|
||||
// If there is a parent initialize it and make sure it is added to the current
|
||||
// context
|
||||
addInitializers(new ParentContextApplicationContextInitializer(
|
||||
addInitializers(true, new ParentContextApplicationContextInitializer(
|
||||
this.parent.run(args)));
|
||||
}
|
||||
|
||||
@@ -111,6 +111,8 @@ public class SpringApplicationBuilder {
|
||||
if (this.running.compareAndSet(false, true)) {
|
||||
synchronized (this.running) {
|
||||
// If not already running copy the sources over and then run.
|
||||
// this.application.setDefaultArgs(this.defaultArgs
|
||||
// .toArray(new String[this.defaultArgs.size()]));
|
||||
this.application.setSources(this.sources);
|
||||
this.context = this.application.run(args);
|
||||
}
|
||||
@@ -190,7 +192,7 @@ public class SpringApplicationBuilder {
|
||||
this.parent = new SpringApplicationBuilder();
|
||||
this.parent.context = parent;
|
||||
this.parent.running.set(true);
|
||||
addInitializers(new ParentContextApplicationContextInitializer(parent));
|
||||
addInitializers(true, new ParentContextApplicationContextInitializer(parent));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -394,17 +396,24 @@ public class SpringApplicationBuilder {
|
||||
*/
|
||||
public SpringApplicationBuilder initializers(
|
||||
ApplicationContextInitializer<?>... initializers) {
|
||||
addInitializers(initializers);
|
||||
addInitializers(false, initializers);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param initializers the initializers to add
|
||||
*/
|
||||
private void addInitializers(ApplicationContextInitializer<?>... initializers) {
|
||||
Set<ApplicationContextInitializer<?>> target = new LinkedHashSet<ApplicationContextInitializer<?>>(
|
||||
this.application.getInitializers());
|
||||
target.addAll(Arrays.asList(initializers));
|
||||
private void addInitializers(boolean prepend,
|
||||
ApplicationContextInitializer<?>... initializers) {
|
||||
Set<ApplicationContextInitializer<?>> target = new LinkedHashSet<ApplicationContextInitializer<?>>();
|
||||
if (prepend) {
|
||||
target.addAll(Arrays.asList(initializers));
|
||||
target.addAll(this.application.getInitializers());
|
||||
}
|
||||
else {
|
||||
target.addAll(this.application.getInitializers());
|
||||
target.addAll(Arrays.asList(initializers));
|
||||
}
|
||||
this.application.setInitializers(target);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,7 +171,7 @@ public class ConfigFileApplicationContextInitializer implements
|
||||
}
|
||||
|
||||
Resource resource = resourceLoader.getResource(location);
|
||||
PropertySource<?> propertySource = getPropertySource(resource, loaders);
|
||||
PropertySource<?> propertySource = getPropertySource(resource, profile, loaders);
|
||||
if (propertySource == null) {
|
||||
return;
|
||||
}
|
||||
@@ -187,9 +187,9 @@ public class ConfigFileApplicationContextInitializer implements
|
||||
environment.getPropertySources().addLast(propertySource);
|
||||
}
|
||||
|
||||
private PropertySource<?> getPropertySource(Resource resource,
|
||||
private PropertySource<?> getPropertySource(Resource resource, String profile,
|
||||
List<PropertySourceLoader> loaders) {
|
||||
String key = resource.getDescription();
|
||||
String key = resource.getDescription() + (profile == null ? "" : "#" + profile);
|
||||
if (this.cached.containsKey(key)) {
|
||||
return this.cached.get(key);
|
||||
}
|
||||
|
||||
@@ -87,6 +87,23 @@ public class SpringApplicationBuilderTests {
|
||||
any(ApplicationContext.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parentFirstCreationWithProfileAndDefaultArgs() throws Exception {
|
||||
SpringApplicationBuilder application = new SpringApplicationBuilder(
|
||||
ExampleConfig.class).profiles("node").defaultArgs("--transport=redis")
|
||||
.child(ChildConfig.class).web(false);
|
||||
this.context = application.run();
|
||||
assertThat(this.context.getEnvironment().acceptsProfiles("node"), is(true));
|
||||
assertThat(this.context.getEnvironment().getProperty("transport"),
|
||||
is(equalTo("redis")));
|
||||
assertThat(this.context.getParent().getEnvironment().acceptsProfiles("node"),
|
||||
is(true));
|
||||
assertThat(this.context.getParent().getEnvironment().getProperty("transport"),
|
||||
is(equalTo("redis")));
|
||||
// only defined in node profile
|
||||
assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("spam")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parentContextIdentical() throws Exception {
|
||||
SpringApplicationBuilder application = new SpringApplicationBuilder(
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
bar: spam
|
||||
@@ -8,6 +8,7 @@
|
||||
</encoder>
|
||||
</appender>
|
||||
<logger name="org.springframework.boot.context.annotation" level="TRACE" />
|
||||
<logger name="org.springframework.boot.context.initializer" level="TRACE" />
|
||||
<logger name="org.springframework.boot.config" level="TRACE" />
|
||||
<logger name="org.thymeleaf" level="TRACE" />
|
||||
<root level="INFO">
|
||||
|
||||
Reference in New Issue
Block a user