最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot的內(nèi)嵌和外置tomcat的實現(xiàn)方式

 更新時間:2025年11月30日 15:07:13   作者:222you  
本文主要介紹了在Spring Boot中定制和修改Servlet容器的配置,包括內(nèi)嵌式和外置式Servlet容器的配置方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.內(nèi)嵌

如何定制和修改Servlet容器的相關(guān)配置

修改修改和server有關(guān)的配置:

server.port=8081
server.context-path=/tx
server.tomcat.uri-encoding=UTF-8

配置了這個之后,重啟應用:

訪問地址變?yōu)椋篽ttp://localhost:8081/tx

所有URL都需要加上 /tx 前綴

注冊Servlet三大組件

由于SpringBoot默認是以jar包的方式啟動嵌入式的Servlet容器來啟動SpringBootweb應用,沒有web.xml文件。

三大組件分別是:

Servlet服務器, Filter(過濾器)和 Listener(監(jiān)聽器)

首先配置配置類

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:標記這是一個Spring配置類,Spring會掃描其中的 @Bean 方法
//相當于傳統(tǒng)的XML配置文件,但使用Java代碼配置
public class WebComponentConfig {

    // 1. 注冊Servlet
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean =
                new ServletRegistrationBean(new MyServlet(), "/myServlet");
        return registrationBean;
    }

    // 2. 注冊Filter
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
        return registrationBean;
    }

    // 3. 注冊Listener
    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean registrationBean =
                new ServletListenerRegistrationBean(new MyListener());
        return registrationBean;
    }
}

Servlet注冊詳解

Spring調(diào)用 myServlet() 方法

創(chuàng)建 MyServlet 實例

創(chuàng)建 ServletRegistrationBean,將Servlet映射到 /myServlet 路徑

返回注冊Bean,Spring將其加入容器

效果:訪問 http://localhost:8080/myServlet 會調(diào)用 MyServlet

Filter注冊詳解

setFilter(new MyFilter()):設(shè)置過濾器實例

setUrlPatterns(Arrays.asList("/hello", "/myServlet")):指定攔截路徑

/hello:攔截該路徑

/myServlet:攔截該路徑

效果:訪問 /hello 或 /myServlet 時,會先經(jīng)過 MyFilter

Listener注冊詳解

不需要配置URL模式,自動監(jiān)聽應用生命周期事件

運行之后,訪問localhost:8080/myServlet?????? (記得把之前的配置文件注釋掉)然后查看控制臺輸出:

2.外置

嵌入式Servlet容器:應用打成可執(zhí)行的jar

優(yōu)點:簡單、便攜;

缺點:默認不支持JSP、優(yōu)化定制比較復雜.;

外置的Servlet容器:外面安裝Tomcat---應用war包的方式打包;

創(chuà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>

點擊ok

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

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

如圖

ServletInitializer的流程:

外置Tomcat啟動→發(fā)現(xiàn) ServletInitializer (繼承自 SpringBootServletInitializer)→調(diào)用 configure() 方法→啟動 Spring Boot 主類 (Application.class)→Spring Boot 應用正常運行

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);
    }

}

配置好之后啟動:

原理

jar包:執(zhí)行SpringBoot主類的main方法,啟動ioc容器,創(chuàng)建嵌入式的Servlet容器;

war包:啟動服務器,服務器啟動SpringBoot應用【SpringBootServletInitializer】,啟動ioc容器;

到此這篇關(guān)于SpringBoot的內(nèi)嵌和外置tomcat的實現(xiàn)方式的文章就介紹到這了,更多相關(guān)SpringBoot內(nèi)嵌和外置tomcat內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的注解與注解處理器

    Java中的注解與注解處理器

    這篇文章主要介紹了Java中的注解與注解處理器,元注解的作用是負責注解其他注解, Java5.0定義了4個標準的meta-annotation(元注解)類型,它們被用來提供對其它注解類型進行說明,需要的朋友可以參考下
    2023-11-11
  • SVN導入maven項目報錯解決方案

    SVN導入maven項目報錯解決方案

    這篇文章主要介紹了SVN導入maven項目報錯解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-12-12
  • idea解決程序包不存在報錯的八種解決方法

    idea解決程序包不存在報錯的八種解決方法

    這篇文章主要介紹了idea解決程序包不存在報錯的八種解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-02-02
  • IDEA怎么設(shè)置maven配置

    IDEA怎么設(shè)置maven配置

    這篇文章主要介紹了IDEA怎么設(shè)置maven配置,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 詳解Spring Boot Security工作流程

    詳解Spring Boot Security工作流程

    Spring Security,這是一種基于 Spring AOP 和 Servlet 。這篇文章主要介紹了Spring Boot Security的相關(guān)知識,需要的朋友可以參考下
    2019-04-04
  • 簡單了解SpringMVC與Struts2的區(qū)別

    簡單了解SpringMVC與Struts2的區(qū)別

    這篇文章主要介紹了簡單了解SpringMVC與Struts2的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • Java實現(xiàn)簡單棋盤存檔和讀取功能

    Java實現(xiàn)簡單棋盤存檔和讀取功能

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單棋盤存檔和讀取功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • SpringCloud?微服務框架詳解

    SpringCloud?微服務框架詳解

    本文詳細介紹了單體架構(gòu)、垂直架構(gòu)、分布式架構(gòu)和微服務架構(gòu)的特點和優(yōu)缺點,重點講解了微服務架構(gòu)的四個原則,最后介紹了如何使用Nacos進行配置中心管理,并實現(xiàn)了配置的熱更新,感興趣的朋友跟隨小編一起看看吧
    2025-11-11
  • Launch4j打包將jar包生成exe執(zhí)行文件實踐

    Launch4j打包將jar包生成exe執(zhí)行文件實踐

    Launch4j是一個開源工具,用于將Java應用程序的JAR包轉(zhuǎn)換為Windows原生可執(zhí)行文件(.exe),支持自定義圖標、JRE版本綁定、啟動參數(shù)配置等
    2026-01-01
  • Spring?@Conditional通過條件控制bean注冊過程

    Spring?@Conditional通過條件控制bean注冊過程

    這篇文章主要為大家介紹了Spring?@Conditional通過條件控制bean注冊過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02

最新評論

商河县| 炉霍县| 梁河县| 文昌市| 龙游县| 佛坪县| 浪卡子县| 普兰店市| 德格县| 岫岩| 常德市| 桐乡市| 弥勒县| 枣庄市| 广河县| 定南县| 米林县| 黎平县| 竹北市| 榆社县| 怀安县| 红桥区| 福泉市| 永宁县| 扎鲁特旗| 景洪市| 奉新县| 固原市| 安多县| 乐东| 永城市| 什邡市| 诸暨市| 罗平县| 临湘市| 甘肃省| 会理县| 铜山县| 呈贡县| 宁海县| 婺源县|