1. 程式人生 > >SpringAOP aspectJ ProceedingJoinPoint 獲取當前方法

SpringAOP aspectJ ProceedingJoinPoint 獲取當前方法

切入點 details blog ceo art join 詳解 targe parameter

  

aspectJ切面通過ProceedingJoinPoint想要獲取當前執行的方法:

錯誤方法:

Signature s = pjp.getSignature();
MethodSignature ms = (MethodSignature)s;
Method m = ms.getMethod();

這種方式獲取到的方法是接口的方法而不是具體的實現類的方法,因此是錯誤的。

正確方法:

Signature sig = pjp.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("該註解只能用於方法");
}
msig = (MethodSignature) sig;
Object target = pjp.getTarget();
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());

Spring 之AOP AspectJ切入點語法詳解

  https://blog.csdn.net/zhengchao1991/article/details/53391244

使用AspectJ編程

  https://blog.csdn.net/zl3450341/article/details/7673938

SpringAOP aspectJ ProceedingJoinPoint 獲取當前方法