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 如何注冊:
- 容器中自動配置了 DispatcherServlet 屬性綁定到 WebMvcProperties;對應(yīng)的配置文件配置項是 spring.mvc
- 通過 ServletRegistrationBean <DispatcherServlet> 把 DispatcherServlet 配置進(jìn)來
- 默認(rèn)映射的是 / 路徑
Tomcat-Servlet中 多個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 容器啟動尋找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)用啟動發(fā)現(xiàn)當(dāng)前是Web應(yīng)用。web場景包-導(dǎo)入tomcat
- web應(yīng)用會創(chuàng)建一個web版的ioc容器 ServletWebServerApplicationContext
- ServletWebServerApplicationContext 啟動的時候?qū)ふ?ServletWebServerFactory(Servlet 的web服務(wù)器工廠—> Servlet 的web服務(wù)器)
- SpringBoot底層默認(rèn)有很多的WebServer工廠;TomcatServletWebServerFactory, JettyServletWebServerFactory, or UndertowServletWebServerFactory
- 底層直接會有一個自動配置類。ServletWebServerFactoryAutoConfiguration
- ServletWebServerFactoryAutoConfiguration導(dǎo)入了ServletWebServerFactoryConfiguration(配置類)
- ServletWebServerFactoryConfiguration 配置類 根據(jù)動態(tài)判斷系統(tǒng)中到底導(dǎo)入了那個Web服務(wù)器的包。(默認(rèn)是web-starter導(dǎo)入tomcat包),容器中就有 TomcatServletWebServerFactory
- TomcatServletWebServerFactory 創(chuàng)建出Tomcat服務(wù)器并啟動;TomcatWebServer 的構(gòu)造器擁有初始化方法initialize---this.tomcat.start();
- 內(nèi)嵌服務(wù)器,就是手動把啟動服務(wù)器的代碼調(diào)用(tomcat核心jar包存在)
定制Servlet容器
1 實現(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在IntelliJ?IDEA中配置SSH服務(wù)器開發(fā)環(huán)境并實現(xiàn)固定地址遠(yuǎn)程連接的操作方法
本文主要介紹如何在IDEA中設(shè)置遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境,并結(jié)合Cpolar內(nèi)網(wǎng)穿透工具實現(xiàn)無公網(wǎng)遠(yuǎn)程連接,然后實現(xiàn)遠(yuǎn)程Linux環(huán)境進(jìn)行開發(fā),本例使用的是IDEA2023.2.5版本,感興趣的朋友跟隨小編一起看看吧2024-01-01
從0開始學(xué)習(xí)大數(shù)據(jù)之java spark編程入門與項目實踐
這篇文章主要介紹了從0開始學(xué)習(xí)大數(shù)據(jù)之java spark編程入門與項目實踐,結(jié)合具體入門項目分析了大數(shù)據(jù)java spark編程項目建立、調(diào)試、輸出等相關(guān)步驟及操作技巧,需要的朋友可以參考下2019-11-11
Spring擴(kuò)展BeanFactoryPostProcessor使用技巧詳解
這篇文章主要為大家介紹了Spring擴(kuò)展BeanFactoryPostProcessor使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Java批量插入數(shù)據(jù)的代碼實現(xiàn)
日常工作或者學(xué)習(xí)中,可能會遇到批量插入數(shù)據(jù)的需求,一般情況下數(shù)據(jù)量少的時候,我們會直接調(diào)用批量接口插入數(shù)據(jù)即可,當(dāng)數(shù)據(jù)量特別大時,我們就會用到分批插入數(shù)據(jù),所以本文給大家介紹了Java批量插入數(shù)據(jù)的代碼實現(xiàn),需要的朋友可以參考下2024-01-01

