聊聊spring.mvc.servlet.load-on-startup

本文主要研究一下spring.mvc.servlet.load-on-startup

spring.mvc.servlet.load-on-startup

org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java

@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {//......private final Servlet servlet = new Servlet();public static class Servlet {/*** Path of the dispatcher servlet.*/private String path = "/";/*** Load on startup priority of the dispatcher servlet.*/private int loadOnStartup = -1;public String getPath() {return this.path;}public void setPath(String path) {Assert.notNull(path, "Path must not be null");Assert.isTrue(!path.contains("*"), "Path must not contain wildcards");this.path = path;}public int getLoadOnStartup() {return this.loadOnStartup;}public void setLoadOnStartup(int loadOnStartup) {this.loadOnStartup = loadOnStartup;}public String getServletMapping() {if (this.path.equals("") || this.path.equals("/")) {return "/";}if (this.path.endsWith("/")) {return this.path + "*";}return this.path + "/*";}public String getPath(String path) {String prefix = getServletPrefix();if (!path.startsWith("/")) {path = "/" + path;}return prefix + path;}public String getServletPrefix() {String result = this.path;int index = result.indexOf('*');if (index != -1) {result = result.substring(0, index);}if (result.endsWith("/")) {result = result.substring(0, result.length() - 1);}return result;}}	
}

WebMvcProperties.Servlet定義了loadOnStartup屬性,默認為-1

DispatcherServletAutoConfiguration

org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
public class DispatcherServletAutoConfiguration {//......@Configuration(proxyBeanMethods = false)@Conditional(DispatcherServletRegistrationCondition.class)@ConditionalOnClass(ServletRegistration.class)@EnableConfigurationProperties(WebMvcProperties.class)@Import(DispatcherServletConfiguration.class)protected static class DispatcherServletRegistrationConfiguration {@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,webMvcProperties.getServlet().getPath());registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());multipartConfig.ifAvailable(registration::setMultipartConfig);return registration;}}//......
}	

DispatcherServletRegistrationConfiguration注冊了DispatcherServletRegistrationBean,它會讀取webMvcProperties.getServlet().getLoadOnStartup()然后設置其loadOnStartup屬性

ServletRegistrationBean

org/springframework/boot/web/servlet/ServletRegistrationBean.java

public class ServletRegistrationBean<T extends Servlet> extends DynamicRegistrationBean<ServletRegistration.Dynamic> {private static final String[] DEFAULT_MAPPINGS = { "/*" };private T servlet;private Set<String> urlMappings = new LinkedHashSet<>();private boolean alwaysMapUrl = true;private int loadOnStartup = -1;private MultipartConfigElement multipartConfig;//......@Overrideprotected void configure(ServletRegistration.Dynamic registration) {super.configure(registration);String[] urlMapping = StringUtils.toStringArray(this.urlMappings);if (urlMapping.length == 0 && this.alwaysMapUrl) {urlMapping = DEFAULT_MAPPINGS;}if (!ObjectUtils.isEmpty(urlMapping)) {registration.addMapping(urlMapping);}registration.setLoadOnStartup(this.loadOnStartup);if (this.multipartConfig != null) {registration.setMultipartConfig(this.multipartConfig);}}}	

ServletRegistrationBean定義了loadOnStartup屬性,默認為-1,其configure方法會設置loadOnStartup到ServletRegistration.Dynamic

StandardWrapper

org/apache/catalina/core/StandardWrapper.java

    /*** Set the load-on-startup order value (negative value means* load on first call).** @param value New load-on-startup value*/@Overridepublic void setLoadOnStartup(int value) {int oldLoadOnStartup = this.loadOnStartup;this.loadOnStartup = value;support.firePropertyChange("loadOnStartup",Integer.valueOf(oldLoadOnStartup),Integer.valueOf(this.loadOnStartup));}/*** @return the load-on-startup order value (negative value means* load on first call).*/@Overridepublic int getLoadOnStartup() {if (isJspServlet && loadOnStartup < 0) {/** JspServlet must always be preloaded, because its instance is* used during registerJMX (when registering the JSP* monitoring mbean)*/return Integer.MAX_VALUE;} else {return this.loadOnStartup;}}    

loadOnStartup屬性最后設置到了tomcat的StandardWrapper

StandardContext

org/apache/catalina/core/StandardContext.java

    /*** Load and initialize all servlets marked "load on startup" in the* web application deployment descriptor.** @param children Array of wrappers for all currently defined*  servlets (including those not declared load on startup)* @return <code>true</code> if load on startup was considered successful*/public boolean loadOnStartup(Container children[]) {// Collect "load on startup" servlets that need to be initializedTreeMap<Integer, ArrayList<Wrapper>> map = new TreeMap<>();for (Container child : children) {Wrapper wrapper = (Wrapper) child;int loadOnStartup = wrapper.getLoadOnStartup();if (loadOnStartup < 0) {continue;}Integer key = Integer.valueOf(loadOnStartup);ArrayList<Wrapper> list = map.get(key);if (list == null) {list = new ArrayList<>();map.put(key, list);}list.add(wrapper);}// Load the collected "load on startup" servletsfor (ArrayList<Wrapper> list : map.values()) {for (Wrapper wrapper : list) {try {wrapper.load();} catch (ServletException e) {getLogger().error(sm.getString("standardContext.loadOnStartup.loadException",getName(), wrapper.getName()), StandardWrapper.getRootCause(e));// NOTE: load errors (including a servlet that throws// UnavailableException from the init() method) are NOT// fatal to application startup// unless failCtxIfServletStartFails="true" is specifiedif(getComputedFailCtxIfServletStartFails()) {return false;}}}}return true;}

StandardContext的loadOnStartup方法會取出所有loadOnStartup大于等于0的wrapper,按loadOnStartup值放入到TreeMap<Integer, ArrayList<Wrapper>>,然后遍歷該TreeMap挨個執行wrapper.load()進行加載

小結

springboot的spring.mvc.servlet.load-on-startup屬性,最后設置到tomcat的StandardWrapper;而tomcat的StandardContext的loadOnStartup方法會取出所有loadOnStartup大于等于0的wrapper,按loadOnStartup值放入到TreeMap<Integer, ArrayList<Wrapper>>,然后遍歷該TreeMap挨個執行wrapper.load()進行加載。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/210703.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/210703.shtml
英文地址,請注明出處:http://en.pswp.cn/news/210703.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

json精講

本文介紹json的規范及javascript和java對數據的交換讀取 1. json介紹1.1 json簡介1.2為什么使用 JSON&#xff1f; 2. json規范2.1基礎規范2.2 key值為-字符串、數字、布爾值2.3 key值為對象Object2.4 key值為數組2.5 json本身就是一個數組 3.javascript操作json3.1 javascript…

WPF(Windows Presentation Foundation) 的 Menu控件

WPF&#xff08;Windows Presentation Foundation&#xff09;的 Menu 是一種用于創建菜單的控件。菜單通常位于應用程序窗口的頂部&#xff0c;并提供了一組命令或選項&#xff0c;用于導航到不同的功能區域、執行特定的操作或訪問特定的功能。 Menu 控件是 WPF 中的一個容器…

2、關于使用ajax驗證繞過(實例2)

ajax原理我上一篇有寫過&#xff0c;參考&#xff1a;1、關于前端js-ajax繞過-CSDN博客 一、實例環境&#xff1a; 為手機上的某一割韭菜app 二、目的&#xff1a; 實現繞過手機驗證碼&#xff0c;找回密碼 三、工具&#xff1a; bp代理 四、驗證步驟如下&#xff1a; …

ECU安全學習網站和書籍介紹

ECU安全是指關注和保護汽車電子控制單元&#xff08;ECU&#xff09;的安全性和防護措施。ECU是現代汽車中的關鍵組件&#xff0c;它負責監控和控制車輛各種系統的運行&#xff0c;如發動機、制動、轉向等。ECU安全的重要性在于防止惡意攻擊者操控或干擾車輛的操作。 ECU安全涉…

hive自定義函數及案例

一.自定義函數 1.Hive自帶了一些函數&#xff0c;比如&#xff1a;max/min等&#xff0c;但是數量有限&#xff0c;自己可以通過自定義UDF來方便的擴展。 2.當Hive提供的內置函數無法滿足你的業務處理需要時&#xff0c;此時就可以考慮使用用戶自定義函數。 3.根據用戶自定義…

GitHub為Rust語言添加了供應鏈安全工具

GitHub的供應鏈安全特性包括咨詢數據庫、Dependabot警報和依賴關系圖現在可以用于Rust Cargo文件。 為了幫助Rust開發人員發現和防止安全漏洞&#xff0c;GitHub已經為快速增長的Rust語言提供了供應鏈安全特性套件。 這些特性包括GitHub Advisory Database&#xff0c;它已經有…

構建外賣系統:使用Django框架

在當今數字化的時代&#xff0c;外賣系統的搭建不再是什么復雜的任務。通過使用Django框架&#xff0c;我們可以迅速建立一個強大、靈活且易于擴展的外賣系統。本文將演示如何使用Django構建一個簡單的外賣系統&#xff0c;并包含一些基本的技術代碼。 步驟一&#xff1a;安裝…

shell的條件測試

shell 的條件測試 概述 條件測試是 shell 編程中非常重要的一個概念&#xff0c;它允許我們根據某個條件是否滿足&#xff0c;來選擇執行相應的任務。 條件測試的語法 shell 中的條件測試語法如下&#xff1a; [ 條件表達式 ]如果條件表達式為真&#xff0c;則返回 0&…

CentOS 7.9--離線安裝python3.9.18+virtualenv-20.25.0

# 想在centos6.x 上安裝新版本的python&#xff0c;但是擔心在用系統的環境被破壞&#xff0c;所以需要安裝python虛擬環境&#xff0c;然后就找到自用的aliyun主機先測試下離線安裝&#xff0c;在用6.X環境是沒有互聯網的&#xff0c;必須需要離線安裝。 1. 下載對應python源…

力扣解題之保姆教程:(1)兩數之和(代碼詳解)

題目描述 給定一個整數數組 nums 和一個整數目標值 target&#xff0c;請你在該數組中找出 和為目標值 target 的那 兩個 整數&#xff0c;并返回它們的數組下標。 假設每種輸入只會對應一個答案。但是&#xff0c;數組中同一個元素在答案里不能重復出現。你可以按任意順序返回…

Django模板

以下是一個簡單的Django模板示例&#xff1a; <!DOCTYPE html> <html><head><title>{{ title }}</title></head><body><h1>{{ heading }}</h1><p>{{ content }}</p></body> </html>一、模板的…

波奇學Linux:父子進程和進程狀態

vim編輯器&#xff0c;編寫一個程序模擬進程 在vim中查看sleep函數 底行模式輸入 寫個Makefile自動運行波奇學Linux:yum和vim-CSDN博客 運行程序 PID和PPID 查看進程目錄信息 實際有過濾出來有兩個&#xff0c;一個進程本身一個是grep程序&#xff0c;通過 -v grep過濾走含gre…

新版Android Studio 正則表達式匹配代碼注釋,刪除注釋,刪除全部注釋,IntelliJ IDEA 正則表達式匹配代碼注釋

正則表達式匹配代碼注釋 完整表達式拼接Android Studio 搜索匹配【IntelliJ IDEA 也是一樣的】 完整表達式拼接 (/*{1,2}[\s\S]?*/)|(//[\x{4e00}-\x{9fa5}].)|(<!-[\s\S]?–>)|(^\s\n)|(System.out.println.*) 表達式拆解&#xff0c;可以根據自己需求自由組合&#x…

Mybatis、Mybatis整合Spring的流程圖

Mybatis 注意MapperProxy里面有invoke方法&#xff0c;當進到invoker方法會拿到 二、mybatis整合Spring 1、當我們的拿到的【Dao】其實就是【MapperProxy】&#xff0c;執行Dao的方法時&#xff0c;會被MapperProxy的【Invoke方法攔截】 2、圖上已經標注了MapperProxy包含哪些…

力扣:200. 島嶼數量(Python3)

題目&#xff1a; 給你一個由 1&#xff08;陸地&#xff09;和 0&#xff08;水&#xff09;組成的的二維網格&#xff0c;請你計算網格中島嶼的數量。 島嶼總是被水包圍&#xff0c;并且每座島嶼只能由水平方向和/或豎直方向上相鄰的陸地連接形成。 此外&#xff0c;你可以假…

STM32-TIM定時器中斷

目錄 一、TIM&#xff08;Timer&#xff09;定時器簡介 二、定時器類型 2.1基本定時器結構 2.2通用定時器結構 2.3高級定時器結構 三、定時中斷基本結構 四、時序圖分析 4.1 預分頻器時序 4.2 計數器時序 4.3 計數器無預裝時序&#xff08;無影子寄存器&#xff09; …

C#的線程技術及操作

每個正在操作系統上運行的應用程序都是一個進程&#xff0c;一個進程可以包括一個或多個線程。線程是操作系統分配處理器時間的基本單元&#xff0c;在進程中可以有多個線程同時執行代碼。每個線程都維護異常處理程序、調度優先級和一組系統用于在調度該線程前保存線程上下文的…

PyQt6 水平布局Horizontal Layout (QHBoxLayout)

鋒哥原創的PyQt6視頻教程&#xff1a; 2024版 PyQt6 Python桌面開發 視頻教程(無廢話版) 玩命更新中~_嗶哩嗶哩_bilibili2024版 PyQt6 Python桌面開發 視頻教程(無廢話版) 玩命更新中~共計41條視頻&#xff0c;包括&#xff1a;2024版 PyQt6 Python桌面開發 視頻教程(無廢話版…

[足式機器人]Part2 Dr. CAN學習筆記-自動控制原理Ch1-1開環系統與閉環系統Open/Closed Loop System

本文僅供學習使用 本文參考&#xff1a; B站&#xff1a;DR_CAN Dr. CAN學習筆記-自動控制原理Ch1-1開環系統與閉環系統Open/Closed Loop System EG1: 燒水與控溫水壺EG2: 蓄水與最終水位閉環控制系統 EG1: 燒水與控溫水壺 EG2: 蓄水與最終水位 h ˙ q i n A ? g h A R \dot{…

阿里云SLS采集jvm日志

一、背景 java應用部署在阿里云的k8s容器里&#xff0c;采集其日志的需求則是一個不可缺少的。而不同公司的jvm日志會存在很大的差異&#xff0c;所以本文僅以我的實際情況作一個示例&#xff0c;僅供有需要采集jvm日志的同學們一個參考。 我們打印的Jvm日志格式見下&#xf…