欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring深入刨析聲明式事務(wù)注解的源碼

 更新時(shí)間:2022年07月16日 09:34:11   作者:悠然予夏  
在spring注解中,使用聲明式事務(wù),需要用到兩個(gè)核心的注解:@Transactional注解和@EnableTransactionManagement注解。將@Transactional注解加在方法上,@EnableTransactionManagement注解加在配置類上

聲明式事務(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 ?法中?注冊(cè)了?個(gè)組件

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

發(fā)現(xiàn)最終,注冊(cè)了?個(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,說(shuō)明這是?個(gè)后置處理器,而且跟spring AOP 開(kāi)啟@EnableAspectJAutoProxy 時(shí)注冊(cè)的 AnnotationAwareAspectJProxyCreator實(shí)

現(xiàn)的是同?個(gè)接口,所以說(shuō),聲明式事務(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è)容器配置類,注冊(cè)了?個(gè)組件transactionAdvisor,稱為事務(wù)增強(qiáng)器,然后在這個(gè)事務(wù)增強(qiáng)器中又注入了兩個(gè)屬性:transactionAttributeSource,即屬性解析器transactionAttributeSource 和 事務(wù)攔截器transactionInterceptor

屬性解析器 AnnotationTransactionAttributeSource 部分源碼如下

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

屬性解析器的作?之?就是?來(lái)解析@Transaction注解

TransactionInterceptor 事務(wù)攔截器,部分源碼如下

2.3、上述組件如何關(guān)聯(lián)起來(lái)的

  • 事務(wù)攔截器實(shí)現(xiàn)了MethodInterceptor接口,追溯?下上面提到的InfrastructureAdvisorAutoProxyCreator后置處理器,它會(huì)在代理對(duì)象執(zhí)行目標(biāo)方法的時(shí)候獲取其攔截器鏈,而攔截器鏈就是這個(gè)TransactionInterceptor,這就把這兩個(gè)組件聯(lián)系起來(lái);
  • 構(gòu)造方法傳?PlatformTransactionManager(事務(wù)管理器)、TransactionAttributeSource(屬性解析器),但是追溯一下上?貼的ProxyTransactionManagementConfiguration的源碼,在注冊(cè)事務(wù)攔截器的時(shí)候并沒(méi)有調(diào)用這個(gè)帶參構(gòu)造方法,而是調(diào)用的無(wú)參構(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring-Bean創(chuàng)建對(duì)象的步驟方式詳解

    Spring-Bean創(chuàng)建對(duì)象的步驟方式詳解

    在本篇文章里小編給大家分享的是關(guān)于Spring-Bean創(chuàng)建對(duì)象的步驟方式詳解內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)下。
    2020-02-02
  • Java的invoke方法的具體使用

    Java的invoke方法的具體使用

    本文主要介紹了Java的invoke方法的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 淺談Java中static和非static的區(qū)別

    淺談Java中static和非static的區(qū)別

    這篇文章主要介紹了Java中static和非static的相關(guān)內(nèi)容,小編覺(jué)得還是很不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-10-10
  • Java 中解決Unsupported major.minor version 51.0的問(wèn)題

    Java 中解決Unsupported major.minor version 51.0的問(wèn)題

    本文主要介紹解決Unsupported major.minor version 51.0的問(wèn)題, 這里給大家整理了詳細(xì)資料,有需要的小伙伴可以參考下
    2016-08-08
  • Java中l(wèi)ogback?自動(dòng)刷新不生效的問(wèn)題解決

    Java中l(wèi)ogback?自動(dòng)刷新不生效的問(wèn)題解決

    本文主要介紹了Java中l(wèi)ogback?自動(dòng)刷新不生效的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • SpringBoot+Spring Security無(wú)法實(shí)現(xiàn)跨域的解決方案

    SpringBoot+Spring Security無(wú)法實(shí)現(xiàn)跨域的解決方案

    這篇文章主要介紹了SpringBoot+Spring Security無(wú)法實(shí)現(xiàn)跨域的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • JAVA 并發(fā)容器的一些易出錯(cuò)點(diǎn)你知道嗎

    JAVA 并發(fā)容器的一些易出錯(cuò)點(diǎn)你知道嗎

    今天給大家?guī)?lái)的文章是Java并發(fā)編程的相關(guān)知識(shí),文中對(duì)java同步容器與并發(fā)容器做了非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-09-09
  • JAVA動(dòng)態(tài)維度笛卡爾積輸出的實(shí)現(xiàn)

    JAVA動(dòng)態(tài)維度笛卡爾積輸出的實(shí)現(xiàn)

    本文主要介紹了JAVA動(dòng)態(tài)維度笛卡爾積輸出的實(shí)現(xiàn),通過(guò)動(dòng)態(tài)生成笛卡爾積,可以方便地處理多維數(shù)據(jù)集,提高數(shù)據(jù)處理效率,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • postman中POST請(qǐng)求時(shí)參數(shù)包含參數(shù)list設(shè)置方式

    postman中POST請(qǐng)求時(shí)參數(shù)包含參數(shù)list設(shè)置方式

    這篇文章主要介紹了postman中POST請(qǐng)求時(shí)參數(shù)包含參數(shù)list設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • Java中的可重入鎖ReentrantLock簡(jiǎn)析

    Java中的可重入鎖ReentrantLock簡(jiǎn)析

    這篇文章主要介紹了Java中的可重入鎖ReentrantLock簡(jiǎn)析,可重入是指同一個(gè)線程如果首次獲得了這把鎖,那么因?yàn)樗沁@把鎖的擁有者,因此有權(quán)利再次獲取這把鎖如果是不可重入鎖,那么第二次獲得鎖時(shí),自己也會(huì)被鎖擋住,需要的朋友可以參考下
    2023-12-12

最新評(píng)論