啟用Spring事務管理@EnableTransactionManagement示例解析
Spring事務管理
Spring事務管理可以通過@EnableTransactionManagement注解開啟,通過對@EnableTransactionManagement的分析,就能揭開Spring啟用事務的底層機制。
直接開始源碼分析。
@EnableTransactionManagement注解作用在配置類上,引入了TransactionManagementConfigurationSelector,這個先放一放,后面再分析。
@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個方法,我們逐個分析一下:
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的方式(設置為TRUE)、還是JDK方式創(chuàng)建代理類(設置為false),默認值為false。僅在Mode設置為PROXY的情況下才有效。
需要注意的是設置為true后會影響所有的的被Spring管理的代理類的創(chuàng)建方式,不止是@Transactional注解標注的類。比如,其他的被@Async標注的類也會被影響。
大概意思是說,在Mode設置為PROXY的情況下,這個參數決定代理類的創(chuàng)建方式,設置為true則通過CGLIB方式創(chuàng)建代理對象,設置為false則通過JDK方式創(chuàng)建代理對象。默認是通過JDK方式創(chuàng)建。
但是為什么設置為true后會影響到不止是@Transactional注解標注的類,而是會影響到整個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.
事務advice(AOP概念)的實現方式,默認值為PXOXY,注意PROXY模式只允許通過代理方式調用,本地調用不生效。事務注解方法如果通過本地調用的話,事務處理不會生效,因為這種情況下Spring AOP的攔截機制不會生效。
JavaDoc說的很明白了,也是我們在其他文章里面提到過的,如果在同一個類中一個方法A調用被@Transactional標注的另外一個方法B的話,方法B的@Transactional不會生效,因為這種情況下的調用就屬于這里所說的“本地調用”,沒有調用到代理類,AOP攔截機制不會生效,因此方法B不會被AOP增強,因此事務不會生效。
Ordered
很熟悉的AOP增強概念,當多個攔截器生效的情況下,決定其順序。
int order() default Ordered.LOWEST_PRECEDENCE;
好了,今天就到這里,由于Spring源碼本身比較復雜,一次分析太多、文章太大的話,會比較亂,所以,改變一下思路,一篇文章只說一個概念,控制文章的篇幅,增強可讀性。
以上就是啟用Spring事務管理@EnableTransactionManagement示例解析的詳細內容,更多關于Spring事務管理的資料請關注腳本之家其它相關文章!
相關文章
Spring boot中使用ElasticSearch的方法詳解
這篇文章主要給大家介紹了關于Spring boot中使用ElasticSearch的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01Spring Boot 整合mybatis 與 swagger2
之前使用springMVC+spring+mybatis,總是被一些繁瑣的xml配置,還經常出錯,下面把以前的一些ssm項目改成了spring boot + mybatis,相對于來說優(yōu)點太明顯了,具體內容詳情大家通過本文學習吧2017-08-08SpringBoot+Response如何統(tǒng)一返回result結果集
這篇文章主要介紹了SpringBoot+Response如何統(tǒng)一返回result結果集,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Mybatis-Plus中updateById方法不能更新空值問題解決
本文主要介紹了Mybatis-Plus中updateById方法不能更新空值問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08