啟用Spring事務(wù)管理@EnableTransactionManagement示例解析
Spring事務(wù)管理
Spring事務(wù)管理可以通過(guò)@EnableTransactionManagement注解開啟,通過(guò)對(duì)@EnableTransactionManagement的分析,就能揭開Spring啟用事務(wù)的底層機(jī)制。
直接開始源碼分析。
@EnableTransactionManagement注解作用在配置類上,引入了TransactionManagementConfigurationSelector,這個(gè)先放一放,后面再分析。
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(TransactionManagementConfigurationSelector.class) public @interface EnableTransactionManagement {
我們先看一下他定義的方法:
boolean proxyTargetClass() default false; AdviceMode mode() default AdviceMode.PROXY; int order() default Ordered.LOWEST_PRECEDENCE;
定義了3個(gè)方法,我們逐個(gè)分析一下:
proxyTargetClass
boolean proxyTargetClass() default false;
Indicate whether subclass-based (CGLIB) proxies are to be created (true) as opposed to standard Java interface-based proxies (false). The default is false. Applicable only if mode() is set to AdviceMode.PROXY.
Note that setting this attribute to true will affect all Spring-managed beans requiring proxying, not just those marked with @Transactional. For example, other beans marked with Spring's @Async annotation will be upgraded to subclass proxying at the same time. This approach has no negative impact in practice unless one is explicitly expecting one type of proxy vs another, e.g. in tests.
決定采用CGLIB的方式(設(shè)置為TRUE)、還是JDK方式創(chuàng)建代理類(設(shè)置為false),默認(rèn)值為false。僅在Mode設(shè)置為PROXY的情況下才有效。
需要注意的是設(shè)置為true后會(huì)影響所有的的被Spring管理的代理類的創(chuàng)建方式,不止是@Transactional注解標(biāo)注的類。比如,其他的被@Async標(biāo)注的類也會(huì)被影響。
大概意思是說(shuō),在Mode設(shè)置為PROXY的情況下,這個(gè)參數(shù)決定代理類的創(chuàng)建方式,設(shè)置為true則通過(guò)CGLIB方式創(chuàng)建代理對(duì)象,設(shè)置為false則通過(guò)JDK方式創(chuàng)建代理對(duì)象。默認(rèn)是通過(guò)JDK方式創(chuàng)建。
但是為什么設(shè)置為true后會(huì)影響到不止是@Transactional注解標(biāo)注的類,而是會(huì)影響到整個(gè)Spring管理的bean呢?待考證。
mode
AdviceMode mode() default AdviceMode.PROXY;
Indicate how transactional advice should be applied.
The default is AdviceMode.PROXY. Please note that proxy mode allows for interception of calls through the proxy only. Local calls within the same class cannot get intercepted that way; an Transactional annotation on such a method within a local call will be ignored since Spring's interceptor does not even kick in for such a runtime scenario. For a more advanced mode of interception, consider switching this to AdviceMode.ASPECTJ.
事務(wù)advice(AOP概念)的實(shí)現(xiàn)方式,默認(rèn)值為PXOXY,注意PROXY模式只允許通過(guò)代理方式調(diào)用,本地調(diào)用不生效。事務(wù)注解方法如果通過(guò)本地調(diào)用的話,事務(wù)處理不會(huì)生效,因?yàn)檫@種情況下Spring AOP的攔截機(jī)制不會(huì)生效。
JavaDoc說(shuō)的很明白了,也是我們?cè)谄渌恼吕锩嫣岬竭^(guò)的,如果在同一個(gè)類中一個(gè)方法A調(diào)用被@Transactional標(biāo)注的另外一個(gè)方法B的話,方法B的@Transactional不會(huì)生效,因?yàn)檫@種情況下的調(diào)用就屬于這里所說(shuō)的“本地調(diào)用”,沒有調(diào)用到代理類,AOP攔截機(jī)制不會(huì)生效,因此方法B不會(huì)被AOP增強(qiáng),因此事務(wù)不會(huì)生效。
Ordered
很熟悉的AOP增強(qiáng)概念,當(dāng)多個(gè)攔截器生效的情況下,決定其順序。
int order() default Ordered.LOWEST_PRECEDENCE;
好了,今天就到這里,由于Spring源碼本身比較復(fù)雜,一次分析太多、文章太大的話,會(huì)比較亂,所以,改變一下思路,一篇文章只說(shuō)一個(gè)概念,控制文章的篇幅,增強(qiáng)可讀性。
以上就是啟用Spring事務(wù)管理@EnableTransactionManagement示例解析的詳細(xì)內(nèi)容,更多關(guān)于Spring事務(wù)管理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Spring聲明式事務(wù)注解之@EnableTransactionManagement解析
- spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫(kù)事務(wù)管理
- Spring框架JdbcTemplate數(shù)據(jù)庫(kù)事務(wù)管理完全注解方式
- 解析spring事務(wù)管理@Transactional為什么要添加rollbackFor=Exception.class
- SpringBoot2整合JTA組件實(shí)現(xiàn)多數(shù)據(jù)源事務(wù)管理
- 五分鐘教你手寫 SpringBoot 本地事務(wù)管理實(shí)現(xiàn)
- Spring事務(wù)管理原理及方法詳解
相關(guān)文章
Spring boot中使用ElasticSearch的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring boot中使用ElasticSearch的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Spring?Boot超大文件上傳實(shí)現(xiàn)秒傳功能
這篇文章主要介紹了Spring?Boot超大文件上傳實(shí)現(xiàn)秒傳功能,在實(shí)現(xiàn)分片上傳的過(guò)程,需要前端和后端配合,比如前后端的上傳塊號(hào)的文件大小,前后端必須得要一致,否則上傳就會(huì)有問題,需要的朋友可以參考下2022-12-12非常適合新手學(xué)生的Java線程池超詳細(xì)分析
作者是一個(gè)來(lái)自河源的大三在校生,以下筆記都是作者自學(xué)之路的一些淺薄經(jīng)驗(yàn),如有錯(cuò)誤請(qǐng)指正,將來(lái)會(huì)不斷的完善筆記,幫助更多的Java愛好者入門2022-03-03Spring Boot 整合mybatis 與 swagger2
之前使用springMVC+spring+mybatis,總是被一些繁瑣的xml配置,還經(jīng)常出錯(cuò),下面把以前的一些ssm項(xiàng)目改成了spring boot + mybatis,相對(duì)于來(lái)說(shuō)優(yōu)點(diǎn)太明顯了,具體內(nèi)容詳情大家通過(guò)本文學(xué)習(xí)吧2017-08-08SpringBoot+Response如何統(tǒng)一返回result結(jié)果集
這篇文章主要介紹了SpringBoot+Response如何統(tǒng)一返回result結(jié)果集,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Mybatis-Plus中updateById方法不能更新空值問題解決
本文主要介紹了Mybatis-Plus中updateById方法不能更新空值問題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08