Spring 事务——源码分析
【事务环境搭建工作】:链接
注解 @EnableTransactionManagement:在配置类中添加注解@EnableTransactionManagement,便开启了事务功能。此注解也是了解Spring事务源码的入口。
@EnableTransactionManagement public class TxConfig {
【1】@EnableTransactionManagement 利用 TransactionManagementConfigurationSelector 给容器中导入组件:如下代码所示,SelectorImposrts共导入了两个组件:①、AutoProxyRegistrar、②、ProxyTransactionManagementConfiguration
// xxxSelector 继承自 ImportSelector 表示给容器中导入组件 @Import({TransactionManagementConfigurationSelector.class}) public @interface EnableTransactionManagement {......} //进入TransactionManagementConfigurationSelector的selectImports方法,发现导入两个组件 public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> { protected String[] selectImports(AdviceMode adviceMode) { switch(null.$SwitchMap$org$springframework$context$annotation$AdviceMode[adviceMode.ordinal()]) { case 1: //导入的两个组件:AutoProxyRegistrar、ProxyTransactionManagementConfiguration return new String[]{AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()}; } }
【2】组件一:AutoProxyRegistrar 给容器中注册了一个 InfrastructureAdvisorAutoProxyCreator 组件。该组件继承自xxxAwareBeanPostProcessor后置处理器,利用后置处理器机制在对象创建以后进行包装,返回一个代理对象(有增强器),代理对象通过拦截器链进行调用。
//通过继承类《BeanDefinitionRegister》可以猜到该类的功能,主要是给容器中注册Bean的属性 public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar { public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { //...... AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry); } } //进入上面的registerxxx方法:创建一个自动代理创建器 public static BeanDefinition registerAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry) { return registerAutoProxyCreatorIfNecessary(registry, (Object)null); } //进入上面registerxxx 发现它给容器中注册了一个 InfrastructureAdvisorAutoProxyCreator 组件 public static BeanDefinition registerAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, @Nullable Object source) { return registerOrEscalateApcAsRequired(InfrastructureAdvisorAutoProxyCreator.class, registry, source); }
【3】ProxyTransactionManagementConfiguration 是一个配置类,如下:
第一步:给容器中注册事务增强器:1)、主要用于对事务注解 @Transactional 中的属性进行解析。给容器中注入:AnnotationTransactionAttributeSource 实例。
@Configuration( proxyBeanMethods = false ) public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration { //注册事务增强器 @Bean( name = {"org.springframework.transaction.config.internalTransactionAdvisor"} ) @Role(2) public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(TransactionAttributeSource transactionAttributeSource, TransactionInterceptor transactionInterceptor) { BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor(); //设置一个事务属性 advisor.setTransactionAttributeSource(transactionAttributeSource); //设置一个事务拦截器 advisor.setAdvice(transactionInterceptor); if(this.enableTx != null) { advisor.setOrder(((Integer)this.enableTx.getNumber("order")).intValue()); } return advisor; } //事务属性源码展示:也是一个bean,注入到容器中 @Bean @Role(2) public TransactionAttributeSource transactionAttributeSource() { return new AnnotationTransactionAttributeSource(); } //注入的bean对象源码展示:主要功能使用注解解析器对事务注解进行解析 publicMethodsOnly=true public AnnotationTransactionAttributeSource(boolean publicMethodsOnly) { this.publicMethodsOnly = publicMethodsOnly; this.annotationParsers = new LinkedHashSet(4); //事务注解解析器 this.annotationParsers.add(new SpringTransactionAnnotationParser()); if(jta12Present) { this.annotationParsers.add(new JtaTransactionAnnotationParser()); } if(ejb3Present) { this.annotationParsers.add(new Ejb3TransactionAnnotationParser()); } } } //进入事务注解解析器 SpringTransactionAnnotationParser 中的parseTransactionAnnotation方法 //里面的都是都是可以在@Transactional中配置的属性 protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) { RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); Propagation propagation = (Propagation)attributes.getEnum("propagation"); rbta.setPropagationBehavior(propagation.value()); Isolation 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(); Class[] var6 = attributes.getClassArray("rollbackFor"); int var7 = var6.length; //...... }
2)、事务拦截器 :TransactionInterceptor 继承自 MethodInterceptor【方法拦截器:目前给容器中放置了一个代理对象,代理对象要执行目标方法时,方法拦截器就进行工作】 ,保存了事务属性信息和事务管理器;在目标方法执行的时候,代理对象会执行拦截器链【TransactionInterceptor 】,TransactionInterceptor(事务拦截器) 的作用:①、获取事务相关属性;②、获取平台事务管理器【platformTransactionManager 】获取方式:首先会根据 Transaction 中定义的 Qualifier 属性获取事务管理器,如果未定义则根据 TransactionManagerBeanName 获取,如果事先没有指定,则根据自动装配的值获取,如果没有指定,最终会从容器中按照类型获取一个 PlatformTransactionManager;③、开启事务、执行目标方法、如果发生异常,利用事务管理器回滚操作。如果正常,则利用事务管理器提交事务。
//继承了方法拦截器 public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable { //拦截器工作通过调用invoke进行触发。 @Nullable public Object invoke(MethodInvocation invocation) throws Throwable { Class<?> targetClass = invocation.getThis() != null?AopUtils.getTargetClass(invocation.getThis()):null; Method var10001 = invocation.getMethod(); invocation.getClass(); //******重点,通过事务进行工作**** 进入该方法 return this.invokeWithinTransaction(var10001, targetClass, invocation::proceed); } /** * 事务拦截器的作用 */ @Nullable protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, TransactionAspectSupport.InvocationCallback invocation) throws Throwable { //省略部分非重要代码 //获取事务的属性 TransactionAttribute txAttr = tas != null?tas.getTransactionAttribute(method, targetClass):null; //获取事务的管理器 ,附源码 PlatformTransactionManager tm = this.determineTransactionManager(txAttr); //得到要执行的事务方法 String joinpointIdentification = this.methodIdentification(method, targetClass, txAttr); //开启事务 TransactionAspectSupport.TransactionInfo txInfo = this.createTransactionIfNecessary(tm, txAttr, joinpointIdentification); try { //invocation 事务方法 下面是执行事务方法 retVal = invocation.proceedWithInvocation(); } catch (Throwable var17) { //进入源码,发现是通过事务管理器进行回滚 this.completeTransactionAfterThrowing(txInfo, var17); throw var17; } finally { this.cleanupTransactionInfo(txInfo); } //...... //如果正常执行,则提交事务 this.commitTransactionAfterReturning(txInfo); return retVal; } //获取事务管理器的源码,首先会根据Transaction中定义的qualifier值获取管理器 @Nullable protected PlatformTransactionManager determineTransactionManager(@Nullable TransactionAttribute txAttr) { if(txAttr != null && this.beanFactory != null) { String qualifier = txAttr.getQualifier(); if(StringUtils.hasText(qualifier)) { return this.determineQualifiedTransactionManager(this.beanFactory, qualifier); //如果qualifier 是空的则根据 transactionManagerBeanName } else if(StringUtils.hasText(this.transactionManagerBeanName)) { return this.determineQualifiedTransactionManager(this.beanFactory, this.transactionManagerBeanName); } else { //如果都没指定,则根据自动装配的值获取 PlatformTransactionManager defaultTransactionManager = this.asPlatformTransactionManager(this.getTransactionManager()); if(defaultTransactionManager == null) { defaultTransactionManager = this.asPlatformTransactionManager(this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY)); //这是我们一般会最终调用的地方,会按照类型获取事务管理器 PlatformTransactionManager if(defaultTransactionManager == null) { defaultTransactionManager = (PlatformTransactionManager)this.beanFactory.getBean(PlatformTransactionManager.class); this.transactionManagerCache.putIfAbsent(DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); } } return defaultTransactionManager; } } } }