1、servlet方式加载时,
【web.xml】

spring容器放在ServletContext中的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC
注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。
Java代码
- ServletContext sc=略
- WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
2、listener方式加载时:
【web.xml】

【jsp/servlet】可以这样取得
Java代码
- ServletContext context = getServletContext();
- WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context);
3、通用的方法来了,神器啊,前的 1、2两种方法并不通用,可以抛弃了。
在配置文件中加入:

Java代码


本文详细介绍了在Spring MVC框架中,如何通过servlet、listener以及一种通用方法来获取WebApplicationContext。具体包括在web.xml中配置servlet-name,使用ServletContext获取对应属性,以及通过WebApplicationContextUtils工具类获取应用上下文。
566

被折叠的 条评论
为什么被折叠?



