Spring AOP:从切入点中排除最终类和枚举 [英] Spring AOP: exclude avoid final classes and enums from pointcut

查看:485
本文介绍了Spring AOP:从切入点中排除最终类和枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring AOP实现日志记录.我已经定义了

I am using try to implement Logging using Spring AOP. I have defined the

@Pointcut("execution(* com.mycom..*(..))")
private void framework() {}

@Around("framework()")
public Object aroundAdviceFramework(ProceedingJoinPoint jp) throws Throwable {
    if (logger.isDebugEnabled())
        logger.debug("DEBUG:: {}  {}  Enter", jp.getTarget().getClass().getName(), jp.getSignature().getName());
    Object returnVal = jp.proceed(jp.getArgs());
    if (logger.isDebugEnabled())
        logger.debug("DEBUG:: {}  {}  Out", jp.getTarget().getClass().getName(), jp.getSignature().getName());
    logger.info("INFO:: " + jp.getTarget().getClass().getName() + " " + jp.getSignature().getName() + " Finished:");
    return returnVal;
}

mycom软件包及其子软件包下有许多类.一些类是枚举和最终类. 因此,我得到

There are lot of classes under mycom package and its subpackages. Some of the classes are enum and final class. Because of this I am getting

nested exception is org.springframework.aop.framework.AopConfigException: 
Could not generate CGLIB subclass of class [class com.mycom.util.BancsServiceProvider]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.mycom.util.BancsServiceProvider
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)

有没有一种方法可以使用某种正则表达式从日志记录中排除所有最终类和枚举类. 注意:我在不同的包中都有枚举类.使用完整的类名来排除它们是很困难的.

Is there a way to exclude all the final classes and enum classes from logging using some kind of regular expression. Note:I have enum classes all over in different packages. It would be difficult to exclude them using complete class names.

更新2014-11-17(尝试使用kriegaex的解决方案):

我尝试使用

@Pointcut("!within(is(FinalType))")

但我收到以下错误消息

Pointcut is not well-formed: expecting ')' at character position 10

!within(is(FinalType))

!within(is(FinalType))

我已经在pom文件中添加了这个maven依赖项

I have added this maven dependency in the pom file

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.4</version>
    </dependency>

我还添加了这个Maven依赖项

I have also added this maven dependency

   <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.4</version>
    </dependency>

现在,一切都像魅力一样运作.有什么想法,这是怎么回事?

Now, everything is working like charm. Any ideas, whats happening here?

推荐答案

当前,您可以通过AspectJ 1.6.9中引入的is()切入点语法来排除枚举,方面,接口,内部类型,匿名类型.在此处.

Currently you can exclude enums, aspects, interfaces, inner types, anonymous types via is() pointcut syntax which was introduced in AspectJ 1.6.9, see also my answer here.

您目前不能做的是通过AspectJ语法排除最终类型.但是我认为这是有道理的,因此我为此创建了一个门票它.

What you cannot do at the moment is exclude final types via AspectJ syntax. But I think it would make sense, so I created a ticket for it.

如何排除枚举:

@Pointcut("execution(* com.mycom..*(..)) && !within(is(EnumType))")


更新:下载部分.在Maven Central上,下载尚不可用,但我想很快就会下载.如果可用,此链接将有效,当前它产生404错误.


Update: AspectJ 1.8.4 has been released, see also overview in the official download section. On Maven Central the download is not available yet, but it will be soon, I guess. When available, this link will be valid, currently it yields a 404 error.

那么为什么这个版本很有趣?由于上述票证已经解决,并且到目前为止还有新的切入点原语is(FinalType),请参阅

So why is this release interesting? Because the ticket mentioned above has been resolved and there is a new pointcut primitive is(FinalType) available as of now, see 1.8.4 release notes.

所以现在您要求的完整解决方案如下:

So now the full solution you requested looks like this:

@Pointcut(
    "execution(* com.mycom..*(..)) && " +
    "!within(is(EnumType)) && " +
    "!within(is(FinalType))"
)

我验证了它的工作原理.

I verified that it works like this.

这篇关于Spring AOP:从切入点中排除最终类和枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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