Spring AOP Exclude Some Classes

后端 未结 2 564
日久生厌
日久生厌 2021-02-05 11:15

I\'m using Spring AspectJ for logging method execution statistics, however, I want to exclude some classes and methods from this without changing the pointcut expression.

<
2条回答
  •  感情败类
    2021-02-05 11:26

    Okay so I found the solution - use @target PCD (pointcut designators) to filter out classes with specific annotation. In this case I already have the @NoLogging annotation so I can use that. The updated pointcut expression will then become as follows -

    @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
    }
    

    Explanation -

    execution(* com.foo.bar.web.controller.*.*(..)) - all methods of all classes in c.f.b.w.controller package

    "&& !@annotation(com.foo.bar.util.NoLogging)" - which do NOT have @NoLogging annotation on them

    "&& !@target(com.foo.bar.util.NoLogging)" - and whose class also does NOT have @NoLogging annotation.

    So now I simply have to add @NoLogging annotation to any class whose methods I want to be excluded from the aspect.

    More PCD can be found in Spring AOP documentation - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-pointcuts-designators

提交回复
热议问题