3

通常有一个ApplicationContext(父)和 0..n DispatcherServlets(子)。是否也可以有一个DispatcherServlet有另一个DispatcherServlet 作为父上下文的父上下文ApplicationContext?据我了解,bean 可以传递解析,因此应该可以访问应用程序上下文。

我不想将共享 bean 放入其中,ApplicationContext因为它们不能暴露给其他DispatcherServlet- 除了一个例外。

4

2 回答 2

1

我扩展了 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;
    }
}
于 2012-01-18T20:32:23.940 回答
1

HttpServletBean并且FrameworkServlet看起来您可以执行以下操作以将bar上下文foo用作自己的:

<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>
于 2011-12-02T20:52:12.520 回答