Spring获取WebApplicationContext为null解决方案

2014-11-24 10:26:46 · 作者: · 浏览: 0

在web.xml中配置Spring,配置如下


springServlet
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
/WEB-INF/applicationContext.xml

1

在Servlet中通过WebApplicationContextUtils.getWebApplicationContext(getServletContext())获取WebApplicationContext对象为null。这是由于除了配置DispatcherServlet,还需要配置ContextLoaderServlet,否则无法获取WebApplicationContext。配置方法如下,在web.xml中加入


context
org.springframework.web.context.ContextLoaderServlet
2