springboot的SpringPropertyAction事务属性源码解读

 更新时间:2023年11月03日 10:47:33   作者:codecraft  
这篇文章主要介绍了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的值

小结

springboot的logback可以通过springProperty来引用spring environment中的属性在logback的配置文件中使用,其主要是通过SpringPropertyAction来实现的。

以上就是springboot的SpringPropertyAction事务属性源码解读的详细内容,更多关于springboot SpringPropertyAction的资料请关注脚本之家其它相关文章!

相关文章

  • JAVA爬虫实现自动登录淘宝

    JAVA爬虫实现自动登录淘宝

    给大家分享一个关于JAVA爬虫的相关知识点,通过代码实现自动登录淘宝网,有兴趣的朋友测试下。
    2018-04-04
  • 使用Java实现生命游戏串行代码示例

    使用Java实现生命游戏串行代码示例

    生命游戏是一种二维细胞自动机,由英国数学家在1970年发明,在游戏的过程中,细胞会形成各种有规律的结构,展现出生命的复杂性和多样性,本文通过java和JavaFX实现了一个简单的生命游戏,可以直观的观察到细胞的迭代过程,需要的朋友可以参考下
    2024-10-10
  • java启动jar包将日志打印到文本的简单操作

    java启动jar包将日志打印到文本的简单操作

    这篇文章主要介绍了java启动jar包将日志打印到文本的简单操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • 10分钟在服务器部署好Jenkins的详细过程

    10分钟在服务器部署好Jenkins的详细过程

    这篇文章主要介绍了10分钟在服务器部署好Jenkins,本文主要是 Jenkins 的安装部署,那前提我们应该装好 Git Maven JDK,准备工作本文不给大家详细介绍了,对服务器部署Jenkins相关知识感兴趣的朋友一起看看吧
    2022-08-08
  • ReadWriteLock接口及其实现ReentrantReadWriteLock方法

    ReadWriteLock接口及其实现ReentrantReadWriteLock方法

    下面小编就为大家带来一篇ReadWriteLock接口及其实现ReentrantReadWriteLock方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Java利用Easyexcel导出excel表格的示例代码

    Java利用Easyexcel导出excel表格的示例代码

    这篇文章主要为大家详细介绍了Java利用Easyexcel导出excel表格的示例代码,文中的代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-07-07
  • @CacheEvict + redis实现批量删除缓存

    @CacheEvict + redis实现批量删除缓存

    这篇文章主要介绍了@CacheEvict + redis实现批量删除缓存方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • java后端操作树结构的案例代码

    java后端操作树结构的案例代码

    这篇文章主要介绍了java后端操作树结构,树结构的三种组装方式(递归.双层for循环,map),通过实例代码介绍了使用递归查询某个节点所在的树结构,需要的朋友可以参考下
    2023-10-10
  • MyBatis中map的应用与模糊查询实现代码

    MyBatis中map的应用与模糊查询实现代码

    这篇文章主要介绍了MyBatis中map的应用与模糊查询实现代码,文中通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-11-11
  • 解决Spring AOP拦截抽象类(父类)中方法失效问题

    解决Spring AOP拦截抽象类(父类)中方法失效问题

    这篇文章主要介绍了解决Spring AOP拦截抽象类(父类)中方法失效问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11

最新评论