獲取springboot的配置文件內(nèi)容(springboot獲取properties中的內(nèi)容)

發(fā)布時(shí)間:2025-01-09
本文主要介紹獲取springboot的配置文件內(nèi)容(springboot獲取properties中的內(nèi)容),下面一起看看獲取springboot的配置文件內(nèi)容(springboot獲取properties中的內(nèi)容)相關(guān)資訊。
springboot可以通過@profile在配置類中定義配置環(huán)境( 發(fā)展與創(chuàng)新),在實(shí)際開發(fā)中可能需要方法級(jí)的控制,也就是說可以獲取當(dāng)前環(huán)境的具體信息。
最后,通過查看springboot日志和源代碼找到答案。
springboot的啟動(dòng)日志里有以下句子:15 : 57 : 56.128【重啟main】。以下配置文件是activ:開發(fā)跟蹤代碼:springapplication.run方法public configuration application context。xt運(yùn)行(字符串...args) { stopwatch秒表=新秒表;秒表. start;configurableapplicationcontext上下文= nullfailureanalyzers analyzers = nullconfigureheadlessproperty;springapplicationrunlisteners listeners = getrunlisteners(args);listeners . starting;try { application arguments application arguments = new defaultapplicationarguments(args);configurable environment environment = prepare environment(listeners,application arguments);banner printed banner = print banner(環(huán)境);context = createapplicationcontext;分析器=新的故障分析器(上下文);準(zhǔn)備上下文(上下文t、環(huán)境、偵聽器、applicationarguments、printed banner);//此處打印,后面是refreshcontext(上下文);afterrefresh(上下文,應(yīng)用程序參數(shù));listeners.finished(上下文,空);秒表. stop;if(this . logstartupinfo){ new startupinfologger(this . mainapplicationclass)。logstarted(getapplicationlog,秒表);}返回上下文;} catch(throwable ex){ handlerunfailure(context,listeners,analyzers,ex);拋出新的illegalstateexception(ex);}}跟蹤代碼:spring應(yīng)用程序。preparecontext方法private void preparecontext(可配置環(huán)境上下文、springapplicationrunlisteners偵聽器、application arguments application arguments、banner printed banner){ context . set environment(環(huán)境);postprocessapplicationcontext(上下文);applyinitializers(上下文);listeners.contextprepared(上下文);if(this . logstartupinfo){ logstartupinfo(context . get parent= = null);logstartupprofileinfo(上下文);//名字顯而易見,繼續(xù)追蹤在}...}跟蹤代碼:springapplica。tion.logstartupprofileinfo的方法protected void logstartupprofileinfo(configurationapplicationcontext context){ log log = getapplicationlog;if(log . isinfoenabled){ string[]active profiles = context . get environment。getactiveprofiles;if(object utils . isempty(active profiles)){ string[]default profiles = cont: string utils . arraytocommadelimitedstring(default profiles));} else { log . info( 以下簡(jiǎn)介是activ: string utils . arraytocommadelimitedstring(active profiles));//我找到了。顯然,我使用了applicationcontxt容器。下一步是編寫工具類來獲取應(yīng)用程序。}}}編寫springcontextholder工具類@ slf4jpublic類springcontextholder實(shí)現(xiàn)applicationcontextaware,disposable bean { private static application context application context = null;/* * *獲取靜態(tài)變量中存儲(chǔ)的applicationcontext。*/public static application context getapplicationcontext{ assertcontextobject;退貨申請(qǐng)icationcontext}/* * *從靜態(tài)變量applicationcontext中獲取bean,并自動(dòng)將其轉(zhuǎn)換為所分配對(duì)象的類型。*/public static t t get bean(字符串名){ assertcontextinjected;return(t)application context . get bean(name);}/* * *從靜態(tài)變量applicationcontext中獲取bean,并自動(dòng)將其轉(zhuǎn)換為所分配對(duì)象的類型。*/public static t t get bean(class required type){ assertcontextinjected;返回application context . get bean(required type);}/* * *檢查applicationcontext是否不為空。*/private static void assertcontextobject{ if(application context = = null){ throw new illegalstateexception( applicaisetapplicatpcontextoncontext屬性未注入,請(qǐng)?jiān)赼pplicationcontext 。或者在springboot啟動(dòng)類中注冊(cè)springcontextholder。 );}}/* * *獲取當(dāng)前環(huán)境* @ return */public static string get active profile{返回應(yīng)用程序上下文。getenvironment。get active profiles[0];}/* * *將springcontextholder中的applicationcontext清除為null。*/public static voidclearholder{ log . debug( 清除applicationcontext: springcontextholder中的applicationcontext應(yīng)用程序上下文= null;} @override public void destroy拋出異常{ springcontextholder . clear holder;} @ override public void setapplicationcontext(application context application context)拋出beans exception { if(spring context holder . application context!= null){ log . warn( springcontextholder中的applicationcontext被覆蓋,原applicationcontext為: springcontextholder . application上下文);} springcontextholder . application context = application cont測(cè)試環(huán)境中控制當(dāng)前訪問的界面url的打印。
如果( 發(fā)展與創(chuàng)新。equals (springcontextholder。getactiveprofile){ system . out . println(string . format( = = = =當(dāng)前界面url : % s ,r:
環(huán)境變量
了解更多獲取springboot的配置文件內(nèi)容(springboot獲取properties中的內(nèi)容)相關(guān)內(nèi)容請(qǐng)關(guān)注本站點(diǎn)。
上一個(gè):灌裝封口機(jī)很多方面都高于傳統(tǒng)設(shè)備給包裝行業(yè)全新的改變
下一個(gè):fba空運(yùn)美國(guó)價(jià)格(fba國(guó)際空運(yùn)配送)

苗木施工時(shí)影響生長(zhǎng)的5個(gè)關(guān)鍵問題
國(guó)際十大物流企業(yè) 國(guó)際十大物流公司
亞馬遜FBA入倉(cāng)的要求都是什么流程?
去美國(guó)海運(yùn)航線船公司(美國(guó)到中國(guó)海運(yùn)船)
物流園區(qū)的分類,物流園區(qū)的分類
FBA頭程運(yùn)輸?shù)男〕WR(shí)
走步亞馬遜FBA頭程到美國(guó)具體收費(fèi)標(biāo)準(zhǔn)是什么?
fba倉(cāng)和海外倉(cāng)的區(qū)別是什么?
RC0603FR-07680RL,0603 680Ω 1% 1/10W 電阻
除氟劑使用時(shí)怎樣才能將它的藥效發(fā)揮好來呢
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos