切入方法实现

时间:2012-09-28 12:24:38

标签: java spring aspectj spring-aop

我使用带注释方法的切入点创建了一个Aspect。我得到的JoinPoint是来自Interface而不是方法实现的方法声明。因此,当我想获得带注释的方法参数时,我必须在接口中添加注释,而不是在实现中。

@AfterReturning(value = "@annotation(pl.styall.scylla.security.authorization.acl.AclSecure) && @annotation(aclSecure)", argNames = "jp,aclSecure,retVal", returning = "retVal")
public void addObjectAndEntry(JoinPoint jp, AclSecure aclSecure,
        Object retVal) {
    System.out.println(jp.toLongString());
    MethodSignature signature = (MethodSignature) jp.getSignature();
    Method method = signature.getMethod();
    Annotation[][] methodAnnotations = method.getParameterAnnotations();

methodAnnotations如果添加方法实现则没有注释。什么是正确的切入点?

1 个答案:

答案 0 :(得分:2)

final String methodName = pjp.getSignature().getName();
final MethodSignature methodSignature = (MethodSignature)pjp.getSignature();
Method method = methodSignature.getMethod();
if (method.getDeclaringClass().isInterface()) {
    method = pjp.getTarget().getClass().getDeclaredMethod(methodName, method.getParameterTypes());    
}

来源:Spring AOP: how to get the annotations of the adviced method