例如,我有以下方法:

public void method1(@MyAnnotation Object a, Object b..) {
   ...
}

public void method1(Object a, Object b..., @MyAnnotation Object n, ...) {
   ...
}


什么是AspectJ切入点,该切入点仅针对具有用@MyAnnotation注释的参数的方法?

该注释可以应用于方法的ANY参数。

最佳答案

以下切入点应与您问题中方法的执行相匹配。我还提供了第二个切入点,这只是对第一个切入点的微小修改,但是如果您需要匹配带注释的特定类型,则可能会发现它很有用。

public aspect AnnotatedMethodParameterMatchingAspect  {

    /**
     * Matches the execution of any method having a parameter annotated with the
     * {@link MyAnnotation} annotation.
     */
    pointcut executionOfMethodWithAnnotatedParameter():
        execution(* *(.., @MyAnnotation (*), ..));

    /**
     * Matches the execution of any method having a parameter annotated with the
     * {@link MyAnnotation} annotation where the parameter type is a {@link MyType}
     * (or a subtype).
     */
    pointcut executionOfMethodWithAnnotatedTypeRestrictedParameter():
        execution(* *(.., @MyAnnotation (MyType+), ..));

}

关于java - 使用带有特定批注的参数的方法的AspectJ Pointcut,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34291666/

10-10 03:16