import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* 获取本机端口号
*/
@Component
public class IpConfiguration implements ApplicationListener<WebServerInitializedEvent> {
private int serverPort;
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
this.serverPort = event.getWebServer().getPort();
}
public int getPort() {
return this.serverPort;
}
}
SpringBoot版本号2.1.3
参考文章https://blog.youkuaiyun.com/anurnomeru/article/details/80236747