Spring AOP: exclude avoid final classes and enums from pointcut

Deadly 提交于 2019-11-30 14:42:09
kriegaex

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!