Spring AOP排除了一些类Spring、AOP

由网友(干净也爱笑)分享简介:我正在使用Spring AspectJ记录方法执行统计信息,但是,我希望在不更改切入点表达式的情况下从中排除一些类和方法。为了排除某些方法,我创建了一个用于过滤掉的定制注释。但是,我无法对类执行相同的操作。以下是我的方面定义-@Around("execution(* com.foo.bar.web.controller...

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

为了排除某些方法,我创建了一个用于过滤掉的定制注释。但是,我无法对类执行相同的操作。

Spring 02 AOP

以下是我的方面定义-

@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

阅读全文

相关推荐

最新文章