Spring深入刨析聲明式事務(wù)注解的源碼
聲明式事務(wù)很方便,尤其純注解模式,僅僅幾個(gè)注解就能控制事務(wù)了
思考:這些注解都做了什么?好神奇!
@EnableTransactionManagement @Transactional
1、@EnableTransactionManagement
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {@EnableTransactionManagement 注解使用@Import 標(biāo)簽引入了TransactionManagementConfigurationSelector類,這個(gè)類又向容器中導(dǎo)入了兩個(gè)重要的組件

2、加載事務(wù)控制組件
2.1、AutoProxyRegistrar
AutoProxyRegistrar 類的 registerBeanDefinitions ?法中?注冊了?個(gè)組件

進(jìn)入AopConfigUtils.registerAutoProxyCreatorIfNecessary 方法

發(fā)現(xiàn)最終,注冊了?個(gè)叫做 InfrastructureAdvisorAutoProxyCreator 的 Bean,而這個(gè)類是AbstractAutoProxyCreator 的子類,實(shí)現(xiàn)了 SmartInstantiationAwareBeanPostProcessor 接口
public class InfrastructureAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator
public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
繼承體系結(jié)構(gòu)圖如下

它實(shí)現(xiàn)了SmartInstantiationAwareBeanPostProcessor,說明這是?個(gè)后置處理器,而且跟spring AOP 開啟@EnableAspectJAutoProxy 時(shí)注冊的 AnnotationAwareAspectJProxyCreator實(shí)
現(xiàn)的是同?個(gè)接口,所以說,聲明式事務(wù)是 springAOP 思想的?種應(yīng)用
2.2、ProxyTransactionManagementConfiguration 組件
@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
// 事務(wù)增強(qiáng)器
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
// 向事務(wù)增強(qiáng)器中注? 屬性解析器 transactionAttributeSource
advisor.setTransactionAttributeSource(transactionAttributeSource());
// 向事務(wù)增強(qiáng)器中注? 事務(wù)攔截器 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)
// 事務(wù)攔截器 transactionInterceptor
public TransactionInterceptor transactionInterceptor() {
TransactionInterceptor interceptor = new TransactionInterceptor();
interceptor.setTransactionAttributeSource(transactionAttributeSource());
if (this.txManager != null) {
interceptor.setTransactionManager(this.txManager);
}
return interceptor;
}
}ProxyTransactionManagementConfiguration是?個(gè)容器配置類,注冊了?個(gè)組件transactionAdvisor,稱為事務(wù)增強(qiáng)器,然后在這個(gè)事務(wù)增強(qiáng)器中又注入了兩個(gè)屬性:transactionAttributeSource,即屬性解析器transactionAttributeSource 和 事務(wù)攔截器transactionInterceptor
屬性解析器 AnnotationTransactionAttributeSource 部分源碼如下

屬性解析器有?個(gè)成員變量是annotationParsers,是?個(gè)集合,可以添加多種注解解析器(TransactionAnnotationParser),我們關(guān)注 Spring 的注解解析器,部分源碼如下

屬性解析器的作?之?就是?來解析@Transaction注解
TransactionInterceptor 事務(wù)攔截器,部分源碼如下


2.3、上述組件如何關(guān)聯(lián)起來的
- 事務(wù)攔截器實(shí)現(xiàn)了MethodInterceptor接口,追溯?下上面提到的InfrastructureAdvisorAutoProxyCreator后置處理器,它會(huì)在代理對象執(zhí)行目標(biāo)方法的時(shí)候獲取其攔截器鏈,而攔截器鏈就是這個(gè)TransactionInterceptor,這就把這兩個(gè)組件聯(lián)系起來;
- 構(gòu)造方法傳?PlatformTransactionManager(事務(wù)管理器)、TransactionAttributeSource(屬性解析器),但是追溯一下上?貼的ProxyTransactionManagementConfiguration的源碼,在注冊事務(wù)攔截器的時(shí)候并沒有調(diào)用這個(gè)帶參構(gòu)造方法,而是調(diào)用的無參構(gòu)造方法,然后再調(diào)用set方法注?這兩個(gè)屬性,效果?樣。
2.4、invokeWithinTransaction?法
部分源碼如下(關(guān)注1、2、3、4 標(biāo)注處)


到此這篇關(guān)于Spring深入刨析聲明式事務(wù)注解的源碼的文章就介紹到這了,更多相關(guān)Spring聲明式事務(wù)注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RabbitMQ中的Connection和Channel信道詳解
這篇文章主要介紹了RabbitMQ中的Connection和Channel信道詳解,信道是建立在 Connection 之上的虛擬連接,RabbitMQ 處理的每條 AMQP 指令都是通過信道完成的,需要的朋友可以參考下2023-08-08
Java中遍歷集合的并發(fā)修改異常解決方案實(shí)例代碼
當(dāng)你遍歷集合的同時(shí),又往集合中添加或者刪除元素,就可能報(bào)并發(fā)修改異常,下面這篇文章主要給大家介紹了關(guān)于Java中遍歷集合的并發(fā)修改異常解決方案的相關(guān)資料,需要的朋友可以參考下2022-12-12
java中for循環(huán)執(zhí)行的順序圖文詳析
關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2021-06-06
springboot結(jié)合mybatis操作事務(wù)配置的處理
在操作數(shù)據(jù)庫的時(shí)候,經(jīng)常會(huì)使用事務(wù)的處理,本文主要介紹了springboot結(jié)合mybatis操作事務(wù)配置的處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Java web含驗(yàn)證碼及權(quán)限登錄實(shí)例代碼
這篇文章主要介紹了Java web含驗(yàn)證碼及權(quán)限登錄實(shí)例代碼,所用到的開發(fā)工具為myeclipse10,MySQL數(shù)據(jù)庫,具體實(shí)現(xiàn)代碼大家參考下本文吧2017-03-03

