繁体   English   中英

春季2至4:Web应用程序上下文中缺少bean

[英]Spring 2 to 4: beans missing from web-app context

我正在将旧版应用程序的Spring版本从2.x升级到4.x,该旧版应用程序作为FileSystem Xml Application上下文运行,并使用来自父上下文的bean使用WebApplicon上下文启动嵌入式码头服务器。

在Spring 2中,这是通过覆盖createContextLoader()的自定义ContextLoaderListener实现的

public class CustomContextLoaderListener extends ContextLoaderListener implements ApplicationContextAware {

    private ApplicationContext parent;

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.parent = ctx;
    }

    @Override
    protected ContextLoader createContextLoader() {
        return new ContextLoader() {
            @Override
            protected ApplicationContext loadParentContext(ServletContext servletContext)
                    throws BeansException {
                return parent;
            }
        };
    }

}

但是从那时起,createContextLoader便已被删除,对于Spring 4则完全删除了。 现在,ContextLoaderListener扩展了ContextLoader本身。 因此,我可以直接:

public class CustomContextLoaderListener extends ContextLoaderListener implements ApplicationContextAware {

    private ApplicationContext parent;

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.parent = ctx;
    }

    @Override
    protected ApplicationContext loadParentContext(ServletContext servletContext)
            throws BeansException {
        return parent;
    }

}

通过这种方式,我能够在WebApplicationContext中启动Bean,但是现在当Tapestry筛选器调用Filter.init(...)时,仅提供Web应用程序Bean。 ServletContext中缺少来自父FileSystem xml上下文的内容。 对于所有过滤器(包括Tapestry 1),仅返回webapp bean:

public class SomeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext ctx = filterConfig.getServletContext();

        WebApplicationContext wctx = (WebApplicationContext) ctx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

        for(String b : wctx.getBeanDefinitionNames()) {
            System.out.println("BEAN DEF : " + b);

        }

        /* FILTER OWN CODE */ 
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        /* FILTER OWN CODE */
    }

    @Override
    public void destroy() {
        /* FILTER OWN CODE */
    }
}

因此,我的Tapestry Webapp无法启动。

我已经在这里待了很长时间了。 任何建议都非常感谢。

通过在webapp上下文配置中将其他bean定义文件指定为导入,您应该能够强制加载其他bean定义文件。

<import resource="classpath:my-other-config-1.xml" />
<import resource="file:/my-other-config-2.xml" />

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/resources.html

如何将一个项目的spring-config.xml导入另一个项目的spring-config.xml?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM