Spring源码系列07——Spring事务管理源码介绍

背景知识

spring事务传播行为

一、开启事务

添加注解:@EnableTransactionManagement

package org.springframework.transaction.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({TransactionManagementConfigurationSelector.class})
public @interface EnableTransactionManagement {
    boolean proxyTargetClass() default false;

    AdviceMode mode() default AdviceMode.PROXY;

    int order() default 2147483647;
}

其中TransactionManagementConfigurationSelector实现了ImportSelector接口,注入了如下的bean:
(1)AutoProxyRegistrar
(2)ProxyTransactionManagementConfiguration

  protected String[] selectImports(AdviceMode adviceMode) {
        switch(adviceMode) {
        case PROXY:
            return new String[]{AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()};
        case ASPECTJ:
            return new String[]{"org.springframework.transaction.aspectj.AspectJTransactionManagementConfiguration"};
        default:
            return null;
        }
    }

(1)AutoProxyRegistrar

public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar
实现了ImportBeanDefinitionRegistrar接口,在方法registerBeanDefinitions()中,注入了InfrastructureAdvisorAutoProxyCreator类;
在这里插入图片描述
InfrastructureAdvisorAutoProxyCreator实现了BeanPostProcessor接口,是一个bean的后置处理器;但是在同时开启AOP和事务的情况下, 该类会被覆盖,由AOP负责创建代理。

(2)ProxyTransactionManagementConfiguration

它是一个配置类。

@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
    public ProxyTransactionManagementConfiguration() {
    }

    @Bean(
        name = {"org.springframework.transaction.config.internalTransactionAdvisor"}
    )
    @Role(2)
    public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
        BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
        advisor.setTransactionAttributeSource(this.transactionAttributeSource());
        advisor.setAdvice(this.transactionInterceptor());
        advisor.setOrder((Integer)this.enableTx.getNumber("order"));
        return advisor;
    }

    @Bean
    @Role(2)
    public TransactionAttributeSource transactionAttributeSource() {
        return new AnnotationTransactionAttributeSource();
    }

    @Bean
    @Role(2)
    public TransactionInterceptor transactionInterceptor() {
        TransactionInterceptor interceptor = new TransactionInterceptor();
        interceptor.setTransactionAttributeSource(this.transactionAttributeSource());
        if (this.txManager != null) {
            interceptor.setTransactionManager(this.txManager);
        }

        return interceptor;
    }
}

其中BeanFactoryTransactionAttributeSourceAdvisor主要用来处理事务。

二、解析advisor

在第一次的beanpostprocess中,将advisor放到advisors集合中

三、创建动态代理

利用AnnotationTransactionAttributeSource类的方法,按照方法——父类——接口的顺序匹配事务注解,匹配成功则创建事务的动态代理。

四、执行方法

拿到之前解析的事务属性。

try{
	责任链调用具体的数据库操作
} catch (){
	回滚事务
}
cmmit;

调用过程简单的可以理解为上面的伪代码。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值