SpringBoot的內(nèi)嵌和外置tomcat的實(shí)現(xiàn)方式
1.內(nèi)嵌
如何定制和修改Servlet容器的相關(guān)配置
修改修改和server有關(guān)的配置:
server.port=8081 server.context-path=/tx server.tomcat.uri-encoding=UTF-8
配置了這個(gè)之后,重啟應(yīng)用:
訪(fǎng)問(wèn)地址變?yōu)椋篽ttp://localhost:8081/tx
所有URL都需要加上 /tx 前綴
注冊(cè)Servlet三大組件
由于SpringBoot默認(rèn)是以jar包的方式啟動(dòng)嵌入式的Servlet容器來(lái)啟動(dòng)SpringBoot的web應(yīng)用,沒(méi)有web.xml文件。
三大組件分別是:
Servlet服務(wù)器, Filter(過(guò)濾器)和 Listener(監(jiān)聽(tīng)器)

首先配置配置類(lèi)
package com.qcby.config;
import com.qcby.component.MyServlet;
import com.qcby.component.MyFilter;
import com.qcby.component.MyListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
@Configuration
//@Configuration:標(biāo)記這是一個(gè)Spring配置類(lèi),Spring會(huì)掃描其中的 @Bean 方法
//相當(dāng)于傳統(tǒng)的XML配置文件,但使用Java代碼配置
public class WebComponentConfig {
// 1. 注冊(cè)Servlet
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean =
new ServletRegistrationBean(new MyServlet(), "/myServlet");
return registrationBean;
}
// 2. 注冊(cè)Filter
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
return registrationBean;
}
// 3. 注冊(cè)Listener
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean registrationBean =
new ServletListenerRegistrationBean(new MyListener());
return registrationBean;
}
}Servlet注冊(cè)詳解
Spring調(diào)用 myServlet() 方法
創(chuàng)建 MyServlet 實(shí)例
創(chuàng)建 ServletRegistrationBean,將Servlet映射到 /myServlet 路徑
返回注冊(cè)Bean,Spring將其加入容器
效果:訪(fǎng)問(wèn) http://localhost:8080/myServlet 會(huì)調(diào)用 MyServlet
Filter注冊(cè)詳解
setFilter(new MyFilter()):設(shè)置過(guò)濾器實(shí)例
setUrlPatterns(Arrays.asList("/hello", "/myServlet")):指定攔截路徑
/hello:攔截該路徑
/myServlet:攔截該路徑
效果:訪(fǎng)問(wèn) /hello 或 /myServlet 時(shí),會(huì)先經(jīng)過(guò) MyFilter
Listener注冊(cè)詳解
不需要配置URL模式,自動(dòng)監(jiān)聽(tīng)?wèi)?yīng)用生命周期事件
運(yùn)行之后,訪(fǎng)問(wèn)localhost:8080/myServlet?????? (記得把之前的配置文件注釋掉)然后查看控制臺(tái)輸出:

2.外置
嵌入式Servlet容器:應(yīng)用打成可執(zhí)行的jar
優(yōu)點(diǎn):簡(jiǎn)單、便攜;
缺點(diǎn):默認(rèn)不支持JSP、優(yōu)化定制比較復(fù)雜.;
外置的Servlet容器:外面安裝Tomcat---應(yīng)用war包的方式打包;
創(chuàng)建一個(gè)新項(xiàng)目:Cloud Native App Initializer

創(chuàng)建好之后下載壓縮包,解壓到idea目錄里邊
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qcby</groupId>
<artifactId>TomcatDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>TomcatDemo</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


點(diǎn)擊ok

我們這邊的webapp和web.xml文件就創(chuàng)建好了

然后我們就可以和之前的ssm項(xiàng)目一樣配置tomcat了:


如圖

ServletInitializer的流程:
外置Tomcat啟動(dòng)→發(fā)現(xiàn) ServletInitializer (繼承自 SpringBootServletInitializer)→調(diào)用 configure() 方法→啟動(dòng) Spring Boot 主類(lèi) (Application.class)→Spring Boot 應(yīng)用正常運(yùn)行
package com.qcby.TomcatDemo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TomcatDemoApplication.class);
}
}
配置好之后啟動(dòng):


原理
jar包:執(zhí)行SpringBoot主類(lèi)的main方法,啟動(dòng)ioc容器,創(chuàng)建嵌入式的Servlet容器;
war包:?jiǎn)?dòng)服務(wù)器,服務(wù)器啟動(dòng)SpringBoot應(yīng)用【SpringBootServletInitializer】,啟動(dòng)ioc容器;
到此這篇關(guān)于SpringBoot的內(nèi)嵌和外置tomcat的實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)SpringBoot內(nèi)嵌和外置tomcat內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Spring中@Value注解使用和源碼分析
通過(guò)深入分析@Value注解的使用和源碼,本文詳細(xì)解釋了Spring如何解析@Value注解并為屬性賦值,首先,Spring會(huì)解析并收集所有被@Value注解修飾的屬性,這一過(guò)程依賴(lài)于AutowiredAnnotationBeanPostProcessor類(lèi)2024-11-11
spring boot aop 記錄方法執(zhí)行時(shí)間代碼示例
這篇文章主要介紹了spring boot aop 記錄方法執(zhí)行時(shí)間代碼示例,分享了相關(guān)代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Mybatis?Plus?中的LambdaQueryWrapper示例詳解
這篇文章主要介紹了Mybatis?Plus?中的LambdaQueryWrapper,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟
Spring Boot與Prometheus的整合可以實(shí)現(xiàn)對(duì)Spring Boot應(yīng)用的實(shí)時(shí)監(jiān)控,有助于更好地維護(hù)應(yīng)用的性能,本文給大家介紹springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟,感興趣的朋友跟隨小編一起看看吧2024-11-11
idea啟動(dòng)項(xiàng)目報(bào)端口號(hào)沖突或被占用的解決方法
這篇文章主要介紹了idea啟動(dòng)項(xiàng)目報(bào)端口號(hào)沖突或被占用的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
SpringMVC的處理器攔截器HandlerInterceptor詳解
這篇文章主要介紹了SpringMVC的處理器攔截器HandlerInterceptor詳解,SpringWebMVC的處理器攔截器,類(lèi)似于Servlet開(kāi)發(fā)中的過(guò)濾器Filter,用于處理器進(jìn)行預(yù)處理和后處理,需要的朋友可以參考下2024-01-01
SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Spring-Data-Jpa實(shí)現(xiàn)CRUD操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
jdbc連SQL?server顯示1433端口連接失敗圖文解決方法
這篇文章主要給大家介紹了關(guān)于jdbc連SQL?server顯示1433端口連接失敗的圖文解決方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06

