@AfterReturning如果和@Around一起使用,那麼就需要給@Around的方法設置返回參數,因爲@AfterReturning接收到的值其實是@Around返回的。
如果@Around的方法沒設置返回參數,有2種可能:
1.@AfterReturning接收到的返回值爲null
2.如果目標方法的返回值類型不是封裝類型如Integer,而是基本類型比如int,則執行會報錯:
org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int cn.e_aop_anno.IUserDao.deleteById(int)
如果將目標方法返回類型改成封裝類型如Integer,則執行不報錯,但是接收到的返回值爲null;即可能1寫的結果。
// 指定切入點表達式:攔截哪些方法,即爲哪些類生成代理對象
@Pointcut("execution(* cn.e_aop_anno.*.*(..))")
public void pointCut_() {}
// 返回後通知:在調用目標方法結束後執行【出現異常,不執行】
@AfterReturning(value="pointCut_()",returning="retValue")
public void afterReturning(JoinPoint jp, Object retValue) {
System.out.println("Aop.afterReturning() 目標方法+"+jp.getSignature().getName()+"返回值:" + retValue);
}
@Around(value="pointCut_()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("環繞開始...");
Object retValue = pjp.proceed();// 執行目標方法
System.out.println("環繞結束...");
return retValue;
}
/*
*此時要麼執行報錯類型不匹配,要麼@AfterReturning的返回值retValue是null
@Around(value="pointCut_()")
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("環繞開始...");
pjp.proceed();// 執行目標方法
System.out.println("環繞結束...");
}*/