问题描述
我需要使用@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
- 执行任何使用
@X
注释的方法第一个建议的任何类中的任何类的任何返回类型 - 在任何使用 @X 第二个。
- execution of any method annotated with
@X
with any return type in any class being in any package for the first advice - 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
包括方法执行 ,方法调用,构造函数执行,构造函数调用,预初始化,静态初始化,初始化,字段集,字段获取,例外处理程序,锁键入连接点(不是全部加入点对周围的建议有效。同样, @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
这也可能意味着大部分前面提到的连接点,取决于注释的目标类型。
which could also mean most of the previously mentioned join points, depending on the target type of your annotation.
这篇关于带注释的类中带注释的方法或方法的切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!