如何让AspectJ的@Around切面不匹配接口的staticinitialization?
解决AspectJ静态初始化切点匹配接口导致的编译错误
你使用如下切点时遇到了AspectJ编译限制错误:
@Around("staticinitialization(*)")
错误信息:
[iajc] [error 4]: error at /home/dave/dev/coyote/projects/aspects/debug_src/com/acme/aspects/exceptions/StaticInitializerExceptions.java:15::443 around on staticinitialization of interface 'com.acme.intgservices.discussions.DiscussionListener' not supported (compiler limitation)
这是因为staticinitialization(*)会匹配所有类和接口的静态初始化,但AspectJ不支持对接口的静态初始化方法应用@Around增强。
要让AspectJ不选择接口,只需修改切点表达式,排除接口类型即可:
@Around("staticinitialization(clazz) && !isInterface(clazz)")
或者使用内置变量简化写法:
@Around("staticinitialization(*) && !isInterface(targetType)")
修改后的切点只会匹配非接口类型(普通类、抽象类、枚举等)的静态初始化,从而避开编译器的限制。
内容的提问来源于stack exchange,提问作者MeBigFatGuy
相关产品推荐
相关产品推荐

