Skip to content

Commit ee3d4b3

Browse files
committed
Consider parent when application is built and then run
Previously, the parent context was only considered when the builder was used to run the application. If the application was built using the builder and then run using SpringApplication.run, the parent context was not considered. This commit updates the builder to consider the parent both when it's used to run the application and when it's used to build the application that will later be run via SpringApplication.run Closes gh-4014
1 parent 050a4fe commit ee3d4b3

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,7 @@
5959
* SpringApplication instead.
6060
*
6161
* @author Dave Syer
62+
* @author Andy Wilkinson
6263
*/
6364
public class SpringApplicationBuilder {
6465

@@ -80,6 +81,8 @@ public class SpringApplicationBuilder {
8081

8182
private boolean registerShutdownHookApplied;
8283

84+
private boolean configuredAsChild = false;
85+
8386
public SpringApplicationBuilder(Object... sources) {
8487
this.application = createSpringApplication(sources);
8588
}
@@ -120,19 +123,11 @@ public SpringApplication application() {
120123
* @return an application context created from the current state
121124
*/
122125
public ConfigurableApplicationContext run(String... args) {
123-
if (this.parent != null) {
124-
// If there is a parent don't register a shutdown hook
125-
if (!this.registerShutdownHookApplied) {
126-
this.application.setRegisterShutdownHook(false);
127-
}
128-
// initialize it and make sure it is added to the current context
129-
initializers(new ParentContextApplicationContextInitializer(
130-
this.parent.run(args)));
131-
}
132126
if (this.running.get()) {
133127
// If already created we just return the existing context
134128
return this.context;
135129
}
130+
configureAsChildIfNecessary();
136131
if (this.running.compareAndSet(false, true)) {
137132
synchronized (this.running) {
138133
// If not already running copy the sources over and then run.
@@ -142,11 +137,23 @@ public ConfigurableApplicationContext run(String... args) {
142137
return this.context;
143138
}
144139

140+
private void configureAsChildIfNecessary() {
141+
if (this.parent != null && !this.configuredAsChild) {
142+
this.configuredAsChild = true;
143+
if (!this.registerShutdownHookApplied) {
144+
this.application.setRegisterShutdownHook(false);
145+
}
146+
initializers(
147+
new ParentContextApplicationContextInitializer(this.parent.run()));
148+
}
149+
}
150+
145151
/**
146152
* Returns a fully configured {@link SpringApplication} that is ready to run.
147153
* @return the fully configured {@link SpringApplication}.
148154
*/
149155
public SpringApplication build() {
156+
configureAsChildIfNecessary();
150157
this.application.setSources(this.sources);
151158
return this.application;
152159
}
@@ -405,7 +412,7 @@ private Map<String, Object> getMapFromProperties(Properties properties) {
405412
/**
406413
* Default properties for the environment. Multiple calls to this method are
407414
* cumulative.
408-
* @param defaults
415+
* @param defaults the default properties
409416
* @return the current builder
410417
* @see SpringApplicationBuilder#properties(String...)
411418
*/

spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void specificApplicationContextClass() throws Exception {
9898
}
9999

100100
@Test
101-
public void parentContextCreation() throws Exception {
101+
public void parentContextCreationThatIsRunDirectly() throws Exception {
102102
SpringApplicationBuilder application = new SpringApplicationBuilder(
103103
ChildConfig.class).contextClass(SpyApplicationContext.class);
104104
application.parent(ExampleConfig.class);
@@ -109,6 +109,18 @@ public void parentContextCreation() throws Exception {
109109
equalTo(false));
110110
}
111111

112+
@Test
113+
public void parentContextCreationThatIsBuiltThenRun() throws Exception {
114+
SpringApplicationBuilder application = new SpringApplicationBuilder(
115+
ChildConfig.class).contextClass(SpyApplicationContext.class);
116+
application.parent(ExampleConfig.class);
117+
this.context = application.build().run();
118+
verify(((SpyApplicationContext) this.context).getApplicationContext())
119+
.setParent(any(ApplicationContext.class));
120+
assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook(),
121+
equalTo(false));
122+
}
123+
112124
@Test
113125
public void parentContextCreationWithChildShutdown() throws Exception {
114126
SpringApplicationBuilder application = new SpringApplicationBuilder(

0 commit comments

Comments
 (0)