带注释的类中带注释的方法或方法的切入点 [英] Pointcut for annotated methods or methods in annotated classes

查看:165
本文介绍了带注释的类中带注释的方法或方法的切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用@X注释的类中的方法或使用@X 注释的方法的切入点。我还需要注释对象。如果类和方法都注释了我更喜欢将方法注释作为参数

I need a pointcut for methods in classes annotated with @X or methods annotated with @X. I also need the annotation object. If both the class and the method are annotated I prefer to get the method annotation as argument.

我尝试了以下操作,创建了一个不一致的约束警告。 (为什么不将它们设置为null?)

I tried the following, which creates an "inconsistent binding" warning. (Why not just set them null?)

@Around("@annotation(methodLevelX) || @within(classLevelX)")
public Object advise(ProceedingJoinPoint pjp, X methodLevelX, X classLevelX)

以下创建切入点警告中参数(x)跨越'||'的模糊绑定。 (在我看来,这不一定有意义:为什么不绑定第一个短路评估?)

The following creates a "ambiguous binding of parameter(s) x across '||' in pointcut" warning. (Which does not necessarily make sense in my opinion: Why not bind the first short circuited evaluation?)

@Around("@annotation(x) || @within(x)")
public Object advise(ProceedingJoinPoint pjp, X x)

如果存在类和方法注释,则将前一次尝试拆分为两个自然会导致两次方法调用。

Splitting the previous attempt in two naturally results in two method calls if class and method annotations are present.

我知道我可以通过这样的切入点获得带有反射和我想要的注释的方法和类:

I know I could just get the method and class with reflection and my desired annotation with a pointcut like this:

@Around("@annotation(com.package.X) || @within(com.package.X)")

但我不愿意。

有没有一个切入点,一个方法,一个注释参数,我的要求的解决方案,不需要反思?

推荐答案

不完全,但是几乎。您将需要两个切入点,两个建议,但您可以将工作委托给单个方法。以下是它的样子:

Not quite, but almost. You will need two pointcuts, two advices, but you can delegate the work to a single method. Here's how it would look like:

@Aspect
public class AnyAspectName {

    @Pointcut("execution(@X * *.*(..))")
    void annotatedMethod() {}

    @Pointcut("execution(* (@X *).*(..))")
    void methodOfAnnotatedClass() {}

    @Around("annotatedMethod() && @annotation(methodLevelX)")
    public Object adviseAnnotatedMethods(ProceedingJoinPoint pjp, X methodLevelX) 
            throws Throwable {
        return aroundImplementation(pjp, methodLevelX);
    }

    @Around("methodOfAnnotatedClass() && !annotatedMethod() && @within(classLevelX)")
    public Object adviseMethodsOfAnnotatedClass(ProceedingJoinPoint pjp, X classLevelX) 
            throws Throwable {
        return aroundImplementation(pjp, classLevelX);
    }

    public Object aroundImplementation(ProceedingJoinPoint pjp, X annotation) 
            throws Throwable {
        return pjp.proceed();
    }

}

请注意,除了分开 @annotation() @within()切入点,我对生成的切入点添加了限制,这样它们就不会宽。我想你想要方法执行连接点,所以我添加了所需的切入点表达式,将其限制为方法执行。它们匹配

Note that besides splitting apart the @annotation() and @within() pointcuts, I added restrictions to the resulting pointcuts so that they aren't too broad. I suppose you want method execution join points, so I added the needed pointcut expressions that would restrict it to method execution. They are matching


  1. 执行任何使用 @X 注释的方法第一个建议的任何类中的任何类的任何返回类型

  2. 在任何使用 @X 第二个。

  1. execution of any method annotated with @X with any return type in any class being in any package for the first advice
  2. execution of any method with any return type in any class annotated with @X for the second.

进一步限制 @在(X) @annotation(X)之内派上用场,因为 @within(X)本身匹配

Further restricting @within(X) and @annotation(X) comes in handy, because @within(X) by itself would match


任何连接点,其中关联代码在类型中定义,
类型注释 X

包括方法执行方法调用构造函数执行构造函数调用预初始化静态初始化初始化字段集字段获取例外处理程序键入连接点(不是全部加入点对周围的建议有效。同样, @annotation(X)本身就意味着

which would include method-execution, method-call, constructor-execution, constructor-call, pre-initialization, static initialization, initialization, field set, field get, exception-handler, lock type join points (not all join points are valid for around advices though). Similarly, @annotation(X) by itself would mean


任何加入点主题的注释类型为 X

这也可能意味着大部分前面提到的连接点,取决于注释的目标类型。

which could also mean most of the previously mentioned join points, depending on the target type of your annotation.

这篇关于带注释的类中带注释的方法或方法的切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆