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)文章
java簡(jiǎn)單實(shí)現(xiàn)八叉樹圖像處理代碼示例
這篇文章主要介紹了java簡(jiǎn)單實(shí)現(xiàn)八叉樹圖像處理代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
在IntelliJ?IDEA中配置SSH服務(wù)器開發(fā)環(huán)境并實(shí)現(xiàn)固定地址遠(yuǎn)程連接的操作方法
本文主要介紹如何在IDEA中設(shè)置遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境,并結(jié)合Cpolar內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)無公網(wǎng)遠(yuǎn)程連接,然后實(shí)現(xiàn)遠(yuǎn)程Linux環(huán)境進(jìn)行開發(fā),本例使用的是IDEA2023.2.5版本,感興趣的朋友跟隨小編一起看看吧2024-01-01
從0開始學(xué)習(xí)大數(shù)據(jù)之java spark編程入門與項(xiàng)目實(shí)踐
這篇文章主要介紹了從0開始學(xué)習(xí)大數(shù)據(jù)之java spark編程入門與項(xiàng)目實(shí)踐,結(jié)合具體入門項(xiàng)目分析了大數(shù)據(jù)java spark編程項(xiàng)目建立、調(diào)試、輸出等相關(guān)步驟及操作技巧,需要的朋友可以參考下2019-11-11
Spring擴(kuò)展BeanFactoryPostProcessor使用技巧詳解
這篇文章主要為大家介紹了Spring擴(kuò)展BeanFactoryPostProcessor使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
IntelliJ IDEA快速查看某個(gè)類/接口的子類或父類
本文主要介紹了IntelliJ IDEA快速查看某個(gè)類/接口的子類或父類,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn)
日常工作或者學(xué)習(xí)中,可能會(huì)遇到批量插入數(shù)據(jù)的需求,一般情況下數(shù)據(jù)量少的時(shí)候,我們會(huì)直接調(diào)用批量接口插入數(shù)據(jù)即可,當(dāng)數(shù)據(jù)量特別大時(shí),我們就會(huì)用到分批插入數(shù)據(jù),所以本文給大家介紹了Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn),需要的朋友可以參考下2024-01-01

