Spring AOP Exclude Some Classes

混江龙づ霸主 提交于 2019-12-03 05:29:49

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

As per Spring AOP documentation

PCD can be &&'ed, ||'ed, and ! (negated) too.

So I guess this is more of trial and error exercise. I think you can try something like && !@within @within is applicable for types. Or you can try !@target

But then again I think it could be tricky.

Another approach: declare two pointcut definitions and combine them. And example, here on the documentation page. I would try this first. Something like

@Pointcut(executionPC() && nonAnnotatedClassesPC() && nonAnnotatedMethodsPC())

Disclaimer: As I said, this looks more like trial and error. And I don't have a clear working example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!