package com.common.utils; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import java.net.InetAddress; import java.net.UnknownHostException; /** * 获取项目的IP和端口 * * @Owner:lwy * @Time: 2020/12/15 */ @Component public class ServerPortConfig implements ApplicationListener<WebServerInitializedEvent> { public int getServerPort() { return serverPort; } private int serverPort; public String getUrl() { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return "http://"+address.getHostAddress()+":"+this.serverPort; } public String getHost() { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return address.getHostAddress(); } @Override public void onApplicationEvent(WebServerInitializedEvent event) { serverPort = event.getWebServer().getPort(); } }
获取项目的IP和端口--springboot
最新推荐文章于 2022-08-25 00:34:47 发布