From b65625bec328c8830631700005f8b7031b81cbc0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 31 Jul 2013 01:54:53 -0700 Subject: [PATCH] Ensure registerShutdownHook is called for apps --- .../java/org/springframework/boot/SpringApplication.java | 1 + .../org/springframework/boot/SpringApplicationTests.java | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 1e2f0a44da1..5e9d0b3794f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -254,6 +254,7 @@ public class SpringApplication { // Create, load, refresh and run the ApplicationContext ApplicationContext context = createApplicationContext(); if (context instanceof ConfigurableApplicationContext) { + ((ConfigurableApplicationContext) context).registerShutdownHook(); ((ConfigurableApplicationContext) context).setEnvironment(environment); } postProcessApplicationContext(context); diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index c2a07024ac9..d638d9743a8 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -326,6 +326,15 @@ public class SpringApplicationTests { assertThat(application.getShowBanner(), is(false)); } + @Test + public void registerShutdownHook() throws Exception { + SpringApplication application = new SpringApplication(ExampleConfig.class); + ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext()); + application.setApplicationContext(applicationContext); + application.run(); + verify(applicationContext).registerShutdownHook(); + } + private boolean hasPropertySource(ConfigurableEnvironment environment, Class propertySourceClass, String name) { for (PropertySource source : environment.getPropertySources()) {