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》

相关内容

热门资讯

好友辅助挂(德州ai)辅助器有... 好友辅助挂(德州ai)辅助器有哪些功能(Ai)辅助挂(好像有挂)运辅助工具,进入游戏界面。进入辅助器...
黑科技辅助(AaPOKER)透... 黑科技辅助(AaPOKER)透明挂(透视)辅助工具存在(一贯是真的有挂);科技安装教程;136704...
科普黑科技(AAPOKer)透... 科普黑科技(AAPOKer)透视辅助(透视)透视挂效果(果然存在有挂)1、ai辅助优化,发牌逻辑科技...
黑科技数据(we poker)... 1、黑科技数据(we poker)软件透明演示(黑科技)有挂么(先前是有挂)。2、透视辅助简单,软件...
安卓版辅助挂(德州ai)插件(... 安卓版辅助挂(德州ai)插件(Ai)外挂(原来是真的有挂)在进入辅助挂后,参与本局比赛的八名玩家的i...
黑科技总结(AAPoker)德... 自定义德州ai辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是...
科普黑科技(AAPOKeR)系... 科普黑科技(AAPOKeR)系统机制(透视)辅助(本来有挂)1、机器人多个强度级别选择2、发牌规律人...
黑科技安卓版(we poke)... 黑科技安卓版(we poke)辅助挂(黑科技)透明挂辅助(确实有挂);1分钟了解详细教程(微信 13...
攻略黑科技(AApOKER)辅... 攻略黑科技(AApOKER)辅助是什么(透视)辅助(原来是真的有挂);1、上手简单,内置详细流程视频...
黑科技辅助(WepokE)辅助... 黑科技辅助(WepokE)辅助使用方法(黑科技)软件透明挂的使用教程(都是真的有挂);建议优先通过透...