Spring AOP排除了一些类

Har*_*rma 14 spring spring-aop spring-aspects

我正在使用Spring AspectJ来记录方法执行统计信息,但是,我希望在不更改切入点表达式的情况下从中排除某些类和方法.

为了排除某些方法,我创建了一个用于过滤掉的自定义注释.但是我无法对课程做同样的事情.

这是我的方面定义 -

@Around("execution(* com.foo.bar.web.controller.*.*(..)) "
            + "&& !@annotation(com.foo.bar.util.NoLogging)")
public Object log(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    // logging logic here
}
Run Code Online (Sandbox Code Playgroud)

NoLogging 是我的自定义注释,用于排除方法.

那么如何在不更改切入点表达式且不添加新顾问程序的情况下过滤掉某些类?

Har*_*rma 21

好的,所以我找到了解决方案 - 使用@targetPCD(切入点指示符)来过滤掉具有特定注释的类.在这种情况下,我已经有了@NoLogging注释,所以我可以使用它.更新的切入点表达式将变为如下 -

@Around("execution(* com.foo.bar.web.controller.*.*(..)) "
            + "&& !@annotation(com.foo.bar.util.NoLogging)" 
            + "&& !@target(com.foo.bar.util.NoLogging)")
public Object log(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    // logging logic here
}
Run Code Online (Sandbox Code Playgroud)

说明 -

execution(* com.foo.bar.web.controller.*.*(..))- c.f.b.w.controller包中所有类的所有方法

"&& !@annotation(com.foo.bar.util.NoLogging)"- 没有@NoLogging注释

"&& !@target(com.foo.bar.util.NoLogging)"- 并且其类也没有@NoLogging注释.

所以现在我只需要为@NoLogging任何我希望从方面中排除其方法的类添加注释.

可以在Spring AOP文档中找到更多PCD - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-pointcuts-designators

  • @harmonious注释不包含任何逻辑。您可以在这里找到它-https://github.com/harshilsharma63/controller-logger/blob/master/src/main/java/io/github/logger/controller/annotation/NoLogging.java (2认同)