前提:
打出來的jar中已經(jīng)包含了各個(gè)application-xxx.yml文件,jar所在位置也引入了外部的application-test.yml。
目的:運(yùn)行時(shí),希望使用的時(shí)外部?application-test.yml? 文件。
使用了以下命令:
java -Xms1024m -Xmx2048m -jar ?/home/test/my-test-app-0.0.1.jar? --spring.config.location=./application-test.yml??--logging.config=./logback.xml ?-server my-test-app &
可以啟動(dòng),但是,使用的時(shí)jar包里面的application-test.yml配置,而不是外部的application-test.yml文件。
解決問題:
使用-D命令設(shè)置系統(tǒng)屬性
java -Xms1024m -Xmx2048m -jar? -Dspring.config.location=./application-test.yml /home/test/my-test-app-0.0.1.jar? ?--logging.config=./logback.xml ?-server my-test-app &
或者:
java -Xms1024m -Xmx2048m -jar? -Dspring.config.location=./application-test.yml -Dlogging.config=./logback.xml /home/test/my-test-app-0.0.1.jar -server my-test-app &
啟動(dòng)時(shí),如果直接在jar包所在的目錄啟動(dòng), 例如java -jar的方式, 那么會(huì)自動(dòng)加載config或者根目錄下的配置文件(properties, yml)
如果使用腳本的啟動(dòng)方式, 那么可能你的執(zhí)行腳本路徑和腳本所在路徑不在同一目錄, 那么這個(gè)時(shí)候可以使用絕對(duì)路徑來配置, 例如:
java -jar ./test.jar --spring.config.additional-location=../config/?--spring.profiles.active=dev
spring.config.location? :會(huì)覆蓋內(nèi)部配置參數(shù)
spring.config.additional-location :會(huì)和內(nèi)部配置參數(shù)互補(bǔ)
問題:文章來源:http://www.zghlxwxcb.cn/news/detail-530563.html
在測(cè)試過程種,如果引入的application文件包含spring.profile?屬性,會(huì)導(dǎo)致引用失敗,所以需要?jiǎng)h掉這個(gè)文件,如果需要使用spring.profile 則需要在啟動(dòng)的腳本中加入這個(gè)配置項(xiàng):文章來源地址http://www.zghlxwxcb.cn/news/detail-530563.html
java -Xms1024m -Xmx2048m -jar? -Dspring.config.location=./application-test.yml -Dlogging.config=./logback.xml /home/test/my-test-app-0.0.1.jar --spring.profile=test -server my-test-app &
到了這里,關(guān)于java啟動(dòng)jar包引入外部配置文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!