[bs-52] Added support for SpringApplication in a ServletContextInitializer
WAR applications should extend SpringServletInitializer to take advantage of Servlet 3.0 initialization and SpringApplication context loading features. [#48386505] [bs-52] Support for running "traditional" webapps in placepull/2/merge
parent
7bd1aeb514
commit
20cce0c69c
@ -1,55 +0,0 @@
|
||||
package org.springframework.bootstrap.web;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.filter.DelegatingFilterProxy;
|
||||
import org.springframework.web.servlet.FrameworkServlet;
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A handy opinionated {@link WebApplicationInitializer} for applications that only have
|
||||
* one Spring servlet, and no more than a single filter (which itself is only enabled when
|
||||
* Spring Security is detected). If your application is more complicated consider using
|
||||
* one of the other WebApplicationInitializers.
|
||||
* <p>
|
||||
*
|
||||
* <p>
|
||||
* Note that a WebApplicationInitializer is only needed if you are building a war file and
|
||||
* deploying it. If you prefer to run an embedded container (we do) then you won't need
|
||||
* this at all.
|
||||
* </p>
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public abstract class BootstrapServletInitializer extends
|
||||
AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Filter[] getServletFilters() {
|
||||
if (ClassUtils.isPresent(
|
||||
"org.springframework.security.config.annotation.web.EnableWebSecurity",
|
||||
null)) {
|
||||
DelegatingFilterProxy filter = new DelegatingFilterProxy(
|
||||
"springSecurityFilterChain");
|
||||
filter.setContextAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX
|
||||
+ "dispatcher");
|
||||
return new Filter[] { filter };
|
||||
}
|
||||
return new Filter[0];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package org.springframework.bootstrap.web;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.bootstrap.SpringApplication;
|
||||
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A handy opinionated {@link WebApplicationInitializer} for applications that only have
|
||||
* one Spring servlet, and no more than a single filter (which itself is only enabled when
|
||||
* Spring Security is detected). If your application is more complicated consider using
|
||||
* one of the other WebApplicationInitializers.
|
||||
* <p>
|
||||
*
|
||||
* <p>
|
||||
* Note that a WebApplicationInitializer is only needed if you are building a war file and
|
||||
* deploying it. If you prefer to run an embedded container (we do) then you won't need
|
||||
* this at all.
|
||||
* </p>
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public abstract class SpringServletInitializer implements WebApplicationInitializer {
|
||||
|
||||
/** Logger available to subclasses. */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
WebApplicationContext rootAppContext = this
|
||||
.createRootApplicationContext(servletContext);
|
||||
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(
|
||||
ServletContext servletContext) {
|
||||
ApplicationContext parent = null;
|
||||
Object object = servletContext
|
||||
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
||||
if (object != null && object instanceof ApplicationContext) {
|
||||
this.logger.info("Root context already created (using as parent).");
|
||||
parent = (ApplicationContext) object;
|
||||
servletContext.setAttribute(
|
||||
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
|
||||
}
|
||||
SpringApplication application = new SpringApplication(
|
||||
(Object[]) getConfigClasses());
|
||||
AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
context.setParent(parent);
|
||||
context.setServletContext(servletContext);
|
||||
application.setApplicationContext(context);
|
||||
return (WebApplicationContext) application.run();
|
||||
}
|
||||
|
||||
protected abstract Class<?>[] getConfigClasses();
|
||||
|
||||
}
|
Loading…
Reference in New Issue