SpringAop学习笔记(四)——@Transactional

目录

一、入口

二、流程图

 三 、源码

AutoProxyRegistrar

AopConfigUtils

ProxyTransactionManagementConfiguration.class

JdkDynamicAopProxy下invoke()

ReflectiveMethodInvocation

MethodInterceptor

TransactionInterceptor类下的invoke()方法


一、入口

package com.xiaojie.spring;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/*
 * 
 * @param null 
 * 配置类
 * @author xiaojie 
 * @date 2021/8/24 
 * @return 
 */
@Configuration
@ComponentScan("com.xiaojie")
@EnableTransactionManagement
public class Config {

 /*
  *  
  * 加载数据源
  * @author xiaojie 
  * @date 2021/8/24
  * @return javax.sql.DataSource
  */
    @Bean
    DataSource dataSource(){
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setURL("jdbc:mysql://127.0.0.1:3306/order?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai");
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("root");
//        mysqlDataSource.setDatabaseName("order");
        return mysqlDataSource;
    }
    /*
     *  加载事务管理器
     * @author xiaojie 
     * @date 2021/8/24 
     * @return org.springframework.transaction.PlatformTransactionManager
     */
    @Bean
    PlatformTransactionManager platformTransactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }
    /*
     *数据库连接
     * @author xiaojie 
     * @date 2021/8/24 
     * @return org.springframework.jdbc.core.JdbcTemplate
     */
    @Bean
    JdbcTemplate jdbcTemplate(){
    return new JdbcTemplate(dataSource());
    }
}
@EnableTransactionManagement 为事务的入口

二、流程图

 三 、源码

AutoProxyRegistrar

            //默认是代理模式
            if (mode == AdviceMode.PROXY) {
                    //注册InfrastructureAdvisorAutoProxyCreator.class类到ioc容器
					AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
					if ((Boolean) proxyTargetClass) {
						AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
						return;
				}
			}

AopConfigUtils

private static BeanDefinition registerOrEscalateApcAsRequired(
			Class<?> cls, BeanDefinitionRegistry registry, @Nullable Object source) {

		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");

		if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
			BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
			if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
				int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
				int requiredPriority = findPriorityForClass(cls);
				if (currentPriority < requiredPriority) {
					apcDefinition.setBeanClassName(cls.getName());
				}
			}
			return null;
		}

		RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
		beanDefinition.setSource(source);
		beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
		beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        //AUTO_PROXY_CREATOR_BEAN_NAME=org.springframework.aop.config.internalAutoProxyCreator
		registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
		return beanDefinition;
	}

上述注册完之后会将InfrastructureAdvisorAutoProxyCreator.class类注入到IOC中

beanId:org.springframework.aop.config.internalAutoProxyCreator

Class:InfrastructureAdvisorAutoProxyCreator.class

 由类图InfrastructureAdvisorAutoProxyCreator.class继承了BeanPostProcessor同样会执行前置增强或者后置增强。

ProxyTransactionManagementConfiguration.class

//将TransactionInterceptor 类注入到IOC	
@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public TransactionInterceptor transactionInterceptor(TransactionAttributeSource transactionAttributeSource) {
		TransactionInterceptor interceptor = new TransactionInterceptor();
		interceptor.setTransactionAttributeSource(transactionAttributeSource);
		if (this.txManager != null) {
			interceptor.setTransactionManager(this.txManager);
		}
		return interceptor;
	}

再看BeanPostProcessor后置增强方法

 然后就会发现跟AOP的代码是一样的SpringAop学习笔记(三)——Aop源码分析

JdkDynamicAopProxy下invoke()

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object oldProxy = null;
		boolean setProxyContext = false;
        .........省略
        // Get the interception chain for this method.
			List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

			// Check whether we have any advice. If we don't, we can fallback on direct
			// reflective invocation of the target, and avoid creating a MethodInvocation.
			if (chain.isEmpty()) {
				// We can skip creating a MethodInvocation: just invoke the target directly
				// Note that the final invoker must be an InvokerInterceptor so we know it does
				// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
				Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
				retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
			}
			else {
				// We need to create a method invocation...
				MethodInvocation invocation =
						new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
				// Proceed to the joinpoint through the interceptor chain.
                //由此进入proceed()方法
				retVal = invocation.proceed();
			}
		.........省略
	}

ReflectiveMethodInvocation

public Object proceed() throws Throwable {
		// We start with an index of -1 and increment early.
		if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
			return invokeJoinpoint();
		}

		Object interceptorOrInterceptionAdvice =
				this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
		if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
			// Evaluate dynamic method matcher here: static part will already have
			// been evaluated and found to match.
			InterceptorAndDynamicMethodMatcher dm =
					(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
			Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
			if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
				return dm.interceptor.invoke(this);
			}
			else {
				// Dynamic matching failed.
				// Skip this interceptor and invoke the next in the chain.
				return proceed();
			}
		}
		else {
			// It's an interceptor, so we just invoke it: The pointcut will have
			// been evaluated statically before this object was constructed.
            //此处用到了责任链设计模式+递归思想实现所有的通知增强
			return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
		}
	}

MethodInterceptor

Object invoke(@Nonnull MethodInvocation invocation) throws Throwable;

 

TransactionInterceptor类下的invoke()方法

   protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, TransactionAspectSupport.InvocationCallback invocation) throws Throwable {
       ......省略
         TransactionAspectSupport.TransactionInfo txInfo = this.createTransactionIfNecessary(ptm, txAttr, joinpointIdentification);

                Object retVal;
                try {
                    //执行目标方法
                    retVal = invocation.proceedWithInvocation();
                } catch (Throwable var20) {
                    //发生异常 则执行事务回滚
                    this.completeTransactionAfterThrowing(txInfo, var20);
                    throw var20;
                } finally {
                //将当前事务归还ThreadLocal<TransactionAspectSupport.TransactionInfo>
                    this.cleanupTransactionInfo(txInfo);
                }

                if (retVal != null && vavrPresent &&             TransactionAspectSupport.VavrDelegate.isVavrTry(retVal)) {
                    TransactionStatus status = txInfo.getTransactionStatus();
                    if (status != null && txAttr != null) {
                        retVal = TransactionAspectSupport.VavrDelegate.evaluateTryFailure(retVal, txAttr, status);
                    }
                }
                //提交事务
                this.commitTransactionAfterReturning(txInfo);
                return retVal;

       .........省略
    }

Spring 版本为5.3.9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熟透的蜗牛

永远满怀热爱,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值