Spring ContextLoaderListener源码分析 .

本文详细解析了Spring框架中WebApplicationContext的初始化过程,包括ContextLoaderListener的配置与工作原理,以及如何通过ServletContext获取WebApplicationContext。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/hz_blog/article/details/7688921

我们要自动装配ApplicationContext配置信息时候,首先在web.xml配置ContextLoaderListener,下面是部分源代码:

[java]  view plain copy
  1. public class ContextLoaderListener implements ServletContextListener {  
  2.   
  3.     private ContextLoader contextLoader;  
  4.   
  5.   
  6.     /** 
  7.      * Initialize the root web application context. 
  8.      */  
  9.     public void contextInitialized(ServletContextEvent event) {  
  10.         this.contextLoader = createContextLoader();  
  11.         this.contextLoader.initWebApplicationContext(event.getServletContext());//注意此处  
  12.     }  
  13.   
  14.     protected ContextLoader createContextLoader() {  
  15.         return new ContextLoader();  
  16.     }  
  17.   
  18.     public ContextLoader getContextLoader() {  
  19.         return this.contextLoader;  
  20.     }  
  21.   
  22.     public void contextDestroyed(ServletContextEvent event) {  
  23.         if (this.contextLoader != null) {  
  24.             this.contextLoader.closeWebApplicationContext(event.getServletContext());  
  25.         }  
  26.     }  
  27.   
  28. }  
我们主要了解下WebApplicationContext是怎么初始化出来的,首先contextInitialized(ServletContextEvent event)方法的this.contextLoader.initWebApplicationContext(event.getServletContext());进去之后我们看到

[java]  view plain copy
  1. public WebApplicationContext initWebApplicationContext(ServletContext servletContext)  
  2.         throws IllegalStateException, BeansException {  
  3.   
  4.     if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {  
  5.         throw new IllegalStateException(  
  6.                 "Cannot initialize context because there is already a root application context present - " +  
  7.                 "check whether you have multiple ContextLoader* definitions in your web.xml!");  
  8.     }  
  9.   
  10.     servletContext.log("Initializing Spring root WebApplicationContext");  
  11.     if (logger.isInfoEnabled()) {  
  12.         logger.info("Root WebApplicationContext: initialization started");  
  13.     }  
  14.     long startTime = System.currentTimeMillis();  
  15.   
  16.     try {  
  17.         // Determine parent for root web application context, if any.  
  18.         ApplicationContext parent = loadParentContext(servletContext);  
  19.   
  20.         // Store context in local instance variable, to guarantee that  
  21.         // it is available on ServletContext shutdown.  
  22.         this.context = createWebApplicationContext(servletContext, parent);  
  23.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);//注意此处  
  24.         currentContextPerThread.put(Thread.currentThread().getContextClassLoader(), this.context);  
  25.   
  26.         if (logger.isDebugEnabled()) {  
  27.             logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +  
  28.                     WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");  
  29.         }  
  30.         if (logger.isInfoEnabled()) {  
  31.             long elapsedTime = System.currentTimeMillis() - startTime;  
  32.             logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");  
  33.         }  
  34.   
  35.         return this.context;  
  36.     }  
  37.     catch (RuntimeException ex) {  
  38.         logger.error("Context initialization failed", ex);  
  39.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);  
  40.         throw ex;  
  41.     }  
  42.     catch (Error err) {  
  43.         logger.error("Context initialization failed", err);  
  44.         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);  
  45.         throw err;  
  46.     }  
  47. }  

注意注意此处部分,说明当我们要得到一个WebApplicationContext只需要在ServletContext中获取属性名为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的常量值就可以得到一个ApplicationContext,最终可以获取spring配置文件中任意一个Bean对象

eg:

[java]  view plain copy
  1. public String execute() throws Exception {  
  2.     // TODO Auto-generated method stub  
  3.     ApplicationContext conn=(ApplicationContext)ServletActionContext.getServletContext().getAttribute(WebApplicationContext.class.getName() + ".ROOT");  
  4.     System.out.println(conn.getBean("biz"));  
  5.     return SUCCESS;  
  6. }  

或者直接使用org.springframework.web.context.support.WebApplicationContextUtils这个抽象的工具类来获得WebApplicationContext,该类的方法

[java]  view plain copy
  1. public static WebApplicationContext getWebApplicationContext(ServletContext sc) {  
  2.     return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);  
  3. }  

还有更多方法就不多介绍了,自行查看这个类的源码。
内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值