[英]spring DispatcherServlet context inheritance
通常有一個ApplicationContext
(父)和0..n DispatcherServlets
(子)。 是否也可以讓DispatcherServlet
具有另一個DispatcherServle
作為父上下文 ,其中ApplicationContext
為父項? 據我所知,bean可以通過傳遞方式解決,因此應該可以訪問應用程序上下文。
我不想將共享bean放入ApplicationContext
因為它們不能暴露給其他DispatcherServlet
- 只有一個例外。
從HttpServletBean
和FrameworkServlet
看起來你可以執行以下操作來使用foo
bar
上下文作為它自己的:
<servlet>
<servlet-name>foo</servlet-name>
<servlet-class>...DispatcherServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>bar</servlet-name>
<servlet-class>...DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>foo-servlet</param-value>
</init-param>
</servlet>
我擴展了DispatcherServlet。 現在它完美無缺!
public class ConfigurableDispatcherServlet extends DispatcherServlet {
private String contextParent;
/**
* Initialize and publish the WebApplicationContext for this servlet.
* <p>
* Delegates to {@link #createWebApplicationContext} for actual creation of
* the context. Can be overridden in subclasses.
*
* @return the WebApplicationContext instance
* @see #setContextClass
* @see #setContextConfigLocation
*/
protected WebApplicationContext initWebApplicationContext() {
// No fixed context defined for this servlet - create a local one.
WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext(),
"org.springframework.web.servlet.FrameworkServlet.CONTEXT." + getContextParent());
WebApplicationContext wac = createWebApplicationContext(parent);
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute with name [" + attrName + "]");
}
if(this.logger.isInfoEnabled()) {
this.logger.info(getServletName() + " is a child of " + parent.getDisplayName());
}
return wac;
}
public String getContextParent() {
return contextParent;
}
public void setContextParent(String contextParent) {
this.contextParent = contextParent;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.