Spring AOP切入“嵌套”注释

Dom*_*ill 2 java spring annotations aspectj spring-aop

我需要定义一个切入点,该切入点触发在带有自定义注释的spring服务的所有方法上的执行。我想定义切入点的注释将在另一个注释上。

@Y
public @interface X {
}
Run Code Online (Sandbox Code Playgroud)

然后该服务将被注释如下

@X
public Service1 {
} 
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下切入点定义,但仅当@Y在服务本身上时才有效,这意味着它看不到注释在@X上

@Around("@within(com.mypackage.Y)")
Run Code Online (Sandbox Code Playgroud)

Bra*_*ndy 6

我在应用程序中有这个确切的需求。我找到了这个答案,但不满意无法完成。

经过更多搜索后,我发现了用于AspectJ / Spring切入点表达式的备忘单。备忘单中的解决方案不能完全如广告中所述那样工作,但是我能够使其满足我的需要。

@Pointcut("within(@(@Annotation *) *)")
public void classAnnotatedWithNestedAnnotationOneLevelDeep() { }
Run Code Online (Sandbox Code Playgroud)

我将此表达式与@within仅用于的表达式结合起来@Annotation以得到我想要的工作。

对于方法执行:

@Pointcut("execution(@(@com.someorg.SomeAnnotation *) * *(..))")
public void methodAnnotatedWithNestedAnnotationOneLevelDeep() { }
Run Code Online (Sandbox Code Playgroud)

我将此表达式与@annotation仅用于的表达式相结合,@Annotation以获得我想为方法工作的东西。