0

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

@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如果在方法实现中添加它们,则没有注释。什么是正确的切入点?

4

1 回答 1

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:如何获取建议方法的注解

于 2012-09-28T12:52:39.683 回答