Spring boot 自带监听 实现接口即可在容器启动的时候 启动监听 获取 端口 代码 如下
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class ConfigDemo implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private int serverPort;
@Override
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
this.serverPort = event.getEmbeddedServletContainer().getPort();
}
public int getPort() {
return this.serverPort;
}
}
ip 的 获取 如下
String host = null;
try {
host = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
log.error("get server host Exception e:", e);
}
更多推荐
spring boot 获取端口和 Ip
发布评论