Spring boot 如何讀取jar包外面的properties檔案

來源:互聯網
上載者:User

標籤:先來   根目錄   http   images   添加   .bat   start   target   設定檔   

 

先來張target跟目錄的檔案結構

conf檔案夾中存放的是properties檔案,

lib檔案夾中存放的是各種jar檔案,

start.bat是windows命令列的開機檔案。

 

來張properties檔案的內容

 

 

1. 建立設定檔相關的配置類,每個properties檔案對應一個配置類

這裡使用了@ConfigurationProperties,

prefix是指properties檔案中的首碼, 比如 asdf.name, 首碼就是asdf,  實體類中的name對應著asdf後面的name。

locations是指的properties檔案的地址,這裡我用了file, 而不是classpath,  使用classpath總是出問題,不曉得為什麼。  conf檔案夾是因為start命令列是在target根目錄,當前啟動並執行目錄就是target根目錄。

 

2. 在application類中添加@EnableConfigurationProperties

有幾個配置類,就要加幾個類。

 

3. 在要使用的地方匯入

4. 可以使用了

 

Spring boot 如何讀取jar包外面的properties檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.