0

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.

Tõnis Ojandu
  • 3,486
  • 4
  • 20
  • 28
  • Tapestry? I haven't heard of that in ten years. Didn't know anyone was still using, or ever used, besides Howard Shipp. – duffymo Dec 01 '14 at 11:30
  • Unfortunately there are systems out there using Tapestry. I have had the 'pleasure' of working on 2 of them. – Alan Hay Dec 01 '14 at 12:35

1 Answers1

0

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?

Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • Sadly I can't just add the parent beans to web application cotext since the whole point of this design is to share some of the beans and configurations over multiple different web applications. – Tõnis Ojandu Dec 02 '14 at 06:26