Java 未加载Spring上下文
我正在尝试将我的一个旧项目从Spring3.0.3.RELEASE迁移到4.0.9.RELEASE。我将pom.xml依赖项更改为4.0.9,从那时起我就面临这个问题。DispatcherServlet无法在应用程序上下文中找到bean 以下是我的档案 web.xmlJava 未加载Spring上下文,java,spring,applicationcontext,servletcontextlistener,Java,Spring,Applicationcontext,Servletcontextlistener,我正在尝试将我的一个旧项目从Spring3.0.3.RELEASE迁移到4.0.9.RELEASE。我将pom.xml依赖项更改为4.0.9,从那时起我就面临这个问题。DispatcherServlet无法在应用程序上下文中找到bean 以下是我的档案 web.xml <context-param> <param-name>bootstrapConfig</param-name> <param-value> /com
<context-param>
<param-name>bootstrapConfig</param-name>
<param-value>
/com/core/choreographer/bootstrap/bootstrap-config.xml
</param-value>
</context-param>
<listener>
<listener-class>
com.core.bootstrap.BootstrapManager
</listener-class>
</listener>
<listener>
<listener-class>com.spring.http.SpringHttpContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>http-remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/remote/springhttp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>http-remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
这是ContextLoader
public class SpringHttpContextLoader extends ContextLoader {
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException {
SpringObjectFactory fact = BaseSpringObjectFactory.getInstance();
return fact.getApplicationContext();
}
}
在Spring3.0.3中,这一切都非常有效。当我试图将所有Jar替换为4.0.9时。springhttp-servlet.xml中定义的bean找不到bootstrap-config.xml中定义的bean
非常感谢您的任何建议。我在过去的2-3周中一直处于这种状态。我发现了
SpringHttpContextLoaderListener的问题。它覆盖了一个方法createContextLoader
和getContextLoader
,该方法在Spring版本3中被弃用,因此在Spring版本4中被删除。因此,自定义上下文未作为父上下文加载
public class SpringHttpContextLoaderListener extends ContextLoaderListener {
private ContextLoader loader;
protected ContextLoader createContextLoader() {
loader = new SpringHttpContextLoader();
return loader;
}
public ContextLoader getContextLoader(){
return loader;
}
}
解决方案
重写类SpringHttpContextLoaderListener中类ContextLoader
的loadParentContext
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException {
SpringObjectFactory fact = BaseSpringObjectFactory.getInstance();
return fact.getApplicationContext();
}
这将把自定义创建的上下文设置为父上下文,并且如果您要迁移到spring 4,则所有子上下文都将可用。AbstractAnnotationConfigDispatchersServletInitializer同时加载上下文spring和应用程序上下文 为什么要创建一个BootstrapManager
来将bean加载到spring上下文中?使用spring inbulid类,您应该尝试在日志中查找(通过增加org.springframework
debug level)框架加载的应用程序上下文。从症状来看,我假设Spring框架没有找到由BootstrapManager
创建的根上下文,而是创建了另一个根上下文(空)并将其用作dispatcherServlet one的根。@Rembo这是一个遗留应用程序,我刚刚得到这个项目进行转换。@SergeBallesta我知道dispatcherServlet没有获得BootstrapManager创建的上下文。我将如何创建某种桥梁?如果你不知道BoostrapManager是什么和(它不是什么),很难猜测。它是否正确地将新创建的ApplicationContext安装为ServletContext属性?
public class SpringHttpContextLoaderListener extends ContextLoaderListener {
private ContextLoader loader;
protected ContextLoader createContextLoader() {
loader = new SpringHttpContextLoader();
return loader;
}
public ContextLoader getContextLoader(){
return loader;
}
}
protected ApplicationContext loadParentContext(ServletContext servletContext)
throws BeansException {
SpringObjectFactory fact = BaseSpringObjectFactory.getInstance();
return fact.getApplicationContext();
}