Commit 17631486 authored by Phillip Webb's avatar Phillip Webb

Defer WebApplicationContext initialization

Update SpringBootServletInitializer so that the WebApplicationContext is
not initialized until ServletContextListener.contextInitialized() is
called. This makes it easier to subclass SpringBootServletInitializer
to add additional listeners to be called in a specific order.

Fixes gh-2070
parent d33c0ebf
...@@ -20,6 +20,7 @@ import javax.servlet.Filter; ...@@ -20,6 +20,7 @@ import javax.servlet.Filter;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
...@@ -62,20 +63,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit ...@@ -62,20 +63,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext); servletContext.addListener(new Listener());
if (rootAppContext != null) {
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
@Override
public void contextInitialized(ServletContextEvent event) {
// no-op because the application context is already initialized
}
});
}
else {
this.logger.debug("No ContextLoaderListener registered, as "
+ "createRootApplicationContext() did not "
+ "return an application context");
}
} }
protected WebApplicationContext createRootApplicationContext( protected WebApplicationContext createRootApplicationContext(
...@@ -136,4 +124,36 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit ...@@ -136,4 +124,36 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
return application; return application;
} }
/**
* {@link ServletContextListener} used to load the context
*/
private class Listener implements ServletContextListener {
private ContextLoaderListener contextLoaderListener;
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
WebApplicationContext applicationContext = createRootApplicationContext(servletContext);
if (applicationContext == null) {
SpringBootServletInitializer.this.logger
.debug("No ContextLoaderListener registered, as "
+ "createRootApplicationContext() did not "
+ "return an application context");
}
else {
this.contextLoaderListener = new ContextLoaderListener(applicationContext);
// Don't delegate contextInitialized event as we're already setup
}
}
@Override
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoaderListener != null) {
this.contextLoaderListener.contextDestroyed(event);
}
}
}
} }
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