Avoid leaking application context shutdown hooks in the tests

Closes gh-4053
This commit is contained in:
Andy Wilkinson
2015-10-02 13:21:12 +01:00
parent c6040e42a8
commit 10fbae8fb6
4 changed files with 26 additions and 9 deletions

View File

@@ -329,14 +329,6 @@ public class SpringApplication {
// Create, load, refresh and run the ApplicationContext
context = createApplicationContext();
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
@@ -358,6 +350,14 @@ public class SpringApplication {
// Refresh the context
refresh(context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
return context;

View File

@@ -76,7 +76,8 @@ public class SimpleMainTests {
private String[] getArgs(String... args) {
List<String> list = new ArrayList<String>(Arrays.asList(
"--spring.main.webEnvironment=false", "--spring.main.showBanner=false"));
"--spring.main.webEnvironment=false", "--spring.main.showBanner=false",
"--spring.main.registerShutdownHook=false"));
if (args.length > 0) {
list.add("--spring.main.sources="
+ StringUtils.arrayToCommaDelimitedString(args));

View File

@@ -673,6 +673,11 @@ public class SpringApplicationTests {
return this.applicationContext;
}
@Override
public void close() {
this.applicationContext.close();
}
}
private static class TestSpringApplication extends SpringApplication {

View File

@@ -310,5 +310,16 @@ public class SpringApplicationBuilderTests {
public boolean getRegisteredShutdownHook() {
return this.registeredShutdownHook;
}
@Override
public void close() {
super.close();
this.applicationContext.close();
}
@Override
public ApplicationContext getParent() {
return this.applicationContext.getParent();
}
}
}