Spring AOP排除了一些类 [英] Spring AOP Exclude Some Classes

查看:0
本文介绍了Spring AOP排除了一些类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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
}

NoLogging是我用于排除方法的自定义批注。

那么,如何在不更改切入点表达式和不添加新顾问的情况下筛选出某些类?

推荐答案

好的,我找到了解决方案--使用@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
}

解释-

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

这篇关于Spring AOP排除了一些类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆