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

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避