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 fecd9899bea..d04b8177b32 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 @@ -33,6 +33,7 @@ import org.springframework.context.event.SourceFilteringListener; import org.springframework.context.i18n.LocaleContext; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.SimpleLocaleContext; +import org.springframework.util.ObjectUtils; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.request.RequestAttributes; @@ -121,6 +122,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; @@ -185,6 +189,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. @@ -413,23 +432,29 @@ public abstract class FrameworkServlet extends HttpServletBean { (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); // Assign the best possible id value. - 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.setParent(parent); 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 e2b06108ff6..d27c47235c6 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 @@ -77,11 +77,18 @@ import org.springframework.util.ObjectUtils; public class ContextLoader { /** - * Config param for the root WebApplicationContext implementation class to - * use: "contextClass" + * Config param for the root WebApplicationContext implementation class to use: + * "contextClass" */ 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: + * "contextId" + */ + public static final String CONTEXT_ID_PARAM = "contextId"; + /** * Name of servlet context parameter (i.e., "contextConfigLocation") * that can specify the config location for the root context, falling back @@ -251,21 +258,20 @@ public class ContextLoader { (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); // Assign the best possible id value. - 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())); } }