Spring boot 2.5.2 获取服务运行端口号
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author shimea
* @date 2021/7/23 14:56
*/
@Component
public class GetServerPort implements ApplicationListener<WebServerInitializedEvent> {
public static int port;
@Override
public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
port= webServerInitializedEvent.getApplicationContext().getWebServer().getPort();
}
}