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外部,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!