简体   繁体   中英

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

I'm upgrading Spring version from 2.x to 4.x for a legacy application that runs as a FileSystem Xml Application context and initiates an embedded jetty server with WebApplicon context using beans from parent context.

In Spring 2 this is achieved via a Custom ContextLoaderListener that overrides the createContextLoader()

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;
            }
        };
    }

}

but createContextLoader has since then been depricated and for Spring 4 removed entirely. Now ContextLoaderListener extends ContextLoader itself. Therefore I can just directly:

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;
    }

}

By doing it this way I was able to initiate the beans in my WebApplicationContext but now when Filter.init(...) is called for my Tapestry filter then only the web app beans are provides. Those from parent FileSystem xml context are missing from the ServletContext. For all the filters (including Tapestry one) this returns only the webapp beans:

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 */
    }
}

And therefore my Tapestry webapp is unable to boot up.

I've been at this for quite some time. Any suggestions are extremely appreciated.

You should be able to force the other bean definition files to be loaded by specifying them as imports in your webapp context config.

<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

How to import spring-config.xml of one project into spring-config.xml of another project?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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