diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java index 8491f1a96db..82d9e9a68a8 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.security.Principal; import java.util.ArrayList; import java.util.Collections; - import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -154,6 +153,9 @@ public abstract class FrameworkServlet extends HttpServletBean { /** WebApplicationContext implementation class to create */ private Class contextClass = DEFAULT_CONTEXT_CLASS; + /** WebApplicationContext id to assign */ + private String contextId; + /** Namespace for this servlet */ private String namespace; @@ -253,6 +255,7 @@ public abstract class FrameworkServlet extends HttpServletBean { this.webApplicationContext = webApplicationContext; } + /** * Set the name of the ServletContext attribute which should be used to retrieve the * {@link WebApplicationContext} that this servlet is supposed to use. @@ -289,6 +292,21 @@ public abstract class FrameworkServlet extends HttpServletBean { return this.contextClass; } + /** + * Specify a custom WebApplicationContext id, + * to be used as serialization id for the underlying BeanFactory. + */ + public void setContextId(String contextId) { + this.contextId = contextId; + } + + /** + * Return the custom WebApplicationContext id, if any. + */ + public String getContextId() { + return this.contextId; + } + /** * Set a custom namespace for this servlet, * to be used for building a default context config location. @@ -574,27 +592,32 @@ public abstract class FrameworkServlet extends HttpServletBean { } protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { - if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information - ServletContext sc = getServletContext(); - if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { - // Servlet <= 2.4: resort to name specified in web.xml, if any. - String servletContextName = sc.getServletContextName(); - if (servletContextName != null) { - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName + - "." + getServletName()); + if (this.contextId != null) { + wac.setId(this.contextId); + } + else { + // Generate default id... + ServletContext sc = getServletContext(); + if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { + // Servlet <= 2.4: resort to name specified in web.xml, if any. + String servletContextName = sc.getServletContextName(); + if (servletContextName != null) { + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName + + "." + getServletName()); + } + else { + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName()); + } } else { - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName()); + // Servlet 2.5's getContextPath available! + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + + ObjectUtils.getDisplayString(sc.getContextPath()) + "/" + getServletName()); } } - else { - // Servlet 2.5's getContextPath available! - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath() + - "/" + getServletName()); - } } wac.setServletContext(getServletContext()); diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java b/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java index 5b5669f8c1d..6176aad650c 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java @@ -23,11 +23,11 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; - import javax.servlet.ServletContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryReference; @@ -91,13 +91,18 @@ import org.springframework.util.StringUtils; public class ContextLoader { /** - * Config param for the root WebApplicationContext implementation class to - * use: {@value} + * Config param for the root WebApplicationContext implementation class to use: {@value} * @see #determineContextClass(ServletContext) * @see #createWebApplicationContext(ServletContext, ApplicationContext) */ public static final String CONTEXT_CLASS_PARAM = "contextClass"; + /** + * Config param for the root WebApplicationContext id, + * to be used as serialization id for the underlying BeanFactory: {@value} + */ + public static final String CONTEXT_ID_PARAM = "contextId"; + /** * Config param for which {@link ApplicationContextInitializer} classes to use * for initializing the web application context: {@value} @@ -348,21 +353,20 @@ public class ContextLoader { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information - if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { - // Servlet <= 2.4: resort to name specified in web.xml, if any. - String servletContextName = sc.getServletContextName(); - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + - ObjectUtils.getDisplayString(servletContextName)); + String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); + if (idParam != null) { + wac.setId(idParam); } else { - // Servlet 2.5's getContextPath available! - try { - String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(sc); + // Generate default id... + if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { + // Servlet <= 2.4: resort to name specified in web.xml, if any. wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + - ObjectUtils.getDisplayString(contextPath)); + ObjectUtils.getDisplayString(sc.getServletContextName())); } - catch (Exception ex) { - throw new IllegalStateException("Failed to invoke Servlet 2.5 getContextPath method", ex); + else { + wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + + ObjectUtils.getDisplayString(sc.getContextPath())); } } }