获取Spring ROOT ApplicationContext 的几种方式
2016-07-04 11:10
351 查看
在spring中我们经常需要获取ROOT ApplicationContext, 以下就几种获取方式作分析:
在使用spring的web项目中,在web.xml中有如下配置,进行spring的初始化:
进入该ContextLoaderListener,注意以下代码:
ContextLoaderListener在实现ServletContextListener接口的同时,也继承了ContextLoader,initWebApplicationContext是在ContextLoader中定义的,我们注意以下代码片段:
很明显,在context创建后,同时也保存到了servletContext中,因此这里有了第一种获取方式:
该方法的原理如下:
当不能够传入ServletContext时,怎么获取ApplicationContext呢,spring为我们提供了ApplicationContextAware接口,实现该接口的bean即可获得ApplicationContext对象。需要注意的是,该bean一定要在spring中定义过,是spring管理的bean,spring在初始化该bean的时候,会进行赋值。
第二种获取方式:
另外,在使用springMVC的时候,需要获得springMVC 的 ApplicationContext有如下方式:
该方法代码实现如下:
这里获取的就是DispatcherServlet ApplicationContext 。DispatcherServlet ApplicationContext 的双亲为ROOT ApplicationContext ,则可以在DispatcherServlet ApplicationContext中获取ROOT ApplicationContext中定义的bean,而在ROOT ApplicationContext无法获取DispatcherServlet ApplicationContext中定义的bean。
在使用spring的web项目中,在web.xml中有如下配置,进行spring的初始化:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
进入该ContextLoaderListener,注意以下代码:
@Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
ContextLoaderListener在实现ServletContextListener接口的同时,也继承了ContextLoader,initWebApplicationContext是在ContextLoader中定义的,我们注意以下代码片段:
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
很明显,在context创建后,同时也保存到了servletContext中,因此这里有了第一种获取方式:
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
该方法的原理如下:
public static WebApplicationContext getWebApplicationContext(ServletContext sc) { return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); } public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) { ····省略代码····· Object attr = sc.getAttribute(attrName); ····省略代码····· return (WebApplicationContext) attr; }
当不能够传入ServletContext时,怎么获取ApplicationContext呢,spring为我们提供了ApplicationContextAware接口,实现该接口的bean即可获得ApplicationContext对象。需要注意的是,该bean一定要在spring中定义过,是spring管理的bean,spring在初始化该bean的时候,会进行赋值。
第二种获取方式:
@Service public class SysScheduleJobService implements ApplicationContextAware{ private static Logger logger = LoggerFactory .getLogger(SysScheduleJobService.class); private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.applicationContext = applicationContext; } }
另外,在使用springMVC的时候,需要获得springMVC 的 ApplicationContext有如下方式:
ApplicationContext ctx = RequestContextUtils .findWebApplicationContext(request);
该方法代码实现如下:
public static WebApplicationContext findWebApplicationContext(HttpServletRequest request) { return findWebApplicationContext(request, request.getServletContext()); } public static WebApplicationContext findWebApplicationContext( HttpServletRequest request, ServletContext servletContext) { WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute( DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (webApplicationContext == null) { if (servletContext != null) { webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); } if (webApplicationContext == null) { webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); } } return webApplicationContext; }
这里获取的就是DispatcherServlet ApplicationContext 。DispatcherServlet ApplicationContext 的双亲为ROOT ApplicationContext ,则可以在DispatcherServlet ApplicationContext中获取ROOT ApplicationContext中定义的bean,而在ROOT ApplicationContext无法获取DispatcherServlet ApplicationContext中定义的bean。
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- 详解Java的MyBatis框架和Spring框架的整合运用
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao