WebApplicationContext返回null获取不了
项目使用的是ssh框架,applicationContext.xml放在/WEB-INF/classes下,web的配置如下:
<servlet>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>PageServlet</servlet-name>
<display-name>PageServlet</display-name>
<description></description>
<servlet-class>com.schoolshape.web.servlet.PageServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
在servlet类有如下代码:
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
System.out.println("webApplicationContext: "+webApplicationContext);
通过服务器访问该servlet类会在控制台输出:
webApplicationContext: null
求解?如何才能获得webApplicationContext
------解决方案--------------------爱莫能助
友情支持
祝您好运
项目结项了,脑子不想动了
------解决方案--------------------
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
参考这个看看