服务器之家

服务器之家 > 正文

Spring声明式事务注解之@EnableTransactionManagement解析

时间:2021-11-11 10:53     来源/作者:金一白

Spring声明式事务注解之@EnableTransactionManagement

1. 说明

@EnableTransactionManagement声明在主配置类上,表示开启声明式事务,其原理是通过@Import导入TransactionManagementConfigurationSelector组件,然后又通过TransactionManagementConfigurationSelector导入组件AutoProxyRegistrar和ProxyTransactionManagementConfiguration;

2. 原理分析

@EnableTransactionManagement代码实现如下:

?
1
2
3
4
5
6
7
8
9
10
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 通过@Import导入TransactionManagementConfigurationSelector组件
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
 boolean proxyTargetClass() default false;
 AdviceMode mode() default AdviceMode.PROXY;
 int order() default Ordered.LOWEST_PRECEDENCE;
}

@EnableTransactionManagement通过@Import导入TransactionManagementConfigurationSelector;

TransactionManagementConfigurationSelector的实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {
 
 /**
  * {@inheritDoc}
  * @return {@link ProxyTransactionManagementConfiguration} or
  * {@code AspectJTransactionManagementConfiguration} for {@code PROXY} and
  * {@code ASPECTJ} values of {@link EnableTransactionManagement#mode()}, respectively
  */
 @Override
 protected String[] selectImports(AdviceMode adviceMode) {
  switch (adviceMode) {
   case PROXY:
    // 根据@EnableTransactionManagement的固定值PROXY,这里会导入AutoProxyRegistrar组件和ProxyTransactionManagementConfiguration组件
    return new String[] {AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()};
   case ASPECTJ:
    return new String[] {TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME};
   default:
    return null;
  }
 }
}

所以TransactionManagementConfigurationSelector又导入了组件AutoProxyRegistrar和ProxyTransactionManagementConfiguration;

3. AutoProxyRegistrar分析

3.1 AutoProxyRegistrar继承关系

?
1
2
3
4
5
6
7
8
InfrastructureAdvisorAutoProxyCreator
    --AbstractAdvisorAutoProxyCreator
        --AbstractAdvisorAutoProxyCreator
            --ProxyProcessorSupport
            --SmartInstantiationAwareBeanPostProcessor  // 跟AOP是原理是一样的
                --InstantiationAwareBeanPostProcessor
                    --BeanPostProcessor
            --BeanFactoryAware

3.2 AutoProxyRegistrar的所用

AutoProxyRegistrar的作用跟AOP中的AnnotationAwareAspectJAutoProxyCreator是一样的,利用后置处理器机制在对象创建以后,包装对象,返回一个代理对象(增强器),代理对象执行方法利用拦截器链进行调用;InfrastructureAdvisorAutoProxyCreator继承SmartInstantiationAwareBeanPostProcessor,跟AOP的原理是一样的,也是通过@Transactional作为方法拦截的标记,把有事务管理的类作为目标类,生成代理对象,然后增强@Transactional标记的方法,在使用目标方法的时候,从IOC容器中获取的其实是被增强的代理类,且事务方法会被代理,跟AOP原理一样的;

4. ProxyTransactionManagementConfiguration分析

ProxyTransactionManagementConfiguration是一个配置类,想IOC容器中导入事务增强器(BeanFactoryTransactionAttributeSourceAdvisor),事务注解@Transactional的解析器(AnnotationTransactionAttributeSource)和事务方法拦截器(TransactionInterceptor);

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.springframework.transaction.annotation;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.transaction.config.TransactionManagementConfigUtils;
import org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
 
@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
 
    
    /**
        事务增强器(Advisor),在事务类创建的时候,被AutoProxyRegistrar导入的组件InfrastructureAdvisorAutoProxyCreator拦截,
        InfrastructureAdvisorAutoProxyCreator拦截的逻就是增强事务类的事务方法,  而BeanFactoryTransactionAttributeSourceAdvisor作为增强器,
        与需要增强的方法(这里是指被@Transactional标记的方法)进行匹配,匹配成功的增强器,最后转成拦截器(MethodInterceptor,
        就是下面的TransactionInterceptor),然后与目标方法一起在拦截器链中被执行,达到方法增强的效果;
        
        
        BeanFactoryTransactionAttributeSourceAdvisor的继承关系如下:
        
        BeanFactoryTransactionAttributeSourceAdvisor
            --AbstractBeanFactoryPointcutAdvisor
                --AbstractPointcutAdvisor
                    --PointcutAdvisor
                        --Advisor
        
        AOP中AspectJPointcutAdvisor的继承关系如下,与AbstractPointcutAdvisor一样,都实现PointcutAdvisor
                --AspectJPointcutAdvisor
                    --PointcutAdvisor
                        --Advisor
    */
    @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
        BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
        advisor.setTransactionAttributeSource(transactionAttributeSource());
        advisor.setAdvice(transactionInterceptor());
        advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
        return advisor;
    }
 
    /**
        @Transactional注解的解析类;负责解析事务方法上@Transactional中的各个参数配置,解析的时机是在创建事务类之后被增强的时候,
        匹配事务方法的时候一起被解析了
        
        AnnotationTransactionAttributeSource的继承关系如下:
        
        AnnotationTransactionAttributeSource
            --AbstractFallbackTransactionAttributeSource
                --TransactionAttributeSource
        通过方法org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.getTransactionAttribute(Method, Class<?>)
        解析出事务信息TransactionAttribute;
        
        AnnotationTransactionAttributeSource在方法findTransactionAttribute(Class<?>)中依赖于SpringTransactionAnnotationParser在解析事务类时,
        绑定事务方法与增强器的时候进行@Transactional注解解析;
        
    */
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public TransactionAttributeSource transactionAttributeSource() {
        return new AnnotationTransactionAttributeSource();
    }
 
    /**
        被@Transactional标记的事务方法的拦截器,实际是一个MethodInterceptor
        保存了事务属性信息,事务管理器;
        在目标方法执行的时候;执行拦截器链;
    */
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public TransactionInterceptor transactionInterceptor() {
        TransactionInterceptor interceptor = new TransactionInterceptor();
        interceptor.setTransactionAttributeSource(transactionAttributeSource());
        if (this.txManager != null) {
            interceptor.setTransactionManager(this.txManager);
        }
        return interceptor;
    }
}

在SpringTransactionAnnotationParser中parseTransactionAnnotation方法来解析@Transactional中的各个参数,其具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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"));
    ArrayList<RollbackRuleAttribute> rollBackRules = new ArrayList<RollbackRuleAttribute>();
    Class<?>[] rbf = attributes.getClassArray("rollbackFor");
    for (Class<?> rbRule : rbf) {
        RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    String[] rbfc = attributes.getStringArray("rollbackForClassName");
    for (String rbRule : rbfc) {
        RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    Class<?>[] nrbf = attributes.getClassArray("noRollbackFor");
    for (Class<?> rbRule : nrbf) {
        NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    String[] nrbfc = attributes.getStringArray("noRollbackForClassName");
    for (String rbRule : nrbfc) {
        NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
        rollBackRules.add(rule);
    }
    rbta.getRollbackRules().addAll(rollBackRules);
    return rbta;
}

spring 事务 @EnableTransactionManagement原理

@EnableXXX原理:注解上有个XXXRegistrar,或通过XXXSelector引入XXXRegistrar,XXXRegistrar实现了ImportBeanDefinitionRegistrar的registerBeanDefinitions方法,给容器注册XXXCreator。

这个Creator实现了后置处理器,后置处理器在对象创建以后,包装对象,返回一个代理对象,代理对象执行方法利用拦截器链进行调用

1)、@EnableTransactionManagement

利用TransactionManagementConfigurationSelector给容器中会导入组件

导入两个组件

?
1
2
AutoProxyRegistrar
ProxyTransactionManagementConfiguration

2)、AutoProxyRegistrar:

给容器中注册一个 InfrastructureAdvisorAutoProxyCreator 组件;

利用后置处理器机制在对象创建以后,包装对象,返回一个代理对象(增强器),代理对象执行方法利用拦截器链进行调用;

3)、ProxyTransactionManagementConfiguration是个@Configuration

1、给容器中注册事务增强器transactionAdvisor;

1)、事务增强器要用事务注解的信息,AnnotationTransactionAttributeSource解析事务注解

2)、事务拦截器transactionInterceptor:

TransactionInterceptor;保存了事务属性信息,事务管理器;

TransactionInterceptor是一个 MethodInterceptor;

在目标方法执行的时候;

执行拦截器链;

只有事务拦截器:

1)、先获取事务相关的属性

2)、再获取PlatformTransactionManager,如果事先没有添加指定任何transactionmanger 最终会从容器中按照类型获取一个PlatformTransactionManager;

3)、执行目标方法

如果异常,获取到事务管理器,利用事务管理回滚操作;

如果正常,利用事务管理器,提交事务

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/luojinbai/article/details/87725112

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部