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 OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

javacv依赖太大导致jar包也大的解决办法

《javacv依赖太大导致jar包也大的解决办法》随着项目的复杂度和依赖关系的增加,打包后的JAR包可能会变得很大,:本文主要介绍javacv依赖太大导致jar包也大的解决办法,文中通过代码介绍的... 目录前言1.检查依赖2.更改依赖3.检查副依赖总结 前言最近在写项目时,用到了Javacv里的获取视频

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja