SpringBoot開發(fā)中的組件和容器詳解
Web原生組件
Web原生組件包括: Servlet、Filter、Listener等
相關(guān)使用
// 指定原生Servlet組件都放在那里 @ServletComponentScan(basePackages = "com.cf.admin") // 效果:直接響應(yīng),沒有經(jīng)過Spring的攔截器 @WebServlet(urlPatterns = "/my") @WebFilter(urlPatterns={"/css/*","/images/*"}) @WebListener
關(guān)于DispatchServlet 如何注冊(cè):
- 容器中自動(dòng)配置了 DispatcherServlet 屬性綁定到 WebMvcProperties;對(duì)應(yīng)的配置文件配置項(xiàng)是 spring.mvc
- 通過 ServletRegistrationBean <DispatcherServlet> 把 DispatcherServlet 配置進(jìn)來
- 默認(rèn)映射的是 / 路徑
Tomcat-Servlet中 多個(gè)Servlet都能處理到同一層路徑,精確優(yōu)選原則
- /my/
- /my/1
RegistrationBean使用
ServletRegistrationBean , FilterRegistrationBean , and ServletListenerRegistrationBean
@Configuration public class MyRegistConfig { @Bean public ServletRegistrationBean myServlet(){ MyServlet myServlet = new MyServlet(); return new ServletRegistrationBean(myServlet,"/my","/my02"); } @Bean public FilterRegistrationBean myFilter(){ MyFilter myFilter = new MyFilter(); // return new FilterRegistrationBean(myFilter,myServlet()); FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter); filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*")); return filterRegistrationBean; } @Bean public ServletListenerRegistrationBean myListener(){ MySwervletContextListener mySwervletContextListener = new MySwervletContextListener(); return new ServletListenerRegistrationBean(mySwervletContextListener); } }
Servlet容器
切換Servlet
- Spring Boot默認(rèn)支持的webServer容器:
- Tomcat , Jetty , or Undertow ServletWebServerApplicationContext 容器啟動(dòng)尋找ServletWebServerFactory 并引導(dǎo)創(chuàng)建服務(wù)器
相關(guān)依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
使用原理:
- SpringBoot應(yīng)用啟動(dòng)發(fā)現(xiàn)當(dāng)前是Web應(yīng)用。web場(chǎng)景包-導(dǎo)入tomcat
- web應(yīng)用會(huì)創(chuàng)建一個(gè)web版的ioc容器 ServletWebServerApplicationContext
- ServletWebServerApplicationContext 啟動(dòng)的時(shí)候?qū)ふ?ServletWebServerFactory(Servlet 的web服務(wù)器工廠—> Servlet 的web服務(wù)器)
- SpringBoot底層默認(rèn)有很多的WebServer工廠;TomcatServletWebServerFactory, JettyServletWebServerFactory, or UndertowServletWebServerFactory
- 底層直接會(huì)有一個(gè)自動(dòng)配置類。ServletWebServerFactoryAutoConfiguration
- ServletWebServerFactoryAutoConfiguration導(dǎo)入了ServletWebServerFactoryConfiguration(配置類)
- ServletWebServerFactoryConfiguration 配置類 根據(jù)動(dòng)態(tài)判斷系統(tǒng)中到底導(dǎo)入了那個(gè)Web服務(wù)器的包。(默認(rèn)是web-starter導(dǎo)入tomcat包),容器中就有 TomcatServletWebServerFactory
- TomcatServletWebServerFactory 創(chuàng)建出Tomcat服務(wù)器并啟動(dòng);TomcatWebServer 的構(gòu)造器擁有初始化方法initialize---this.tomcat.start();
- 內(nèi)嵌服務(wù)器,就是手動(dòng)把啟動(dòng)服務(wù)器的代碼調(diào)用(tomcat核心jar包存在)
定制Servlet容器
1 實(shí)現(xiàn) WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> 把配置文件的值和ServletWebServerFactory 進(jìn)行綁定
2 修改配置文件servce.xxx, 直接自定義 ConfigurableServletWebServerFactory
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.stereotype.Component; @Component public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(9000); } }
到此這篇關(guān)于SpringBoot開發(fā)中的組件和容器詳解的文章就介紹到這了,更多相關(guān)SpringBoot組件和容器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+Vue項(xiàng)目部署上線的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot+Vue項(xiàng)目部署上線的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02如何解決SpringBoot定時(shí)任務(wù)報(bào)錯(cuò)Unexpected error occurred 
這篇文章主要介紹了如何解決SpringBoot定時(shí)任務(wù)報(bào)錯(cuò)Unexpected error occurred in scheduled task問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08解決RabbitMq消息隊(duì)列Qos?Prefetch消息堵塞問題
這篇文章主要為大家介紹了關(guān)于如何解決解決RabbitMq?Qos?Prefetch消息堵塞的問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01Windows7下的Java運(yùn)行環(huán)境搭建過程圖解
這篇文章主要介紹了Windows7下的Java運(yùn)行環(huán)境搭建過程圖解,需要的朋友可以參考下2014-04-04一篇文章教帶你了解Java Spring之自動(dòng)裝配
今天小編就為大家分享一篇關(guān)于Spring中的自動(dòng)裝配,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2021-09-09java同一個(gè)類中,一個(gè)無事務(wù)方法調(diào)用一個(gè)有事務(wù)方法時(shí),事務(wù)失效問題
本文詳細(xì)介紹了Spring框架中事務(wù)管理的實(shí)現(xiàn)原理,包括@Transactional注解的使用、事務(wù)的開啟、提交和回滾機(jī)制,以及代理對(duì)象的兩種實(shí)現(xiàn)方式(JDK動(dòng)態(tài)代理和CGLIB代理),文章還探討了在同一個(gè)類中調(diào)用有事務(wù)方法時(shí)事務(wù)失效的原因,并提供了解決方法2024-12-12Java中轉(zhuǎn)換器設(shè)計(jì)模式深入講解
這篇文章主要給大家介紹了關(guān)于Java中轉(zhuǎn)換器設(shè)計(jì)模式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04