![](/img/trans.png)
[英]Null pointer exception for request scoped bean using AspectJ-style Spring AOP (@Around advising an @Transactional webservice method)
[英]How can I access methods attributes with Spring AOP (AspectJ-style)?
我需要通過使用注釋作為切入點來接受一些方法及其屬性,但是如何訪問這些方法屬性。 我有以下代碼,可以在方法運行之前成功運行代碼,但我只是不知道如何訪問這些attrbiutes。
package my.package;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyAspect {
@Pointcut(value="execution(public * *(..))")
public void anyPublicMethod() {
}
@Around("anyPublicMethod() && @annotation(myAnnotation )")
public Object myAspect(ProceedingJoinPoint pjp, MyAnnotation myAnnotation)
throws Throwable {
// how can I access method attributes here ?
System.out.println("hello aspect!");
return pjp.proceed();
}
}
您可以從ProceedingJoinPoint
對象獲取它們:
@Around("anyPublicMethod() && @annotation(myAnnotation )")
public Object myAspect(final ProceedingJoinPoint pjp,
final MyAnnotation myAnnotation) throws Throwable{
// retrieve the methods parameter types (static):
final Signature signature = pjp.getStaticPart().getSignature();
if(signature instanceof MethodSignature){
final MethodSignature ms = (MethodSignature) signature;
final Class<?>[] parameterTypes = ms.getParameterTypes();
for(final Class<?> pt : parameterTypes){
System.out.println("Parameter type:" + pt);
}
}
// retrieve the runtime method arguments (dynamic)
for(final Object argument : pjp.getArgs()){
System.out.println("Parameter value:" + argument);
}
return pjp.proceed();
}
ProceedingJoinPoint
具有pjp.getArgs()
,它返回方法的所有參數。
(但這些被稱為參數/參數,而不是屬性)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.