首页  

springboot2获取web端口     所属分类 spring 浏览量 1154
读取配置文件的方式不靠谱, 因为有多种方式设置端口

 springboot web端口设置三种方式 


使用 ApplicationListener 监听指定的时间
web容器启动发布 WebServerInitializedEvent 事件
从该事件拿到 web 端口





import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;


@Component
public class AppListener1 implements ApplicationListener<WebServerInitializedEvent> {
	@Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        int serverPort = event.getWebServer().getPort();
        
        AppConfigUtil.setServerPort(serverPort);
        
        System.out.println("serverPort="+serverPort);
        
    }
}



上一篇     下一篇
优秀投资者的十大特征

springboot web端口设置三种方式

springboot2 actuator 使用

springboot2微服务实例

springcloud DiscoveryClient使用说明

maven知识点整理