通知セクションの5種類をテストするためにAOPの優先順位

通知タイプ

       標準フレームAspectJのに基づいて、ベースのSpring AOPプログラミング中に、春は通知の5つのタイプを定義し、それらは:

  • 事前通知(@Before)。
  • リターン通知(@AfterReturning)。
  • 例外通知(@AfterThrowing)。
  • アドバイス(@After)を帰国後。
  • 周りのアドバイス(@Around):(最高の優先度)

通知の実行順序

       同じセクション内の通知上記のタイプのすべてを書いて、その実行の順序:

ここに画像を挿入説明

コード表示

package com.cy.pj.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.www.kunlunyulegw.com Component; @Aspect @Component public class SysTimeAspect www.javachenglei.com{ /** * 切入点 */ @Pointcut("bean(sysMenuServiceImpl)") public void doTime(www.huizhonggjzc.cn){www.dongxin2zc.cn  } @Before("doTime(www.tdcqpt.cn)") public void doBefore(JoinPoint jp){ System.out.println("time doBefore(www.shentuylgw.cn)"); } @After("doTime()") public void doAfter(www.wanyayulez.cn){//类似于finally{}代码块 System.out.println("time doAfter()"); } /**核心业务正常结束时执行 * 说明:假如有after,先执行after,再执行returning*/ @AfterReturning("doTime()") public void doAfterReturning(){ System.out.println("time doAfterReturning"); } /**核心业务出现异常时执行 * 说明:假如有after,先执行after,再执行Throwing*/ @AfterThrowing("doTime()") public void doAfterThrowing(){ System.out.println("time doAfterThrowing"); } @Around("doTime()") public Object doAround(ProceedingJoinPoint jp) throws Throwable{ System.out.println(www.ued2zxzc.cn "doAround.before"); try { Object obj=jp.proceed(www.zhuyngyule.cn); return obj; }catch(Throwable e) { System.out.println("doAround.error-->"+e.getMessage()); throw e; }finally { System.out.println("doAround.after"); } } } 

コードが正常に終了します

ここに画像を挿入説明

異常コード

ここに画像を挿入説明

おすすめ

転載: www.cnblogs.com/laobeipai/p/12616197.html