Spring Boot 把設定檔和記錄檔放到jar外部,springjar

來源:互聯網
上載者:User

Spring Boot 把設定檔和記錄檔放到jar外部,springjar

如果不想使用預設的application.properties,而想將屬性檔案放到jar包外面,可以使用如下兩種方法:

只能設定全路徑。因為Java -jar運行jar包時,無法指定classpath(無論通過參數還是環境變數,設定的classpath都會被覆蓋)。

方法1:命令列傳參指定spring.config.location

java -jar -Dspring.config.location=D:\zTest\config\config1.properties springbootrestdemo-0.0.1-SNAPSHOT.jar 

還可以用spring.config.location指定路徑,這樣會在這個路徑中去尋找application-{profile}.properties。

還可以用spring.config.location指定路徑,然後用spring.config.name指定設定檔名字。

可以用逗號隔開,指定多個路徑和名字

方法2:使用@PropertySource註解。

@SpringBootApplication@PropertySource(value={"file:D:/zTest/config/config1.properties"})public class SpringbootrestdemoApplication {  public static void main(String[] args) {    SpringApplication.run(SpringbootrestdemoApplication.class, args);  }}

下面看下Spring Boot 設定檔和記錄檔放到jar之外

1.設定打包jar的時候排除檔案

<resources>  <resource>    <directory>src/main/resources</directory>    <excludes>      <exclude>*.properties</exclude>      <exclude>logback.xml</exclude>    </excludes>  </resource></resources>

2.啟動的時候傳入參數指定位置

java -jar xxx.jar --spring.config.location=D:\springconfig\ --logging.config=D:\springconfig\logback.xml

springboot 預設找設定檔的位置如下

 // Note the order is from least to most specific (last one wins)private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:

總結

以上所述是小編給大家介紹的Spring Boot 把設定檔和記錄檔放到jar外部,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.