亲宝软件园·资讯

展开

Spring声明式事务注解

悠然予夏 人气:0

声明式事务很方便,尤其纯注解模式,仅仅几个注解就能控制事务了

思考:这些注解都做了什么?好神奇!

@EnableTransactionManagement @Transactional

1、@EnableTransactionManagement

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {

@EnableTransactionManagement 注解使用@Import 标签引入了TransactionManagementConfigurationSelector类,这个类又向容器中导入了两个重要的组件

2、加载事务控制组件

2.1、AutoProxyRegistrar

AutoProxyRegistrar 类的 registerBeanDefinitions ⽅法中⼜注册了⼀个组件

进入AopConfigUtils.registerAutoProxyCreatorIfNecessary 方法

发现最终,注册了⼀个叫做 InfrastructureAdvisorAutoProxyCreator 的 Bean,而这个类是AbstractAutoProxyCreator 的子类,实现了 SmartInstantiationAwareBeanPostProcessor 接口

public class InfrastructureAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator
public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware

继承体系结构图如下

它实现了SmartInstantiationAwareBeanPostProcessor,说明这是⼀个后置处理器,而且跟spring AOP 开启@EnableAspectJAutoProxy 时注册的 AnnotationAwareAspectJProxyCreator实

现的是同⼀个接口,所以说,声明式事务是 springAOP 思想的⼀种应用

2.2、ProxyTransactionManagementConfiguration 组件

@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
    @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
// 事务增强器
        BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
// 向事务增强器中注⼊ 属性解析器 transactionAttributeSource
        advisor.setTransactionAttributeSource(transactionAttributeSource());
// 向事务增强器中注⼊ 事务拦截器 transactionInterceptor
        advisor.setAdvice(transactionInterceptor());
        if (this.enableTx != null) {
            advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
        }
        return advisor;
    }
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
// 属性解析器 transactionAttributeSource
    public TransactionAttributeSource transactionAttributeSource() {
        return new AnnotationTransactionAttributeSource();
    }
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
// 事务拦截器 transactionInterceptor
    public TransactionInterceptor transactionInterceptor() {
        TransactionInterceptor interceptor = new TransactionInterceptor();
        interceptor.setTransactionAttributeSource(transactionAttributeSource());
        if (this.txManager != null) {
            interceptor.setTransactionManager(this.txManager);
        }
        return interceptor;
    }
}

ProxyTransactionManagementConfiguration是⼀个容器配置类,注册了⼀个组件transactionAdvisor,称为事务增强器,然后在这个事务增强器中又注入了两个属性:transactionAttributeSource,即属性解析器transactionAttributeSource 和 事务拦截器transactionInterceptor

属性解析器 AnnotationTransactionAttributeSource 部分源码如下

属性解析器有⼀个成员变量是annotationParsers,是⼀个集合,可以添加多种注解解析器(TransactionAnnotationParser),我们关注 Spring 的注解解析器,部分源码如下

属性解析器的作⽤之⼀就是⽤来解析@Transaction注解

TransactionInterceptor 事务拦截器,部分源码如下

2.3、上述组件如何关联起来的

2.4、invokeWithinTransaction⽅法

部分源码如下(关注1、2、3、4 标注处)

加载全部内容

相关教程
猜你喜欢
用户评论