Merge branch '1.2.x'

This commit is contained in:
Andy Wilkinson 2015-10-09 12:42:23 +01:00
commit aae1b31f3a
3 changed files with 49 additions and 1 deletions

View File

@ -146,8 +146,8 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
@Override @Override
protected void doClose() { protected void doClose() {
super.doClose();
stopAndReleaseEmbeddedServletContainer(); stopAndReleaseEmbeddedServletContainer();
super.doClose();
} }
private synchronized void createEmbeddedServletContainer() { private synchronized void createEmbeddedServletContainer() {

View File

@ -36,11 +36,14 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.Scope; import org.springframework.beans.factory.config.Scope;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory.MockEmbeddedServletContainer;
import org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer; import org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@ -56,6 +59,7 @@ import org.springframework.web.filter.GenericFilterBean;
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.is;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance; import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -442,6 +446,22 @@ public class EmbeddedWebApplicationContextTests {
sameInstance(scope)); sameInstance(scope));
} }
@Test
public void containerIsStoppedBeforeContextIsClosed() {
addEmbeddedServletContainerFactoryBean();
this.context.registerBeanDefinition("shutdownOrderingValidator",
BeanDefinitionBuilder.rootBeanDefinition(ShutdownOrderingValidator.class)
.addConstructorArgReference("embeddedServletContainerFactory")
.getBeanDefinition());
this.context.refresh();
ShutdownOrderingValidator validator = this.context
.getBean(ShutdownOrderingValidator.class);
this.context.close();
assertThat(validator.destroyed, is(true));
assertThat(validator.containerStoppedFirst, is(true));
}
private void addEmbeddedServletContainerFactoryBean() { private void addEmbeddedServletContainerFactoryBean() {
this.context.registerBeanDefinition("embeddedServletContainerFactory", this.context.registerBeanDefinition("embeddedServletContainerFactory",
new RootBeanDefinition(MockEmbeddedServletContainerFactory.class)); new RootBeanDefinition(MockEmbeddedServletContainerFactory.class));
@ -491,4 +511,25 @@ public class EmbeddedWebApplicationContextTests {
} }
protected static class ShutdownOrderingValidator implements DisposableBean {
private final MockEmbeddedServletContainer servletContainer;
private boolean destroyed = false;
private boolean containerStoppedFirst = false;
ShutdownOrderingValidator(
MockEmbeddedServletContainerFactory servletContainerFactory) {
this.servletContainer = servletContainerFactory.getContainer();
}
@Override
public void destroy() {
this.destroyed = true;
this.containerStoppedFirst = this.servletContainer.isStopped();
}
}
} }

View File

@ -90,6 +90,8 @@ public class MockEmbeddedServletContainerFactory
private final int port; private final int port;
private boolean stopped = false;
public MockEmbeddedServletContainer(ServletContextInitializer[] initializers, public MockEmbeddedServletContainer(ServletContextInitializer[] initializers,
int port) { int port) {
this.initializers = initializers; this.initializers = initializers;
@ -174,6 +176,11 @@ public class MockEmbeddedServletContainerFactory
public void stop() { public void stop() {
this.servletContext = null; this.servletContext = null;
this.registeredServlets.clear(); this.registeredServlets.clear();
this.stopped = true;
}
public boolean isStopped() {
return this.stopped;
} }
public Servlet[] getServlets() { public Servlet[] getServlets() {