diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java index c0e9a81ff2..262ffdd660 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java @@ -333,6 +333,7 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine this.started = false; try { this.manager.stop(); + this.manager.undeploy(); this.undertow.stop(); } catch (Exception ex) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 59a9317733..86f9f54165 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -55,6 +55,8 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.servlet.GenericServlet; import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -110,6 +112,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; @@ -1035,6 +1038,26 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { assertThat(documentRoot).isNull(); } + @Test + public void servletContextListenerContextDestroyedIsCalledWhenContainerIsStopped() + throws Exception { + final ServletContextListener listener = mock(ServletContextListener.class); + AbstractEmbeddedServletContainerFactory factory = getFactory(); + this.container = factory + .getEmbeddedServletContainer(new ServletContextInitializer() { + + @Override + public void onStartup(ServletContext servletContext) + throws ServletException { + servletContext.addListener(listener); + } + + }); + this.container.start(); + this.container.stop(); + verify(listener).contextDestroyed(any(ServletContextEvent.class)); + } + protected abstract void addConnector(int port, AbstractEmbeddedServletContainerFactory factory);