package com.bjpowernode.ba02; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import java.util.Date; @Aspect public class MyAspect { /** * 后置通知定義方法,方法是實現切面功能的 * 方法的定義要求 * 1. 公共方法public * 2. 方法沒有返回值 * 3. 方法名稱自定義 * 4. 方法有參數,推薦是object 參數名自定義 * */ /** * @AfterReturning 后置通知 * 屬性1. value切入點位置 * 2. returning 自定義的變量,標識目標方法的返回值 * 自定義變量名必須和通知方法的形參一樣 * 在方法定義的上面 * 特點:在目標方法之后執行的 * 能夠獲取到目標方法的返回值,可以根據這個返回值做不同的處理功能 */ @AfterReturning(value = "execution(* doOther(..))",returning = "res") public void doaspect(JoinPoint jp,String res){ if(res.equals("abcd")){ System.out.println("獲得的返回值是"+res+"符合要求"); System.out.println(jp.getSignature().getName()); } else{ System.out.println("獲得的返回值不等於abcd,不符合要求"); } } }