一、servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图。ServletContext实例是经过 getServletContext()方法得到的,因为HttpServlet继承Servlet的关系,GenericServlet类和 HttpServlet类同时具备该方法。其实servletContext和application 是同样的,就至关于一个类建立了两个不一样名称的变量。 二、ActionContext是Action执行时的上下文,里面存放着Action在执行时须要用到的对象,咱们也称之为广义值栈。 访问其它的Web对象的值也是与此相似的,你经过ActionContext去访问的都是包装后的Map。 三、spring上下文:ApplicationContext 测试环境获取: 测试环境能够这样获取,从新加载spring环境获取到上下文,可是在web环境下不能这样获取,比较耗费资源。 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); web环境获取: WebApplicationContextUtils.getWebApplicationContext(servletContext); 三、经过Aware接口: Spring中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实例这些 Aware接口的Bean在被初始以后,能够取得一些相对应的资源,例如实例BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实例ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。 Bean取得BeanFactory、ApplicationContextAware的实例目的是什么,通常的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制,例如ApplicationContextAware提供了publishEvent()方法,能够支持基于Observer模式的事件传播机制。 @Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; // Spring应用上下文环境 /* * 实现了ApplicationContextAware 接口,必须实现该方法; *经过传递applicationContext参数初始化成员变量applicationContext */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) applicationContext.getBean(name); } } 注意:这个地方使用了Spring的注解@Component,若是不是使用annotation的方式,而是使用xml的方式管理Bean,记得写入配置文件 <bean id="springContextUtil" class="com.ecdatainfo.util.SpringContextUtil" singleton="true" /> springMvc 获取applicationContext import org.springframework.web.context.ContextLoader; applicationContext ac =ContextLoader.getCurrentWebApplicationContext(); struts2 获取applicationContex HttpServletRequest requet = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); applicationContext ac =WebApplicationContextUtils.getWebApplicationContext(requet.getServletContext());