Spring AOP: exclude avoid final classes and enums from pointcut

前端 未结 1 1134
悲哀的现实
悲哀的现实 2021-01-01 02:08

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

@Pointcut(\"execution(* com.mycom..*(..))\         


        
相关标签:
1条回答
  • 2021-01-01 02:23

    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.

    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.

    How to exclude enums:

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题