Spring事务源码(三)

Spring事务源码(三)

一、开始,看过我前面Aop源码的朋友,对于事务整个流程应该非常熟悉
在这里插入图片描述
二、源码分析,本章只分析寻找符合条件的增强器这一块源码findAdvisorsThatCanApply,对于寻找增强器的源码findCandidateAdvisors请参照博客https://blog.csdn.net/mlplds/article/details/103145409

protected List<Advisor> findAdvisorsThatCanApply(
			List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) {

		ProxyCreationContext.setCurrentProxiedBeanName(beanName);
		try {
		//真正的去候选的增强器中找到当前能用的增强器
			return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
		}
		finally {
			ProxyCreationContext.setCurrentProxiedBeanName(null);
		}
	}
public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
		//若传入进来的候选增强器为空直接返回
		if (candidateAdvisors.isEmpty()) {
			return candidateAdvisors;
		}
		//创建一个本类能用的增前期集合
		List<Advisor> eligibleAdvisors = new LinkedList<Advisor>();
		//循环候选的增强器
		for (Advisor candidate : candidateAdvisors) {
		//判断增强器是不是实现了IntroductionAdvisor 很明显没实现该接口(看继承图)
			if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
				eligibleAdvisors.add(candidate);
			}
		}
		boolean hasIntroductions = !eligibleAdvisors.isEmpty();
		for (Advisor candidate : candidateAdvisors) {
			if (candidate instanceof IntroductionAdvisor) {
				// already processed
				continue;
			}
			//正在找出能用的增强器
			if (canApply(candidate, clazz, hasIntroductions)) {
				eligibleAdvisors.add(candidate);
			}
		}
		return eligibleAdvisors;
	}
public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
		//根据类的继承图 发现 BeanFactoryTransactionAttributeSourceAdvisor没实现IntroductionAdvisor接口
		if (advisor instanceof IntroductionAdvisor) {
			return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
		}
		//BeanFactoryTransactionAttributeSourceAdvisor实现了PointcutAdvisor接口
		else if (advisor instanceof PointcutAdvisor) {
		//强制转换为PointcutAdvisor
			PointcutAdvisor pca = (PointcutAdvisor) advisor;
			return canApply(pca.getPointcut(), targetClass, hasIntroductions);
		}
		else {
			// It doesn't have a pointcut so we assume it applies.
			return true;
		}
	}
public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
		Assert.notNull(pc, "Pointcut must not be null");
		if (!pc.getClassFilter().matches(targetClass)) {
			return false;
		}
			/** 
			* 获取切点中的方法匹配器 TransactionAttributeSourcePointcut 
			* * 该切点在创建BeanFactoryTransactionAttributeSourceAdvisor的时候 创建了切点TransactionAttributeSourcePointcut 
			* */
		MethodMatcher methodMatcher = pc.getMethodMatcher();
		if (methodMatcher == MethodMatcher.TRUE) {
			// No need to iterate the methods if we're matching any method anyway...
			return true;
		}
		
		/** 
		* 判断方法匹配器是不是IntroductionAwareMethodMatcher(此次不是IntroductionAwareMethodMatcher类型)
		* */
		IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
		if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
			introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
		}
		//获取当前类的实现接口类型
		Set<Class<?>> classes = new LinkedHashSet<Class<?>>(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
		classes.add(targetClass);
		//循环上一步的接口类型
		for (Class<?> clazz : classes) {
		//获取接口的所有方法
			Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz);
			//循环我们接口中的方法
			for (Method method : methods) {
			//正在进行匹配的是methodMatcher.matches(method, targetClass)这个逻辑
				if ((introductionAwareMethodMatcher != null &&
						introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) ||
						methodMatcher.matches(method, targetClass)) {
					return true;
				}
			}
		}

		return false;
	}

TransactionAttributeSourcePointcut

public boolean matches(Method method, Class<?> targetClass) {
		if (targetClass != null && TransactionalProxy.class.isAssignableFrom(targetClass)) {
			return false;
		}
		//获取我们的事物源对象(在ProxyTransactionManagementConfiguration配置类配置的这里获取) 
		//在创建时BeanFactoryTransactionAttributeSourceAdvisor时创建TransactionAttributeSourcePointcut时复写了getTransactionAttributeSource
		TransactionAttributeSource tas = getTransactionAttributeSource();
		//从事物源对象中获取事物属性
		return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
	}
public TransactionAttribute getTransactionAttribute(Method method, Class<?> targetClass) {
		if (method.getDeclaringClass() == Object.class) {
			return null;
		}

		// First, see if we have a cached value.
		//通过目标类和目标类的接口方法 拼接缓存key
		Object cacheKey = getCacheKey(method, targetClass);
		//去缓存中获取
		TransactionAttribute cached = this.attributeCache.get(cacheKey);
		if (cached != null) {
			// Value will either be canonical value indicating there is no transaction attribute,
			// or an actual transaction attribute.
			//缓存中有 直接返回就可以了
			if (cached == NULL_TRANSACTION_ATTRIBUTE) {
				return null;
			}
			else {
				return cached;
			}
		}
		else {
			// 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);
				//为事物属性设置方法描述符号
				if (txAttr instanceof DefaultTransactionAttribute) {
					((DefaultTransactionAttribute) txAttr).setDescriptor(methodIdentification);
				}
				if (logger.isDebugEnabled()) {
					logger.debug("Adding transactional method '" + methodIdentification + "' with attribute: " + txAttr);
				}
				//加入到缓存
				this.attributeCache.put(cacheKey, txAttr);
			}
			return txAttr;
		}
	}
protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) {
		// Don't allow no-public methods as required.
		if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
			return null;
		}

		// Ignore CGLIB subclasses - introspect the actual user class.
		Class<?> userClass = ClassUtils.getUserClass(targetClass);
		// The method may be on an interface, but we need attributes from the target class.
		// If the target class is null, the method will be unchanged.
		/** * method为接口中的方法,specificMethod为我们实现类方法 * */
		Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass);
		// If we are dealing with method with generic parameters, find the original method.
		specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

		// First try is the method in the target class.
		// 找我们【实现类】中的【方法】上的事物属性
		TransactionAttribute txAttr = findTransactionAttribute(specificMethod);
		if (txAttr != null) {
			return txAttr;
		}

		// Second try is the transaction attribute on the target class.
		//【方法所在类】上有没有事物属性
		txAttr = findTransactionAttribute(specificMethod.getDeclaringClass());
		if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
			return txAttr;
		}
		//【接口上的指定的方法】
		if (specificMethod != method) {
			// Fallback is to look at the original method.
			txAttr = findTransactionAttribute(method);
			if (txAttr != null) {
				return txAttr;
			}
			// Last fallback is the class of the original method.
			//【接口上】
			txAttr = findTransactionAttribute(method.getDeclaringClass());
			if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
				return txAttr;
			}
		}

		return null;
	}
@Override
	protected TransactionAttribute findTransactionAttribute(Method method) {
		return determineTransactionAttribute(method);
	}
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement element) {
		//获取方法上的注解
		if (element.getAnnotations().length > 0) {
		//事物注解解析器
			for (TransactionAnnotationParser annotationParser : this.annotationParsers) {
			//解析我们的注解
				TransactionAttribute attr = annotationParser.parseTransactionAnnotation(element);
				if (attr != null) {
					return attr;
				}
			}
		}
		return null;
	}

SpringTransactionAnnotationParser

@Override
	public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
	//解析@Transactional属性对象(AnnotationAttributes extends LinkedHashMap<String, Object>)
		AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(
				element, Transactional.class);
		if (attributes != null) {
		//真正的解析@Transactional属性(包装一下)
			return parseTransactionAnnotation(attributes);
		}
		else {
			return null;
		}
	}
protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
		RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
		//传播行为
		Propagation propagation = attributes.getEnum("propagation");
		rbta.setPropagationBehavior(propagation.value());
		//隔离级别
		Isolation isolation = attributes.getEnum("isolation");
		rbta.setIsolationLevel(isolation.value());
		//事物超时
		rbta.setTimeout(attributes.getNumber("timeout").intValue());
		//判断是否为只读事物
		rbta.setReadOnly(attributes.getBoolean("readOnly"));
		//事物的名称吧
		rbta.setQualifier(attributes.getString("value"));

		List<RollbackRuleAttribute> rollbackRules = new ArrayList<RollbackRuleAttribute>();
		//事物回滚规则
		for (Class<?> rbRule : attributes.getClassArray("rollbackFor")) {
			rollbackRules.add(new RollbackRuleAttribute(rbRule));
		}
		//对哪个类进行回滚
		for (String rbRule : attributes.getStringArray("rollbackForClassName")) {
			rollbackRules.add(new RollbackRuleAttribute(rbRule));
		}
		//对哪些异常不回滚
		for (Class<?> rbRule : attributes.getClassArray("noRollbackFor")) {
			rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
		}
		//对哪些类不回滚
		for (String rbRule : attributes.getStringArray("noRollbackForClassName")) {
			rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
		}
		rbta.setRollbackRules(rollbackRules);

		return rbta;
	}

寻好到了该类对于的增强器。开始创建动态代理,前面已经分析过了,此次不做详细分析
在这里插入图片描述
三、本章就到这里,下一章将分析动态代理的调用。

Spring事务Spring框架中非常重要的特性之一,它提供了对事务的管理和控制。在Spring中,事务源码主要涉及到以下几个方面: 1. 事务管理器接口(PlatformTransactionManager):该接口定义了事务管理器的基本操作,如开启事务、提交事务、回滚事务等。不同的数据访问技术(如JDBC、Hibernate、JPA等)有不同的实现类。 2. 事务定义接口(TransactionDefinition):该接口定义了事务的属性,如隔离级别、传播行为、超时时间等。Spring中提供了多种事务定义的实现类,例如DefaultTransactionDefinition。 3. 事务状态接口(TransactionStatus):该接口定义了事务的状态信息,如是否处于活动状态、是否可以提交、是否可以回滚等。Spring中提供了多种事务状态的实现类,例如DefaultTransactionStatus。 4. 事务切面(TransactionAspectSupport):该类是Spring事务的核心类之一,它是一个抽象类,提供了对事务的具体实现。它通过AOP技术为带有@Transactional注解的方法创建代理对象,并在方法执行前后进行事务的开启、提交和回滚等操作。 5. 事务切面的通知类(TransactionInterceptor):该类是Spring事务的通知类之一,实现了MethodInterceptor接口。它负责在方法执行前后执行事务相关的逻辑,如开启事务、提交事务、回滚事务等。 以上是Spring事务源码的主要组成部分,通过对这些类的深入理解和研究,可以更好地掌握Spring事务的原理和实现机制。注意,这里只是简单介绍了一些关键的类和接口,实际上Spring事务源码非常庞大且复杂。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值