# 使用 maven spring-boot:run 修改默认端口
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
mvn spring-boot:run -Dspring-boot.run.jvmArguments=--server.port=8085
# 修改端口的多种姿势(不限于修改端口,也可以修改其他属性)
various configuration externalization mechanism (opens new window)
Pass property through command line argument as application argument
java -jar <path/to/my/jar> --server.port=7788From property in
SPRING_APPLICATION_JSON(Spring Boot 1.3.0+)Define environment variable in U*IX shell:
SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar>By using Java system property:
java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar>Pass through command line argument:
java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}'
Define JVM system property
java -Dserver.port=7788 -jar <path/to/my/jar>Define OS environment variable
U*IX Shell
SERVER_PORT=7788 java -jar <path/to/my/jar>Windows
SET SERVER_PORT=7788 java -jar <path/to/my/jar>
Place property in
./config/application.propertiesconfiguration fileserver.port=7788and run:
java -jar <path/to/my/jar>Place property in
./config/application.yamlserver: port: 7788and run:
java -jar <path/to/my/jar>Place property in
./application.propertiesserver.port=7788and run:
java -jar <path/to/my/jar>Place property in
./application.yamlserver: port: 7788and run:
java -jar <path/to/my/jar>
# 使用程序修改属性
@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8012);
});
}
}
或者
HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);
new SpringApplicationBuilder()
.sources(SampleController.class)
.properties(props)
.run(args);
# 随机端口
server.port=0