|
33 | 33 | import org.springframework.context.i18n.LocaleContext;
|
34 | 34 | import org.springframework.context.i18n.LocaleContextHolder;
|
35 | 35 | import org.springframework.context.i18n.SimpleLocaleContext;
|
| 36 | +import org.springframework.util.ObjectUtils; |
36 | 37 | import org.springframework.web.context.ConfigurableWebApplicationContext;
|
37 | 38 | import org.springframework.web.context.WebApplicationContext;
|
38 | 39 | import org.springframework.web.context.request.RequestAttributes;
|
@@ -121,6 +122,9 @@ public abstract class FrameworkServlet extends HttpServletBean {
|
121 | 122 | /** WebApplicationContext implementation class to create */
|
122 | 123 | private Class contextClass = DEFAULT_CONTEXT_CLASS;
|
123 | 124 |
|
| 125 | + /** WebApplicationContext id to assign */ |
| 126 | + private String contextId; |
| 127 | + |
124 | 128 | /** Namespace for this servlet */
|
125 | 129 | private String namespace;
|
126 | 130 |
|
@@ -185,6 +189,21 @@ public Class getContextClass() {
|
185 | 189 | return this.contextClass;
|
186 | 190 | }
|
187 | 191 |
|
| 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 | + |
188 | 207 | /**
|
189 | 208 | * Set a custom namespace for this servlet,
|
190 | 209 | * to be used for building a default context config location.
|
@@ -413,23 +432,29 @@ protected WebApplicationContext createWebApplicationContext(ApplicationContext p
|
413 | 432 | (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
|
414 | 433 |
|
415 | 434 | // 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 | + } |
423 | 451 | }
|
424 | 452 | 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()); |
426 | 456 | }
|
427 | 457 | }
|
428 |
| - else { |
429 |
| - // Servlet 2.5's getContextPath available! |
430 |
| - wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + sc.getContextPath() + |
431 |
| - "/" + getServletName()); |
432 |
| - } |
433 | 458 |
|
434 | 459 | wac.setParent(parent);
|
435 | 460 | wac.setServletContext(getServletContext());
|
|
0 commit comments