温馨提示×

springboot怎么指定端口启动

小亿
112
2024-06-06 14:24:17
栏目: 编程语言

在Spring Boot中,可以通过在application.properties文件中设置server.port属性来指定端口启动。示例如下:

server.port=8080

也可以在启动类中使用@Value注解来指定端口,示例如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan
public class Application extends SpringBootServletInitializer {

    @Value("${server.port}")
    private int port;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

以上两种方式都可以用来指定Spring Boot应用的端口启动。

0