[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.