SpringBoot的内嵌和外置tomcat

目录

1.内嵌

如何定制和修改Servlet容器的相关配置

注册Servlet三大组件

Servlet注册详解

Filter注册详解

Listener注册详解

2.外置


1.内嵌

如何定制和修改Servlet容器的相关配置

修改修改和server有关的配置:

复制代码
server.port=8081
server.context-path=/tx
server.tomcat.uri-encoding=UTF-8

配置了这个之后,重启应用:

访问地址变为:http://localhost:8081/tx

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

注册Servlet三大组件

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。

三大组件分别是:

Servlet服务器, Filter(过滤器)和 Listener(监听器)

首先配置配置类

java 复制代码
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 方法
//相当于传统的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调用 myServlet() 方法

创建 MyServlet 实例

创建 ServletRegistrationBean,将Servlet映射到 /myServlet 路径

返回注册Bean,Spring将其加入容器

效果:访问 http://localhost:8080/myServlet 会调用 MyServlet

Filter注册详解

setFilter(new MyFilter()):设置过滤器实例

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

/hello:拦截该路径

/myServlet:拦截该路径

效果:访问 /hello 或 /myServlet 时,会先经过 MyFilter

Listener注册详解

不需要配置URL模式,自动监听应用生命周期事件

运行之后,访问localhost:8080/myServlet​​​​​​ (记得把之前的配置文件注释掉)然后查看控制台输出:

2.外置

嵌入式Servlet容器:应用打成可执行的jar

优点:简单、便携;

缺点:默认不支持JSP、优化定制比较复杂.;

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

创建一个新项目:Cloud Native App Initializerhttps://start.aliyun.com/

创建好之后下载压缩包,解压到idea目录里边

pom.xml

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文件就创建好了

然后我们就可以和之前的ssm项目一样配置tomcat了:

如图

ServletInitializer的流程:

外置Tomcat启动→发现 ServletInitializer (继承自 SpringBootServletInitializer)→调用 configure() 方法→启动 Spring Boot 主类 (Application.class)→Spring Boot 应用正常运行

java 复制代码
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包:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;

war包:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;

相关推荐
bcbnb4 小时前
如何解析iOS崩溃日志:从获取到符号化分析
后端
许泽宇的技术分享4 小时前
当AI学会“说人话“:Azure语音合成技术的魔法世界
后端·python·flask
用户69371750013844 小时前
4.Kotlin 流程控制:强大的 when 表达式:取代 Switch
android·后端·kotlin
用户69371750013844 小时前
5.Kotlin 流程控制:循环的艺术:for 循环与区间 (Range)
android·后端·kotlin
vx_bisheyuange4 小时前
基于SpringBoot的宠物商城网站的设计与实现
spring boot·后端·宠物
bcbnb4 小时前
全面解析网络抓包工具使用:Wireshark和TCPDUMP教程
后端
leonardee5 小时前
Spring Security安全框架原理与实战
java·后端
一个处女座的程序猿O(∩_∩)O5 小时前
Spring Boot、Redis、RabbitMQ 在项目中的核心作用详解
spring boot·redis·java-rabbitmq
q***5185 小时前
Spring Cloud gateway 路由规则
java
回家路上绕了弯5 小时前
包冲突排查指南:从发现到解决的全流程实战
分布式·后端