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》

相关内容

热门资讯

计算辅助挂(德州)app辅助器... 计算辅助挂(德州)app辅助器怎么用(Ai)软件铺牌器(一直真的有挂)一、AI软件牌型概率发牌机制”...
黑科技科技!(wEpOke)软... 黑科技科技!(wEpOke)软件透明挂,(wEpOke)一向是有挂,介绍教程(有挂攻略);支持2-1...
黑科技科技(红龙扑克)外挂辅助... 黑科技科技(红龙扑克)外挂辅助神器(透视)透明挂教程(果然是真的有挂)1、超多福利:超高返利,海量正...
wpk透视辅助合作(wPK)w... wpk透视辅助合作(wPK)wpk微扑克最新辅助(透视)必胜教程(切实真的是有挂)1、每一步都需要思...
最新辅助挂(德扑之星)ai代打... 最新辅助挂(德扑之星)ai代打风险分析(AI)ai代打(确实是有挂)1、每一步都需要思考,不同水平的...
黑科技美元局!(德州ai)外挂... 黑科技美元局!(德州ai)外挂辅助神器,(德州ai)都是真的有挂,我来教教你(有挂辅助)科技教程也叫...
黑科技app(微扑克机器人)外... 黑科技app(微扑克机器人)外挂透视辅助软件(透视)AI教程(一直是有挂)1、任何微扑克机器人ai辅...
wpkai透视外挂售卖(wPK... wpkai透视外挂售卖(wPK)wpk如何才能稳定长期收益(透视)解说技巧(原来真的有挂);一、wp...
插件辅助挂(德州之星)ai人工... 插件辅助挂(德州之星)ai人工智能软件下载(Ai)ai购买(好像真的是有挂)1、金币登录送、破产送、...
黑科技脚本!(wePoke)软... 黑科技脚本!(wePoke)软件透明挂,(WepokE)真是是真的有挂,解说技巧(有挂技巧);1分钟...