Skip to content

Commit 10d9860

Browse files
committed
ContextLoader and FrameworkServlet support "contextId" parameter for custom serialization id
1 parent 01eab03 commit 10d9860

File tree

2 files changed

+57
-26
lines changed

2 files changed

+57
-26
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FrameworkServlet.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.context.i18n.LocaleContext;
3434
import org.springframework.context.i18n.LocaleContextHolder;
3535
import org.springframework.context.i18n.SimpleLocaleContext;
36+
import org.springframework.util.ObjectUtils;
3637
import org.springframework.web.context.ConfigurableWebApplicationContext;
3738
import org.springframework.web.context.WebApplicationContext;
3839
import org.springframework.web.context.request.RequestAttributes;
@@ -121,6 +122,9 @@ public abstract class FrameworkServlet extends HttpServletBean {
121122
/** WebApplicationContext implementation class to create */
122123
private Class contextClass = DEFAULT_CONTEXT_CLASS;
123124

125+
/** WebApplicationContext id to assign */
126+
private String contextId;
127+
124128
/** Namespace for this servlet */
125129
private String namespace;
126130

@@ -185,6 +189,21 @@ public Class getContextClass() {
185189
return this.contextClass;
186190
}
187191

192+
/**
193+
* Specify a custom WebApplicationContext id,
194+
* to be used as serialization id for the underlying BeanFactory.
195+
*/
196+
public void setContextId(String contextId) {
197+
this.contextId = contextId;
198+
}
199+
200+
/**
201+
* Return the custom WebApplicationContext id, if any.
202+
*/
203+
public String getContextId() {
204+
return this.contextId;
205+
}
206+
188207
/**
189208
* Set a custom namespace for this servlet,
190209
* to be used for building a default context config location.
@@ -413,23 +432,29 @@ protected WebApplicationContext createWebApplicationContext(ApplicationContext p
413432
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
414433

415434
// Assign the best possible id value.
416-
ServletContext sc = getServletContext();
417-
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
418-
// Servlet <= 2.4: resort to name specified in web.xml, if any.
419-
String servletContextName = sc.getServletContextName();
420-
if (servletContextName != null) {
421-
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName +
422-
"." + getServletName());
435+
if (this.contextId != null) {
436+
wac.setId(this.contextId);
437+
}
438+
else {
439+
// Generate default id...
440+
ServletContext sc = getServletContext();
441+
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
442+
// Servlet <= 2.4: resort to name specified in web.xml, if any.
443+
String servletContextName = sc.getServletContextName();
444+
if (servletContextName != null) {
445+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName +
446+
"." + getServletName());
447+
}
448+
else {
449+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName());
450+
}
423451
}
424452
else {
425-
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName());
453+
// Servlet 2.5's getContextPath available!
454+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
455+
ObjectUtils.getDisplayString(sc.getContextPath()) + "/" + getServletName());
426456
}
427457
}
428-
else {
429-
// Servlet 2.5's getContextPath available!
430-
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath() +
431-
"/" + getServletName());
432-
}
433458

434459
wac.setParent(parent);
435460
wac.setServletContext(getServletContext());

org.springframework.web/src/main/java/org/springframework/web/context/ContextLoader.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,18 @@
7777
public class ContextLoader {
7878

7979
/**
80-
* Config param for the root WebApplicationContext implementation class to
81-
* use: "<code>contextClass</code>"
80+
* Config param for the root WebApplicationContext implementation class to use:
81+
* "<code>contextClass</code>"
8282
*/
8383
public static final String CONTEXT_CLASS_PARAM = "contextClass";
8484

85+
/**
86+
* Config param for the root WebApplicationContext id,
87+
* to be used as serialization id for the underlying BeanFactory:
88+
* "<code>contextId</code>"
89+
*/
90+
public static final String CONTEXT_ID_PARAM = "contextId";
91+
8592
/**
8693
* Name of servlet context parameter (i.e., "<code>contextConfigLocation</code>")
8794
* that can specify the config location for the root context, falling back
@@ -251,21 +258,20 @@ protected WebApplicationContext createWebApplicationContext(ServletContext sc, A
251258
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
252259

253260
// Assign the best possible id value.
254-
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
255-
// Servlet <= 2.4: resort to name specified in web.xml, if any.
256-
String servletContextName = sc.getServletContextName();
257-
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
258-
ObjectUtils.getDisplayString(servletContextName));
261+
String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
262+
if (idParam != null) {
263+
wac.setId(idParam);
259264
}
260265
else {
261-
// Servlet 2.5's getContextPath available!
262-
try {
263-
String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(sc);
266+
// Generate default id...
267+
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
268+
// Servlet <= 2.4: resort to name specified in web.xml, if any.
264269
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
265-
ObjectUtils.getDisplayString(contextPath));
270+
ObjectUtils.getDisplayString(sc.getServletContextName()));
266271
}
267-
catch (Exception ex) {
268-
throw new IllegalStateException("Failed to invoke Servlet 2.5 getContextPath method", ex);
272+
else {
273+
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
274+
ObjectUtils.getDisplayString(sc.getContextPath()));
269275
}
270276
}
271277

0 commit comments

Comments
 (0)