spring boot 默認端口為8080
1.修改為指定端口
(1)修改配置文件
src/main/resources/application.properties
server.port=8081
(2)通過編碼的方式來指定端口
在啟動類中添加servletContainer方法
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public TomcatServletWebServerFactory servletContainer(){ return new TomcatServletWebServerFactory(8081) ; } }
說明:
該代碼適用於spring boot2X中
2.動態指定端口
生成jar包后,動態指定端口
(1)在命令行中指定啟動端口
java -jar test.jar --server.port=8081
(2)傳入虛擬機系統屬性
java -Dserver.port=8081 -jar test.jar
說明:
java [options] -jar filename [args]
其中 options
-D[property]=value
定義系統屬性值
property變量是一個字符串代表屬性名,value代表設定的屬性值