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

版权声明:看什么?6,你和我,走一波! https://blog.csdn.net/qq_31323797/article/details/84833397
  • 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 测试结果

运行结果

猜你喜欢

转载自blog.csdn.net/qq_31323797/article/details/84833397
22