Commit b21f56a2 authored by Phillip Webb's avatar Phillip Webb

Don't register shutdown hook from embedded context

Change `EmbeddedWebApplicationContext` to no longer automatically call
`registerShutdownHook()`. Shutdown hooks must now be registered by the
caller (for users of SpringApplication this will happen automatically).

Fixes gh-314
parent 5863795e
...@@ -128,7 +128,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext ...@@ -128,7 +128,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
@Override @Override
protected void onRefresh() { protected void onRefresh() {
super.onRefresh(); super.onRefresh();
registerShutdownHook();
try { try {
createEmbeddedServletContainer(); createEmbeddedServletContainer();
} }
......
...@@ -45,7 +45,6 @@ import org.springframework.web.context.request.SessionScope; ...@@ -45,7 +45,6 @@ import org.springframework.web.context.request.SessionScope;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
...@@ -109,14 +108,17 @@ public class EmbeddedWebApplicationContextTests { ...@@ -109,14 +108,17 @@ public class EmbeddedWebApplicationContextTests {
} }
@Test @Test
public void registersShutdownHook() throws Exception { public void doesNotRegistersShutdownHook() throws Exception {
// See gh-314 for background. We no longer register the shutdown hook
// since it is really the callers responsibility. The shutdown hook could
// also be problematic in a classic WAR deployment.
addEmbeddedServletContainerFactoryBean(); addEmbeddedServletContainerFactoryBean();
this.context.refresh(); this.context.refresh();
Field shutdownHookField = AbstractApplicationContext.class Field shutdownHookField = AbstractApplicationContext.class
.getDeclaredField("shutdownHook"); .getDeclaredField("shutdownHook");
shutdownHookField.setAccessible(true); shutdownHookField.setAccessible(true);
Object shutdownHook = shutdownHookField.get(this.context); Object shutdownHook = shutdownHookField.get(this.context);
assertThat(shutdownHook, not(nullValue())); assertThat(shutdownHook, nullValue());
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment