微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring AOP:排除避免切入点的最终类和枚举

我正在尝试使用Spring AOP实现Logging.我已定义了

@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.getSignature().getName());
    logger.info("INFO:: " + jp.getTarget().getClass().getName() + " " + jp.getSignature().getName() + " Finished:");
    return returnVal;
}

mycom包及其子包下有很多类.有些课程是enum和final class.
因此,我得到了

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)

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

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

我试过用

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

但我得到以下错误

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

!内(是(FinalType))

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

    

我还添加了这个maven依赖项

   

现在,一切都像魅力一样.有什么想法,这里发生了什么?

最佳答案
目前,您可以通过在AspectJ 1.6.9中引入的is()切入点语法来排除枚举,方面,接口,内部类型,匿名类型,另请参阅我的答案here.

目前你不能做的是通过AspectJ语法排除最终类型.但我认为这是有道理的,所以我为它创造了一个ticket.

如何排除枚举:

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

更新:AspectJ 1.8.4已经发布,请参阅官方download section中的概述.在Maven Central上,下载尚不可用,但我想很快就会发布.如果可用,this link将有效,目前它产生404错误.

那么为什么这个版本很有趣?因为上面提到的票证已经解决,并且现在有一个新的切入点原语(FinalType),见1.8.4 release notes.

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

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

我确认它的工作原理是这样的.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐