1. 程式人生 > >22 配置嵌入式Servlet容器(Tomcat為例)

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 測試結果

執行結果