[英]spring DispatcherServlet context inheritance
Typically there is one ApplicationContext
(parent) and 0..n DispatcherServlets
(children). 通常有一个
ApplicationContext
(父)和0..n DispatcherServlets
(子)。 Is it also possible to have a DispatcherServlet
that has another DispatcherServle
t as parent context which has the ApplicationContext
as parent? 是否也可以让
DispatcherServlet
具有另一个DispatcherServle
作为父上下文 ,其中ApplicationContext
为父项? As I understood, beans can be resolved transitively so it should be possible to access the application context. 据我所知,bean可以通过传递方式解决,因此应该可以访问应用程序上下文。
I don't want to put the shared beans into the ApplicationContext
because they must not be exposed to other DispatcherServlet
- with one exception. 我不想将共享bean放入
ApplicationContext
因为它们不能暴露给其他DispatcherServlet
- 只有一个例外。
From HttpServletBean
and FrameworkServlet
it looks like you can do the following to make bar
use context of foo
as its own: 从
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>
I extended the DispatcherServlet. 我扩展了DispatcherServlet。 Now it works perfectly!
现在它完美无缺!
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.