springboot 運(yùn)行 jar 包讀取外部配置文件的問(wèn)題
案例:本文主要描述linux系統(tǒng)執(zhí)行jar包讀取jar包同級(jí)目錄的外部配置文件方法一:相對(duì)路徑設(shè)置配置文件(1)在jar包同級(jí)目錄創(chuàng)建配置文件conf.properties并寫(xiě)入配置數(shù)據(jù):
confData=data
(2)開(kāi)始寫(xiě)入自動(dòng)化測(cè)試代碼
//from www.fhadmin.cnpublic class Test{ public String getData() throws IOException {//讀取配置文件Properties properties = new Properties();File file = new File('conf.properties');FileInputStream fis = new FileInputStream(file);properties.load(fis);fis.close();//獲取配置文件數(shù)據(jù)String confData = properties.getProperty('confData');System.out.println(confData); }}
(3)執(zhí)行jar包
java -jar jarNanexxx
方法二:絕對(duì)路徑設(shè)置配置文件解決問(wèn)題:使用相對(duì)路徑的方法在jar包同級(jí)目錄手動(dòng)執(zhí)行jar包時(shí)沒(méi)有問(wèn)題,但使用linux系統(tǒng)的crontab文件定時(shí)調(diào)度時(shí)報(bào)錯(cuò),原因:因?yàn)槲覀兪謩?dòng)執(zhí)行某個(gè)腳本時(shí),是在當(dāng)前shell環(huán)境下進(jìn)行的,程序能找到環(huán)境變量;而系統(tǒng)自動(dòng)執(zhí)行任務(wù)調(diào)度時(shí),除了默認(rèn)的環(huán)境,是不會(huì)加載任何其他環(huán)境變量的。因此就需要在crontab文件中指定任務(wù)運(yùn)行所需的所有環(huán)境變量,或者在程序中使用絕對(duì)路徑。(1)在jar包同級(jí)目錄創(chuàng)建配置文件conf.properties并寫(xiě)入配置數(shù)據(jù):
confData=data
(2)開(kāi)始寫(xiě)入自動(dòng)化測(cè)試代碼
//from www.fhadmin.cnpublic class Test{ public String getData() throws IOException { //獲取jar包同級(jí)目錄String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();String[] pathSplit = path.split('/');String jarName = pathSplit[pathSplit.length - 1];String jarPath = path.replace(jarName, '');String pathName=jarPath+'minhang.properties';System.out.println('配置文件路徑:'+jarPath);//讀取配置文件Properties properties = new Properties();File file = new File(pathName);FileInputStream fis = new FileInputStream(file);properties.load(fis);fis.close();//獲取配置文件數(shù)據(jù)String confData = properties.getProperty('confData');System.out.println(confData); }}
(3)執(zhí)行jar包
java -jar jarNanexxx
到此這篇關(guān)于springboot 運(yùn)行 jar 包讀取外部配置文件的文章就介紹到這了,更多相關(guān)springboot 配置文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式3. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼4. React優(yōu)雅的封裝SvgIcon組件示例5. 輕松學(xué)習(xí)XML教程6. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. jsp中sitemesh修改tagRule技術(shù)分享9. ASP基礎(chǔ)知識(shí)VBScript基本元素講解10. 詳解瀏覽器的緩存機(jī)制
