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.