目錄
- 一、@EnableTransactionManagement
- 二、加載事務控制組件
宣告式事務很方便,尤其純注解模式,僅僅幾個注解就能控制事務了
思考:這些注解都做了什么?好神奇!
@EnableTransactionManagement @Transactional
一、@EnableTransactionManagement
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
}
@EnableTransactionManagement 注解使用 @Import 標簽 參考了 TransactionManagementConfigurationSelector類,這個類又向容器中匯入了兩個重要的組件

本文參考資料《Spring高級原始碼筆記》,需要的同學添加助理VX:
C18173184271,備注一下CSDN+作業年限!免費獲取
二、加載事務控制組件
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 思想的一種應用
ProxyTransactionManagementConfiguration組件
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.BeanFactoryTransactionAttribut eSourceAdvisor;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
/**
* {@code @Configuration} class that registers the Spring infrastructure
beans
* necessary to enable proxy-based annotation-driven transaction
management.
*
* @author Chris Beams
* @since 3.1
* @see EnableTransactionManagement
* @see TransactionManagementConfigurationSelector
*/
@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事務攔截器,部分原始碼如下


- 上述組件如何關聯起來的?
- 事務攔截器實作了 MethodInterceptor介面,追溯一下上面提到的
InfrastructureAdvisorAutoProxyCreator后置處理器,它會在代理物件執行目標方法的時候獲取其攔截器鏈,而攔截器鏈就是這個TransactionInterceptor,這就把這兩個組件聯系起來; - 構造方法傳入
PlatformTransactionManager(事務管理器)、TransactionAttributeSource(屬性決議器),但是追溯一下上面貼的ProxyTransactionManagementConfiguration的原始碼,在注冊事務攔截器的時候并沒有呼叫這個帶參構造方法,而是呼叫的無參構造方法,然后再呼叫set方法注入這兩個屬性,效果一樣,
- 事務攔截器實作了 MethodInterceptor介面,追溯一下上面提到的
invokeWithinTransaction方法,部分原始碼如下(關注1、2、3、4 標注處)


如果你需要這份完整版的《Spring高級原始碼筆記》,只需你多多支持我這篇文章,
多多支持,即可免費獲取資料——三連之后(承諾:100%免費)
快速入手通道:添加助理VX:C18173184271,備注一下CSDN+作業年限! 免費獲取!誠意滿滿!!!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/253082.html
標籤:java

