springboot的SpringPropertyAction事務屬性源碼解讀
序
本文主要研究一下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的動作為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中讀取屬性,然后通過ActionUtil.setProperty寫入到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>這里通過springProperty定義了application.name屬性,其從spring environment讀取key為spring.application.name的值作為application.name的值
小結(jié)
springboot的logback可以通過springProperty來引用spring environment中的屬性在logback的配置文件中使用,其主要是通過SpringPropertyAction來實現(xiàn)的。
以上就是springboot的SpringPropertyAction事務屬性源碼解讀的詳細內(nèi)容,更多關(guān)于springboot SpringPropertyAction的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
ReadWriteLock接口及其實現(xiàn)ReentrantReadWriteLock方法
下面小編就為大家?guī)硪黄猂eadWriteLock接口及其實現(xiàn)ReentrantReadWriteLock方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
@CacheEvict + redis實現(xiàn)批量刪除緩存
這篇文章主要介紹了@CacheEvict + redis實現(xiàn)批量刪除緩存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

