返回通知
返回通知
無論連接點是正常返回還是拋出異常, 后置通知都會執(zhí)行. 如果只想在連接點返回的時候記錄日志, 應使用返回通知代替后置通知.
在返回通知中訪問連接點的返回值?
在返回通知中, 只要將 returning 屬性添加到 @AfterReturning 注解中, 就可以訪問連接點的返回值. 該屬性的值即為用來傳入返回值的參數(shù)名稱.?
必須在通知方法的簽名中添加一個同名參數(shù). 在運行時, Spring AOP 會通過這個參數(shù)傳遞返回值.
原始的切點表達式需要出現(xiàn)在 pointcut 屬性中
package com.learn.spring.aspectJ;import java.util.Arrays;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.core.annotation.Order; import org.springframework.stereotype.Component;/*** 日志切面*/ @Component //標識為組件 @Aspect //標識為切面 @Order(3) //指定切面的優(yōu)先級. 值越小,優(yōu)先級越高. 標注@Order的切面比不標注@Order切面的優(yōu)先級高 public class LoggingAspect {/*** 返回通知: 在方法正常執(zhí)行,返回結果以后執(zhí)行的.* 返回通知就能訪問到方法的返回值.*///@AfterReturning(value="execution(* com.learn.spring.aspectJ.*.*(..))",returning="result")public void afterReturningMethod(JoinPoint joinPoint,Object result){//獲取方法名:String methodName = joinPoint.getSignature().getName();System.out.println("The method "+methodName+" ends with " + result );} }?
總結