Web容器启动时加载Spring分析

2024-06-24 08:38

本文主要是介绍Web容器启动时加载Spring分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在应用程序web.xml中做了以下配置信息时,当启动Web容器时就会自动加载Spring容器。

[java]  view plain copy
print ?
  1. <listener>  
  2.   
  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.   
  5. </listener>  

ContextLoaderListener类实现了javax.servlet.ServletContextListener接口并且继承了org.springframework.web.context.ContextLoader类。ServletContextListener事件类是Web容器的一部分,处理Web应用的Servlet上下文(context)的监听。实现ServletContextListener接口中的contextInitialized和contextDestroyed方法。当Web容器启动时会自动调用contextInitialized方法,进行初始化Spring Web应用程序上下文,主要加载web.xml中contextConfigLocation的配置文件;当Web容器关闭之前会调用contextDestroyed方法,进行销毁Spring Web应用程序上下文。ContextLoader类实现了Spring上下文初始化的工作,执行initWebApplicationContext方法返回WebApplicationContext。Spring实现的contextInitialized和contextDestroyed代码如下:
[java]  view plain copy
print ?
  1. public void contextInitialized(ServletContextEvent event) {  
  2.   
  3.        this.contextLoader = createContextLoader();  
  4.   
  5.        if (this.contextLoader ==null) {  
  6.   
  7.            this.contextLoader = this;  
  8.   
  9.        }  
  10.   
  11.     this.contextLoader.initWebApplicationContext(event.getServletContext());  
  12.   
  13.     }  
  14.   
  15.    
  16.   
  17. public void contextDestroyed(ServletContextEvent event) {  
  18.   
  19.        if (this.contextLoader !=null) {        
[java]  view plain copy
print ?
  1. this.contextLoader.closeWebApplicationContext(event.getServletContext());  
[java]  view plain copy
print ?
  1. }  
  2.   
  3.  ContextCleanupListener.cleanupAttributes(event.getServletContext());  
[java]  view plain copy
print ?
  1. }  
  2.    
Spring执行实现Spring上下文的方法
[java]  view plain copy
print ?
  1. public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {  
  2.   
  3.         if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) !=null) {  
  4.   
  5.             throw new IllegalStateException(  
  6.   
  7.                     "Cannot initialize context because there is already a root application context present - " +  
  8.   
  9.                     "check whether you have multiple ContextLoader* definitions in your web.xml!");  
  10.   
  11.         }  
  12.   
  13.    
  14.   
  15.         Log logger = LogFactory.getLog(ContextLoader.class);  
  16.   
  17.         servletContext.log("Initializing Spring root WebApplicationContext");  
  18.   
  19.         if (logger.isInfoEnabled()) {  
  20.   
  21.             logger.info("Root WebApplicationContext: initialization started");  
  22.   
  23.         }  
  24.   
  25.         long startTime = System.currentTimeMillis();  
  26.   
  27.    
  28.   
  29.         try {  
  30.   
  31.             // Store context in local instance variable, to guarantee that  
  32.   
  33.             // it is available on ServletContext shutdown.  
  34.   
  35.             if (this.context ==null) {  
  36.   
  37.                 this.context = createWebApplicationContext(servletContext);  
  38.   
  39.             }  
  40.   
  41.             if (this.context instanceof ConfigurableWebApplicationContext) {  
  42.   
  43.                 configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, servletContext);  
  44.   
  45.             }  
  46.   
  47.             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);  
  48.   
  49.    
  50.   
  51.             ClassLoader ccl = Thread.currentThread().getContextClassLoader();  
  52.   
  53.             if (ccl == ContextLoader.class.getClassLoader()) {  
  54.   
  55.                 currentContext = this.context;  
  56.   
  57.             }  
  58.   
  59.             else if (ccl != null) {  
  60.   
  61.                 currentContextPerThread.put(ccl, this.context);  
  62.   
  63.             }  
  64.   
  65.    
  66.   
  67.             if (logger.isDebugEnabled()) {  
  68.   
  69.                 logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +  
  70.   
  71.                         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");  
  72.   
  73.             }  
  74.   
  75.             if (logger.isInfoEnabled()) {  
  76.   
  77.                 long elapsedTime = System.currentTimeMillis() - startTime;  
  78.   
  79.                 logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");  
  80.   
  81.             }  
  82.   
  83.    
  84.   
  85.             return this.context;  
  86.   
  87.         }  
  88.   
  89.         catch (RuntimeException ex) {  
  90.   
  91.             logger.error("Context initialization failed", ex);  
  92.   
  93.             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);  
  94.   
  95.             throw ex;  
  96.   
  97.         }  
  98.   
  99.         catch (Error err) {  
  100.   
  101.             logger.error("Context initialization failed", err);  
  102.   
  103.             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);  
  104.   
  105.             throw err;  
  106.   
  107.         }  
  108.   
  109.     }  

这篇关于Web容器启动时加载Spring分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089652

相关文章

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一