欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot啟動時如何獲取端口和項目名

 更新時間:2021年11月19日 15:23:10   作者:懵懂學(xué)子  
這篇文章主要介紹了springboot啟動時如何獲取端口和項目名,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot啟動獲取端口和項目名

背景

項目啟動每次都要手動輸url在瀏覽器中訪問,就想能和vue項目一樣啟動能直接在控制臺打印出url

在這里插入圖片描述

踩坑

在項目中獲取配置文件的方法為@Value,但是在啟動類中無法使用,獲取到的全都為null

使用

Environment

 public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        Environment environment = context.getBean(Environment.class);
		System.out.println("訪問鏈接:http://localhost:" +environment.getProperty("server.port")+environment.getProperty("server.servlet.context-path");
      );
 }

效果

在這里插入圖片描述

springboot配置項目運行端口號

可以通過application.properties配置文件來實現(xiàn),添加一行代碼即可

這個方法極其簡潔

server.port=8084

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論