3

通常、1つApplicationContext(親)と0..n DispatcherServlets(子)があります。親として別のtを持つaDispatcherServletDispatcherServleを持つことも可能ApplicationContextですか?私が理解したように、Beanは一時的に解決できるため、アプリケーションコンテキストにアクセスできるはずです。

ApplicationContext共有Beanを他のBeanに公開してはならないため、共有Beanを入れたくありませんDispatcherServlet。1つの例外があります。

4

2 に答える 2

1

からHttpServletBean、コンテキストを独自のものとして使用FrameworkServletするには、次のように実行できるようです。barfoo

<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 に答える
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 に答える