AspectJ的註解開發AOP:切點定義

經過@Pointcut爲切點命名,方便咱們統一管理java

在每一個通知內定義切點,會形成工做量大,不易維護,對於重複的切點,能夠使用@Pointcut進行定義spa

切點方法:private void無參方法,方法名爲切點名code

當通知多個切點時,能夠使用||來進行鏈接blog

具體代碼:element

import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import java.math.BigInteger; @Aspect public class aspectj { @Before(value = "execution(* com.AspecJ.xiaomaoDao.*(..))") public void before(){ System.out.println("我是前置通知!"); } @AfterReturning(value = "Mypoint2()",returning = "element") // 使用returning來接受返回值
    public void After(Object element){ System.out.println("我刪除了"+element); //打印輸出了返回值
 } @Around(value="MyPoint1()") public Object around(ProceedingJoinPoint joinPoint){ Object obj=null; System.out.println("環繞前"); try { obj=joinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("環繞後"); return obj; } @AfterThrowing(value = "MyPoint()",throwing = "throwinfo") public void throwafter(Throwable throwinfo){ System.out.println("發生了異常,異常信息以下:"); System.out.println(throwinfo.getMessage()); } @After(value = "MyPoint()") public void after(){ System.out.println("最終通知執行了!"); } @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.find())") public void MyPoint(){} @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.update())") public void MyPoint1(){} @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.delete())") public void Mypoint2(){} }
相關文章
相關標籤/搜索