springboot的SpringPropertyAction事務(wù)屬性源碼解讀
序
本文主要研究一下springboot的SpringPropertyAction
SpringBootJoranConfigurator
org/springframework/boot/logging/logback/SpringBootJoranConfigurator.java
class SpringBootJoranConfigurator extends JoranConfigurator { private LoggingInitializationContext initializationContext; SpringBootJoranConfigurator(LoggingInitializationContext initializationContext) { this.initializationContext = initializationContext; } @Override public void addInstanceRules(RuleStore rs) { super.addInstanceRules(rs); Environment environment = this.initializationContext.getEnvironment(); rs.addRule(new ElementSelector("configuration/springProperty"), new SpringPropertyAction(environment)); rs.addRule(new ElementSelector("*/springProfile"), new SpringProfileAction(environment)); rs.addRule(new ElementSelector("*/springProfile/*"), new NOPAction()); } }
SpringBootJoranConfigurator繼承了JoranConfigurator,其addInstanceRules添加了configuration/springProperty的動(dòng)作為SpringPropertyAction
SpringPropertyAction
org/springframework/boot/logging/logback/SpringPropertyAction.java
class SpringPropertyAction extends Action { private static final String SOURCE_ATTRIBUTE = "source"; private static final String DEFAULT_VALUE_ATTRIBUTE = "defaultValue"; private final Environment environment; SpringPropertyAction(Environment environment) { this.environment = environment; } @Override public void begin(InterpretationContext context, String elementName, Attributes attributes) throws ActionException { String name = attributes.getValue(NAME_ATTRIBUTE); String source = attributes.getValue(SOURCE_ATTRIBUTE); Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE)); String defaultValue = attributes.getValue(DEFAULT_VALUE_ATTRIBUTE); if (OptionHelper.isEmpty(name) || OptionHelper.isEmpty(source)) { addError("The \"name\" and \"source\" attributes of <springProperty> must be set"); } ActionUtil.setProperty(context, name, getValue(source, defaultValue), scope); } private String getValue(String source, String defaultValue) { if (this.environment == null) { addWarn("No Spring Environment available to resolve " + source); return defaultValue; } return this.environment.getProperty(source, defaultValue); } @Override public void end(InterpretationContext context, String name) throws ActionException { } }
SpringPropertyAction繼承了Action,它的主要功能就是允許從spring的environment中讀取logback的配置;其getValue方法從environment中讀取屬性,然后通過(guò)ActionUtil.setProperty寫(xiě)入到InterpretationContext中
示例
<property name="LOGS" value="./logs" /> <springProperty scope="context" name="application.name" source="spring.application.name" /> <springProfile name="production"> <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOGS}/${application.name}.log</file> <!-- configuration --> </appender> </springProfile>
這里通過(guò)springProperty定義了application.name屬性,其從spring environment讀取key為spring.application.name的值作為application.name的值
小結(jié)
springboot的logback可以通過(guò)springProperty來(lái)引用spring environment中的屬性在logback的配置文件中使用,其主要是通過(guò)SpringPropertyAction來(lái)實(shí)現(xiàn)的。
以上就是springboot的SpringPropertyAction事務(wù)屬性源碼解讀的詳細(xì)內(nèi)容,更多關(guān)于springboot SpringPropertyAction的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JAVA爬蟲(chóng)實(shí)現(xiàn)自動(dòng)登錄淘寶
給大家分享一個(gè)關(guān)于JAVA爬蟲(chóng)的相關(guān)知識(shí)點(diǎn),通過(guò)代碼實(shí)現(xiàn)自動(dòng)登錄淘寶網(wǎng),有興趣的朋友測(cè)試下。2018-04-04java啟動(dòng)jar包將日志打印到文本的簡(jiǎn)單操作
這篇文章主要介紹了java啟動(dòng)jar包將日志打印到文本的簡(jiǎn)單操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-0810分鐘在服務(wù)器部署好Jenkins的詳細(xì)過(guò)程
這篇文章主要介紹了10分鐘在服務(wù)器部署好Jenkins,本文主要是?Jenkins?的安裝部署,那前提我們應(yīng)該裝好?Git?Maven?JDK,準(zhǔn)備工作本文不給大家詳細(xì)介紹了,對(duì)服務(wù)器部署Jenkins相關(guān)知識(shí)感興趣的朋友一起看看吧2022-08-08ReadWriteLock接口及其實(shí)現(xiàn)ReentrantReadWriteLock方法
下面小編就為大家?guī)?lái)一篇ReadWriteLock接口及其實(shí)現(xiàn)ReentrantReadWriteLock方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06Java利用Easyexcel導(dǎo)出excel表格的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java利用Easyexcel導(dǎo)出excel表格的示例代碼,文中的代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-07-07@CacheEvict + redis實(shí)現(xiàn)批量刪除緩存
這篇文章主要介紹了@CacheEvict + redis實(shí)現(xiàn)批量刪除緩存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10java后端操作樹(shù)結(jié)構(gòu)的案例代碼
這篇文章主要介紹了java后端操作樹(shù)結(jié)構(gòu),樹(shù)結(jié)構(gòu)的三種組裝方式(遞歸.雙層for循環(huán),map),通過(guò)實(shí)例代碼介紹了使用遞歸查詢(xún)某個(gè)節(jié)點(diǎn)所在的樹(shù)結(jié)構(gòu),需要的朋友可以參考下2023-10-10MyBatis中map的應(yīng)用與模糊查詢(xún)實(shí)現(xiàn)代碼
這篇文章主要介紹了MyBatis中map的應(yīng)用與模糊查詢(xún)實(shí)現(xiàn)代碼,文中通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11解決Spring?AOP攔截抽象類(lèi)(父類(lèi))中方法失效問(wèn)題
這篇文章主要介紹了解決Spring?AOP攔截抽象類(lèi)(父類(lèi))中方法失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11