Spring 项目 运行后打印程序的IP以及端口
修改前
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
@SpringBootApplication
public class DemoApp {
public static void main(String[] args) {
SpringApplication.run(DemoApp.class, args);
}
}
修改后
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
@SpringBootApplication
public class DemoApp {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext application = SpringApplication.run(DemoApp.class, args);
Environment env = application.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String property = env.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" +
"------------------------------------------------------------");
}
}

浙公网安备 33010602011771号