免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

怎么理解SpringAOP執(zhí)行先后順序?qū)嵗?創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)怎么理解SpringAOP執(zhí)行先后順序?qū)嵗恼聝?nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)福安免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

眾所周知,spring聲明式事務(wù)是基于AOP實(shí)現(xiàn)的,那么,如果我們?cè)谕粋€(gè)方法自定義多個(gè)AOP,我們?nèi)绾沃付ㄋ麄兊膱?zhí)行順序呢?

網(wǎng)上很多答案都是指定order,order越小越是最先執(zhí)行,這種也不能算是錯(cuò),但有些片面。

配置AOP執(zhí)行順序的三種方式:

通過實(shí)現(xiàn)org.springframework.core.Ordered接口

@Component @Aspect @Slf4j public class MessageQueueAopAspect1 implements Ordered{@Override   public int getOrder() {     // TODO Auto-generated method stub     return 2;   }    }

通過注解

@Component @Aspect @Slf4j @Order(1) public class MessageQueueAopAspect1{      ... }

通過配置文件配置

<aop:config expose-proxy="true">   <aop:aspect ref="aopBean" order="0">      <aop:pointcut id="testPointcut" expression="@annotation(xxx.xxx.xxx.annotation.xxx)"/>      <aop:around pointcut-ref="testPointcut" method="doAround" />      </aop:aspect>  </aop:config>

我們?cè)谕粋€(gè)方法上加以下兩個(gè)AOP,看看究竟。

@Component @Aspect @Slf4j public class MessageQueueAopAspect1 implements Ordered{      @Resource(name="actionMessageProducer")   private IProducer<MessageQueueInfo> actionProducer;        @Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire1)")   private void pointCutMethod() {   }      //聲明前置通知   @Before("pointCutMethod()")   public void doBefore(JoinPoint point) {     log.info("MessageQueueAopAspect1:doBefore");     return;   }    //聲明后置通知   @AfterReturning(pointcut = "pointCutMethod()", returning = "returnValue")   public void doAfterReturning(JoinPoint point,Object returnValue) {     log.info("MessageQueueAopAspect1:doAfterReturning");   }    //聲明例外通知   @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")   public void doAfterThrowing(Exception e) {     log.info("MessageQueueAopAspect1:doAfterThrowing");   }    //聲明最終通知   @After("pointCutMethod()")   public void doAfter() {     log.info("MessageQueueAopAspect1:doAfter");   }    //聲明環(huán)繞通知   @Around("pointCutMethod()")   public Object doAround(ProceedingJoinPoint pjp) throws Throwable {     log.info("MessageQueueAopAspect1:doAround-1");     Object obj = pjp.proceed();     log.info("MessageQueueAopAspect1:doAround-2");     return obj;   }      @Override   public int getOrder() {     return 1001;   } }

@Component @Aspect @Slf4j public class MessageQueueAopAspect2 implements Ordered{      @Resource(name="actionMessageProducer")   private IProducer<MessageQueueInfo> actionProducer;        @Pointcut("@annotation(com.xxx.annotation.MessageQueueRequire2)")   private void pointCutMethod() {   }         //聲明前置通知   @Before("pointCutMethod()")   public void doBefore(JoinPoint point) {     log.info("MessageQueueAopAspect2:doBefore");     return;   }    //聲明后置通知   @AfterReturning(pointcut = "pointCutMethod()", returning = "returnValue")   public void doAfterReturning(JoinPoint point,Object returnValue) {     log.info("MessageQueueAopAspect2:doAfterReturning");   }    //聲明例外通知   @AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")   public void doAfterThrowing(Exception e) {     log.info("MessageQueueAopAspect2:doAfterThrowing");   }    //聲明最終通知   @After("pointCutMethod()")   public void doAfter() {     log.info("MessageQueueAopAspect2:doAfter");   }    //聲明環(huán)繞通知   @Around("pointCutMethod()")   public Object doAround(ProceedingJoinPoint pjp) throws Throwable {     log.info("MessageQueueAopAspect2:doAround-1");     Object obj = pjp.proceed();     log.info("MessageQueueAopAspect2:doAround-2");     return obj;   }      @Override   public int getOrder() {     return 1002;   } }

@Transactional(propagation=Propagation.REQUIRES_NEW) @MessageQueueRequire1 @MessageQueueRequire2 public PnrPaymentErrCode bidLoan(String id){        ...     }

看看執(zhí)行結(jié)果:

從上面的測試我們看到,確實(shí)是order越小越是最先執(zhí)行,但更重要的是最先執(zhí)行的最后結(jié)束。

這個(gè)不難理解,Spring AOP就是面向切面編程,什么是切面,畫一個(gè)圖來理解下:

由此得出:spring aop就是一個(gè)同心圓,要執(zhí)行的方法為圓心,最外層的order最小。從最外層按照AOP1、AOP2的順序依次執(zhí)行doAround方法,doBefore方法。然后執(zhí)行method方法,最后按照AOP2、AOP1的順序依次執(zhí)行doAfter、doAfterReturn方法。也就是說對(duì)多個(gè)AOP來說,先before的,一定后after。

如果我們要在同一個(gè)方法事務(wù)提交后執(zhí)行自己的AOP,那么把事務(wù)的AOP order設(shè)置為2,自己的AOP order設(shè)置為1,然后在doAfterReturn里邊處理自己的業(yè)務(wù)邏輯。

關(guān)于怎么理解SpringAOP執(zhí)行先后順序?qū)嵗头窒淼竭@里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

網(wǎng)站題目:怎么理解SpringAOP執(zhí)行先后順序?qū)嵗?創(chuàng)新互聯(lián)
URL分享:http://newbst.com/article6/diphog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)小程序開發(fā)自適應(yīng)網(wǎng)站ChatGPT移動(dòng)網(wǎng)站建設(shè)外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站