加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:39:01 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用Spring AOP实现Logging.我已定义了 @Pointcut("execution(* com.mycom..*(..))")private void framework() {}@Around("framework()")public Object aroundAdviceFramework(ProceedingJoinPoint jp) throws Throwable { if (logger.isDebugEnab

我正在尝试使用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))"
)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读