22 配置嵌入式Servlet容器(Tomcat为例)

  • SpringBoot默认使用Tomcat作为嵌入式的Servlet容器
    SpringBoot依赖图

1 自定义Server相关配置

1.1 通过application.properties自定义

# 通用的Servlet容器设置
server.port=8081

# Tomcat的设置--server.tomcat.xxx
server.tomcat.uri-encoding=UTF-8

1.1.1 属性来源

  • 相关属性都在
org.springframework.boot.autoconfigure.web.ServerProperties

1.2 通过WebServerFactoryCustomizer自定义

package com.gp6.springboot19.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

    //配置嵌入式的Servlet容器
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        /*return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {

            // 定制Servlet容器的相关规则
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8081);
            }
        };*/

        // 该段代码为上面代码的lambda的表达
        return factory -> factory.setPort(8081);
    }
}

1.2.1 测试结果

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值