18 事务注解@Transaction的解析原理

本文深入探讨了Spring框架中@Transaction注解的解析原理,包括从缓存获取注解信息、在方法和类上查找注解、使用SpringTransactionAnnotationParser解析@Transactional属性,以及如何封装到RuleBasedTransactionAttribute中。
摘要由CSDN通过智能技术生成

由于md文档部分hugo插件语法不兼容,建议访问作者网站查阅文章:wlizhi.cc

spring源码系列文章,示例代码的中文注释,均是 copy 自 https://gitee.com/wlizhi/spring-framework

链接中源码是作者从 github 下载,并以自身理解对核心流程及主要节点做了详细的中文注释。


[17 声明式事务执行流程源码分析] 中已经列举,通过 getTransactionAttribute() 获取到了方法上事务注解的一些信息。

来到其源码:

public abstract class AbstractFallbackTransactionAttributeSource implements TransactionAttributeSource {
   
    public TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
   
		// 获取事务注解属性
		if (method.getDeclaringClass() == Object.class) {
   
			return null;
		}

		// 先从缓存中拿。如果一个方法的事务注解信息被获取过,就会将其缓存到一个并发安全的map中。后面再获取就从这个缓存中获取。
		// First, see if we have a cached value.
		Object cacheKey = getCacheKey(method, targetClass);
		TransactionAttribute cached = this.attributeCache.get(cacheKey);
		if (cached != null) {
   
			// 省略...
		}else {
   
			// TODO 获取事务注解属性,放入缓存。
			// We need to work it out.
			TransactionAttribute txAttr = computeTransactionAttribute(method, targetClass);
			// Put it in the cache.
			if (txAttr == null) {
   // 放入缓存
				this.attributeCache.put(cacheKey, NULL_TRANSACTION_ATTRIBUTE);
			}
			else {
   
				String methodIdentification = ClassUtils.getQualifiedMethodName(method, targetClass);
				// 省略...
				this.attributeCache.put(cacheKey, txAttr);
			}
			return txAttr;
		
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值