Skip to content

Commit

Permalink
Allow FrameworkServlet to be used as a bean
Browse files Browse the repository at this point in the history
Change FrameworkServlet so that it can also be used as a bean within an
existing ApplicationContext. Primarily added to support use with
embedded servlet containers where Spring bootstraps the servlet
container, rather than the servlet container bootstrapping Spring.

Issue: SPR-10381
  • Loading branch information
philwebb committed May 8, 2013
1 parent eaf8115 commit 19187fd
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
Expand Down Expand Up @@ -122,14 +125,15 @@
* @author Sam Brannen
* @author Chris Beams
* @author Rossen Stoyanchev
* @author Phillip Webb
* @see #doService
* @see #setContextClass
* @see #setContextConfigLocation
* @see #setContextInitializerClasses
* @see #setNamespace
*/
@SuppressWarnings("serial")
public abstract class FrameworkServlet extends HttpServletBean {
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {

/**
* Suffix for WebApplicationContext namespaces. If a servlet of this class is
Expand Down Expand Up @@ -190,6 +194,9 @@ public abstract class FrameworkServlet extends HttpServletBean {
/** WebApplicationContext for this servlet */
private WebApplicationContext webApplicationContext;

/** If the WebApplicationContext was injected via {@link #setApplicationContext} */
private boolean webApplicationContextInjected = false;

/** Flag used to detect whether onRefresh has already been called */
private boolean refreshEventReceived = false;

Expand Down Expand Up @@ -789,6 +796,9 @@ protected void onRefresh(ApplicationContext context) {
*/
@Override
public void destroy() {
if (this.webApplicationContextInjected) {
return;
}
getServletContext().log("Destroying Spring FrameworkServlet '" + getServletName() + "'");
if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) this.webApplicationContext).close();
Expand Down Expand Up @@ -1055,6 +1065,30 @@ protected String getUsernameForRequest(HttpServletRequest request) {
return (userPrincipal != null ? userPrincipal.getName() : null);
}

/**
* Called by Spring via {@link ApplicationContextAware} to inject the current
* application context. This method allows FrameworkServlets to be registered as
* Spring Beans inside an existing {@link WebApplicationContext} rather than
* {@link #findWebApplicationContext() finding} a
* {@link org.springframework.web.context.ContextLoaderListener bootstrapped} context.
*
* <p>Primarily added to support use in embedded servlet containers, this method is not
* intended to be called directly.
*
* @since 4.0
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
if (this.webApplicationContext == null
&& applicationContext instanceof WebApplicationContext) {
if (logger.isDebugEnabled()) {
logger.debug("Using existing application context for "
+ ClassUtils.getShortName(getClass()));
}
this.webApplicationContext = (WebApplicationContext) applicationContext;
this.webApplicationContextInjected = true;
}
}

/**
* Subclasses must implement this method to do the work of request handling,
Expand Down

0 comments on commit 19187fd

Please sign in to comment.