Browse Source

Avoid leaking application context shutdown hooks in the tests

Closes gh-4053
pull/4077/merge
Andy Wilkinson 10 years ago
parent
commit
10fbae8fb6
  1. 16
      spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 3
      spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java
  3. 5
      spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
  4. 11
      spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java

16
spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

@ -329,14 +329,6 @@ public class SpringApplication { @@ -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 { @@ -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;

3
spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java

@ -76,7 +76,8 @@ public class SimpleMainTests { @@ -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));

5
spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

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

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

@ -310,5 +310,16 @@ public class SpringApplicationBuilderTests { @@ -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();
}
}
}

Loading…
Cancel
Save