项目场景:
为了减少项目启动后经常输入端口号的操作,结合配置参数在启动时新增链接地址
解决方案:
参数分析:application.getEnvironment().getProperty(“server.port”);
里的server.port就是对应配置类里的对应配置
public static void main(String[] args) throws UnknownHostException {
//SpringApplication.run(CarHostApplication.class, args);
ConfigurableApplicationContext application = SpringApplication.run(CarHostApplication.class, args);
String ip = InetAddress.getLocalHost().getHostAddress();
String port =application.getEnvironment().getProperty("server.port");
String property =application.getEnvironment().getProperty("server.servlet.context-path");
String path = property == null ? "" : property;
System.out.println(
"\n\t" +
"----------------------------------------------------------\n\t" +
"Application Sailrui-Boot is running! Access URLs:\n\t" +
"Local: \t\thttp://localhost:" + port + path + "/\n\t" +
"External: \thttp://" + ip + ":" + port + path + "/\n\t" +
"Swagger: \thttp://" + ip + ":" + port + "/swagger-ui.html\n\t" +
"------------------------------------------------------------");
}
}