最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解

 更新時(shí)間:2021年09月28日 11:56:06   作者:一擼向北  
這篇文章主要介紹了spring源碼閱讀--@Transactional實(shí)現(xiàn)原理講解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@Transactional注解簡介

@Transactional是spring中聲明式事務(wù)管理的注解配置方式,相信這個(gè)注解的作用大家都很清楚。

@Transactional注解可以幫助我們把事務(wù)開啟、提交或者回滾的操作,通過aop的方式進(jìn)行管理。

通過@Transactional注解就能讓spring為我們管理事務(wù),免去了重復(fù)的事務(wù)管理邏輯,減少對業(yè)務(wù)代碼的侵入,使我們開發(fā)人員能夠?qū)W⒂跇I(yè)務(wù)層面開發(fā)。

在這里插入圖片描述

我們知道實(shí)現(xiàn)@Transactional原理是基于spring aop,aop又是動(dòng)態(tài)代理模式的實(shí)現(xiàn),通過對源碼的閱讀,總結(jié)出下面的步驟來了解實(shí)際中,在spring 是如何利用aop來實(shí)現(xiàn)@Transactional的功能的。如果對spring的aop實(shí)現(xiàn)原理不了解,可以看aop實(shí)現(xiàn)原理分析。

spring中聲明式事務(wù)實(shí)現(xiàn)原理猜想

  • 首先,對于spring中aop實(shí)現(xiàn)原理有了解的話,應(yīng)該知道想要對一個(gè)方法進(jìn)行代理的話,肯定需要定義切點(diǎn)。在@Transactional的實(shí)現(xiàn)中,同樣如此,spring為我們定義了以 @Transactional 注解為植入點(diǎn)的切點(diǎn),這樣才能知道@Transactional注解標(biāo)注的方法需要被代理。
  • 有了切面定義之后,在spring的bean的初始化過程中,就需要對實(shí)例化的bean進(jìn)行代理,并且生成代理對象。
  • 生成代理對象的代理邏輯中,進(jìn)行方法調(diào)用時(shí),需要先獲取切面邏輯,@Transactional注解的切面邏輯類似于@Around,在spring中是實(shí)現(xiàn)一種類似代理邏輯。

在這里插入圖片描述

@Transactional作用

根據(jù)上面的原理猜想,下面簡單介紹每個(gè)步驟的源碼以進(jìn)行驗(yàn)證。

首先是@Transactional,作用是定義代理植入點(diǎn)?!綼op實(shí)現(xiàn)原理分析】中,分析知道代理對象創(chuàng)建的通過BeanPostProcessor的實(shí)現(xiàn)類AnnotationAwareAspectJAutoProxyCreator的postProcessAfterInstantiation方法來實(shí)現(xiàn)個(gè),如果需要進(jìn)行代理,那么在這個(gè)方法就會(huì)返回一個(gè)代理對象給容器,同時(shí)判斷植入點(diǎn)也是在這個(gè)方法中。

那么下面開始分析,在配置好注解驅(qū)動(dòng)方式的事務(wù)管理之后,spring會(huì)在ioc容器創(chuàng)建一個(gè)BeanFactoryTransactionAttributeSourceAdvisor實(shí)例,這個(gè)實(shí)例可以看作是一個(gè)切點(diǎn),在判斷一個(gè)bean在初始化過程中是否需要?jiǎng)?chuàng)建代理對象,都需要驗(yàn)證一次BeanFactoryTransactionAttributeSourceAdvisor是否是適用這個(gè)bean的切點(diǎn)。如果是,就需要?jiǎng)?chuàng)建代理對象,并且把BeanFactoryTransactionAttributeSourceAdvisor實(shí)例注入到代理對象中。

其中【aop實(shí)現(xiàn)原理分析】知道在AopUtils#findAdvisorsThatCanApply中判斷切面是否適用當(dāng)前bean,可以在這個(gè)地方斷點(diǎn)分析調(diào)用堆棧,AopUtils#findAdvisorsThatCanApply一致調(diào)用,最終通過以下代碼判斷是否適用切點(diǎn)。

AbstractFallbackTransactionAttributeSource#computeTransactionAttribute(Method method, Class<?> targetClass) 

這里可以根據(jù)參數(shù)打上條件斷點(diǎn)進(jìn)行調(diào)試分析調(diào)用棧,targetClass就是目標(biāo)class

…一系列調(diào)用 最終

SpringTransactionAnnotationParser#parseTransactionAnnotation(java.lang.reflect.AnnotatedElement)
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
    //這里就是分析Method是否被@Transactional注解標(biāo)注,有的話,不用說BeanFactoryTransactionAttributeSourceAdvisor適配當(dāng)前bean,進(jìn)行代理,并且注入切點(diǎn)
    //BeanFactoryTransactionAttributeSourceAdvisor
   AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
   if (attributes != null) {
      return parseTransactionAnnotation(attributes);
   }
   else {
      return null;
   }
}

上面就是判斷是否需要根據(jù)@Transactional進(jìn)行代理對象創(chuàng)建的判斷過程。@Transactional的作用一個(gè)就是標(biāo)識方法需要被代理,一個(gè)就是攜帶事務(wù)管理需要的一些屬性信息。

動(dòng)態(tài)代理邏輯實(shí)現(xiàn)

【aop實(shí)現(xiàn)原理分析】中知道,aop最終的代理對象的代理方法是

DynamicAdvisedInterceptor#intercept 

所以我們可以在這個(gè)方法斷點(diǎn)分析代理邏輯。

@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
   Object oldProxy = null;
   boolean setProxyContext = false;
   Class<?> targetClass = null;
   Object target = null;
   try {
      if (this.advised.exposeProxy) {
         // Make invocation available if necessary.
         oldProxy = AopContext.setCurrentProxy(proxy);
         setProxyContext = true;
      }
      // May be null. Get as late as possible to minimize the time we
      // "own" the target, in case it comes from a pool...
      target = getTarget();
      if (target != null) {
         targetClass = target.getClass();
      }
       //follow
      List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
      Object retVal;
      // Check whether we only have one InvokerInterceptor: that is,
      // no real advice, but just reflective invocation of the target.
      if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
         // We can skip creating a MethodInvocation: just invoke the target directly.
         // Note that the final invoker must be an InvokerInterceptor, so we know
         // it does nothing but a reflective operation on the target, and no hot
         // swapping or fancy proxying.
         Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
         retVal = methodProxy.invoke(target, argsToUse);
      }
      else {
         // We need to create a method invocation...
         retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
      }
      retVal = processReturnType(proxy, target, method, retVal);
      return retVal;
   }
   finally {
      if (target != null) {
         releaseTarget(target);
      }
      if (setProxyContext) {
         // Restore old proxy.
         AopContext.setCurrentProxy(oldProxy);
      }
   }
}

通過分析

List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass)

返回的是TransactionInterceptor,利用TransactionInterceptor是如何實(shí)現(xiàn)代理邏輯調(diào)用的?跟蹤

new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();

發(fā)現(xiàn)最終是調(diào)用TransactionInterceptor#invoke方法,并且把CglibMethodInvocation注入到invoke方法中,從上面可以看到CglibMethodInvocation是包裝了目標(biāo)對象的方法調(diào)用的所有必須信息,因此,在TransactionInterceptor#invoke里面也是可以調(diào)用目標(biāo)方法的,并且還可以實(shí)現(xiàn)類似@Around的邏輯,在目標(biāo)方法調(diào)用前后繼續(xù)注入一些其他邏輯,比如事務(wù)管理邏輯。

TransactionInterceptor–最終事務(wù)管理者

下面看代碼。

TransactionInterceptor#invoke 
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
	// Work out the target class: may be {@code null}.
	// The TransactionAttributeSource should be passed the target class
	// as well as the method, which may be from an interface.
	Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
	// Adapt to TransactionAspectSupport's invokeWithinTransaction...
	return invokeWithinTransaction(invocation.getMethod(), targetClass, new InvocationCallback() {
		@Override
		public Object proceedWithInvocation() throws Throwable {
			return invocation.proceed();
		}
	});
}

繼續(xù)跟蹤invokeWithinTransaction,下面的代碼中其實(shí)就可以看出一些邏輯端倪,就是我們猜想的實(shí)現(xiàn)方式,事務(wù)管理。

protected Object invokeWithinTransaction(Method method, Class<?> targetClass, final InvocationCallback invocation)
      throws Throwable {
   // If the transaction attribute is null, the method is non-transactional.
   final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
   final PlatformTransactionManager tm = determineTransactionManager(txAttr);
   final String joinpointIdentification = methodIdentification(method, targetClass);
   if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
      // Standard transaction demarcation with getTransaction and commit/rollback calls.
       //開啟事務(wù)
      TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
      Object retVal = null;
      try {
         // This is an around advice: Invoke the next interceptor in the chain.
         // This will normally result in a target object being invoked.
          //方法調(diào)用
         retVal = invocation.proceedWithInvocation();
      }
      catch (Throwable ex) {
         // target invocation exception
   		//回滾事務(wù)
         completeTransactionAfterThrowing(txInfo, ex);
         throw ex;
      }
      finally {
         cleanupTransactionInfo(txInfo);
      }
       //提交事務(wù)
      commitTransactionAfterReturning(txInfo);
      return retVal;
   }
   else {
      // It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
      try {
         Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr,
               new TransactionCallback<Object>() {
                  @Override
                  public Object doInTransaction(TransactionStatus status) {
                     TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
                     try {
                        return invocation.proceedWithInvocation();
                     }
                     catch (Throwable ex) {
                        if (txAttr.rollbackOn(ex)) {
                           // A RuntimeException: will lead to a rollback.
                           if (ex instanceof RuntimeException) {
                              throw (RuntimeException) ex;
                           }
                           else {
                              throw new ThrowableHolderException(ex);
                           }
                        }
                        else {
                           // A normal return value: will lead to a commit.
                           return new ThrowableHolder(ex);
                        }
                     }
                     finally {
                        cleanupTransactionInfo(txInfo);
                     }
                  }
               });
         // Check result: It might indicate a Throwable to rethrow.
         if (result instanceof ThrowableHolder) {
            throw ((ThrowableHolder) result).getThrowable();
         }
         else {
            return result;
         }
      }
      catch (ThrowableHolderException ex) {
         throw ex.getCause();
      }
   }
}

總結(jié)

最終可以總結(jié)一下整個(gè)流程,跟開始的猜想對照。

在這里插入圖片描述

分析源碼后對照

在這里插入圖片描述

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • EasyExcel實(shí)現(xiàn)導(dǎo)入+各種數(shù)據(jù)校驗(yàn)功能

    EasyExcel實(shí)現(xiàn)導(dǎo)入+各種數(shù)據(jù)校驗(yàn)功能

    這篇文章主要介紹了EasyExcel實(shí)現(xiàn)導(dǎo)入+各種數(shù)據(jù)校驗(yàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Java利用jenkins做項(xiàng)目的自動(dòng)化部署

    Java利用jenkins做項(xiàng)目的自動(dòng)化部署

    這篇文章主要介紹了Java利用jenkins做項(xiàng)目的自動(dòng)化部署,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Spring運(yùn)行環(huán)境Environment的解析

    Spring運(yùn)行環(huán)境Environment的解析

    本文主要介紹了Spring運(yùn)行環(huán)境Environment的解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Java中BorderLayout布局管理器的兩種排列方式

    Java中BorderLayout布局管理器的兩種排列方式

    這篇文章主要介紹了Java中BorderLayout布局管理器的兩種排列方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解詳析

    Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解詳析

    編譯時(shí)注解相信對每一個(gè)java開發(fā)者來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧
    2018-05-05
  • 因BigDecimal類型數(shù)據(jù)引出的問題詳析

    因BigDecimal類型數(shù)據(jù)引出的問題詳析

    Java在java.math包中提供的API類BigDecimal,用來對超過16位有效位的數(shù)進(jìn)行精確的運(yùn)算,下面這篇文章主要給大家介紹了因BigDecimal類型數(shù)據(jù)引出的問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-08-08
  • Java并發(fā)工具類LongAdder原理實(shí)例解析

    Java并發(fā)工具類LongAdder原理實(shí)例解析

    這篇文章主要介紹了Java并發(fā)工具類LongAdder原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Java多線程ForkJoinPool實(shí)例詳解

    Java多線程ForkJoinPool實(shí)例詳解

    這篇文章主要介紹了Java多線程ForkJoinPool實(shí)例詳解,涉及forkjoin框架的相關(guān)內(nèi)容,需要的朋友可以參考下。
    2017-09-09
  • JDK8并行流及串行流區(qū)別原理詳解

    JDK8并行流及串行流區(qū)別原理詳解

    這篇文章主要介紹了JDK8并行流及串行流區(qū)別原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • mybatis中如何傳遞單個(gè)String類型的參數(shù)

    mybatis中如何傳遞單個(gè)String類型的參數(shù)

    這篇文章主要介紹了mybatis中如何傳遞單個(gè)String類型的參數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論

育儿| 浙江省| 河东区| 桓仁| 宁乡县| 宣汉县| 平罗县| 汪清县| 敖汉旗| 巴彦淖尔市| 资溪县| 东安县| 南澳县| 林州市| 台中县| 新巴尔虎右旗| 连州市| 尖扎县| 册亨县| 锡林郭勒盟| 芦溪县| 邢台县| 沙洋县| 库尔勒市| 鹿邑县| 灵璧县| 潼南县| 磐安县| 湘潭市| 岑巩县| 高邮市| 武定县| 思南县| 辛集市| 牙克石市| 邹平县| 灵武市| 临泽县| 忻州市| 扎鲁特旗| 凉城县|