java:aocache 与Spring Aop兼容问题
创始人
2025-01-08 12:06:38
0

本文适用于所有AspectJ与Spring AOP混用的场景。

Spring AOP 是基于动态代理的实现AOP,基于 JDK代理和CGLib代理实现运行时织入(runtime weaving)。

Spring AOP的切面定义沿用了ASpectJ的注解体系,所以在Spring体系中注解定义切面的方式与ASpectJ完全兼容, 但如果一个项目中有Spring 切面定义又要使用aocache静态态织入(CTW)的情况下,就会存在冲突问题。

如下面的示例,项目中定义了一个Spring AOP切面,拦截所有带@RestController注解的服务方法

import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;  /**  *   * 服务上下文切面  *  */ @Aspect @Component public class ServiceContextAop {     @Before("restControllerAspect()") 	public void doServiceContextBefore(JoinPoint joinPoint) throws Throwable {         // DO SOMETHING     } 	/** 	 * 定义切入点为有注解{@link org.springframework.web.bind.annotation.RestController}下的所有类 	 */ 	@Pointcut("@within(org.springframework.web.bind.annotation.RestController)") 	public void restControllerAspect() { 	}  } 

pom.xml中如下定义了aspectj-maven-plugin插件在项目编译时静态织入aocache定义的切面

	 		 			 				org.codehaus.mojo 				aspectj-maven-plugin 				1.10 				 					1.8 					1.8 					UTF-8 					1.8 					true 					true 					 						 							com.gitee.l0km 							aocache 						 					 				 				 					 						 							compile 						 					 				 			 		 	 

编译时不会有任何问题,但是在程序运行时,就会发现ServiceContextAop定义的切面没有起作用。

因为aspectj-maven-plugin插件识别到了ServiceContextAop类上的@Aspect注解,将本该由Spring AOP管理的切面也执行了静态织入。如下为ServiceContextAop.class反编译显示的代码,可以看到ASpectJ在类中增加了额外代码。
在这里插入图片描述

为解决ASpectJ CTW模式与Spring AOP的共存问题,就要在aspectj-maven-plugin插件中使用 参数指定在执行静态织入时排除ServiceContextAop.java

 	 		 			org.codehaus.mojo 			aspectj-maven-plugin 			1.10 			 				1.8 				1.8 				UTF-8 				1.8 				true 				true 				 					 						com.gitee.l0km 						aocache 					 				     			                  	                **/ServiceContextAop.java                  			 			 				 					 						compile 					 				 			 		 	  

参考资料

关于 参数说明参见

《Using includes/excludes》

相关内容

热门资讯

黑科技数据!aapoker透明... 黑科技数据!aapoker透明挂(透视)第三方教程(2020已更新)(哔哩哔哩)1、下载好aapok...
黑科技计算(wepokeapp... 黑科技计算(wepokeapp下载教程)外挂透明挂辅助神器(透视)本来有挂(有挂详细)-哔哩哔哩;1...
黑科技有挂(wpk助手)外挂透... 黑科技有挂(wpk助手)外挂透明挂辅助下载(ai辅助)竟然真的有挂(了解有挂)-哔哩哔哩1)wpk助...
黑科技苹果版!德州之星透牌器(... 黑科技苹果版!德州之星透牌器(透视)插件教程(2025已更新)(哔哩哔哩)1、这是跨平台的德州之星透...
黑科技新版(WPK教程)外挂透... 黑科技新版(WPK教程)外挂透明挂辅助科技(ai代打)其实是有挂(有挂功能)-哔哩哔哩进入游戏-大厅...
辅助黑科技(wpk线上德州俱乐... 辅助黑科技(wpk线上德州俱乐部)外挂透明挂辅助神器(透视)真是有挂(的确有挂)-哔哩哔哩;1、在w...
黑科技能赢!wepower有a... 黑科技能赢!wepower有ai吗(透视)2025版教程(2024已更新)(哔哩哔哩)1、让任何用户...
黑科技ai代打(微扑克机器人)... 黑科技ai代打(微扑克机器人)外挂透明挂辅助助手(ai代打)一贯存在有挂(有挂解惑)-哔哩哔哩黑科技...
黑科技了解(aapoker能玩... 黑科技了解(aapoker能玩吗)外挂透明挂辅助插件(透视)本来真的有挂(有挂神器)-哔哩哔哩;1、...
黑科技挂黑科技!wpk积分后台... 黑科技挂黑科技!wpk积分后台管理系统(透视)第三方教程(2024已更新)(哔哩哔哩)运wpk积分后...