我是Springboot的新手,只需看一篇教程就知道,如果我想改变端口,我必须在application.properties中做。我想知道有没有办法改变港口。提前感谢
发布于 2019-12-23 13:03:13
可编程配置我们可以通过在启动应用程序时设置特定属性或自定义嵌入式服务器配置来编程配置端口。
首先,让我们看看如何在main @SpringBootApplication类中设置属性:
@SpringBootApplication
public class CustomApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(CustomApplication.class);
app.setDefaultProperties(Collections
.singletonMap("server.port", "8083"));
app.run(args);
}
}接下来,要定制服务器配置,我们必须实现WebServerFactoryCustomizer接口:
@Component
public class ServerPortCustomizer
implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8086);
}
}请注意,这适用于SpringBoot2.x版本。
对于SpringBoot1.x,我们可以类似地实现EmbeddedServletContainerCustomizer接口。
使用命令行参数将应用程序打包和运行为jar,我们可以使用java命令设置server.port参数:
--server.port=8083
或者使用等效的语法:
spring-5.jar
了解更多信息,请访问:https://www.baeldung.com/spring-boot-change-port
注意:如果您在application.properties中提到了8080,但您想在8083上运行它,那么它将通过在命令行参数中提供端口号来工作,如下所示,
spring-5.jar
https://stackoverflow.com/questions/59455736
复制相似问题