java:aocache 与Spring Aop兼容问题

6 篇文章 0 订阅
5 篇文章 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定义的切面

	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>aspectj-maven-plugin</artifactId>
				<version>1.10</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
					<complianceLevel>1.8</complianceLevel>
					<verbose>true</verbose>
					<showWeaveInfo>true</showWeaveInfo>
					<aspectLibraries>
						<aspectLibrary>
							<groupId>com.gitee.l0km</groupId>
							<artifactId>aocache</artifactId>
						</aspectLibrary>
					</aspectLibraries>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

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

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

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

<build>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>aspectj-maven-plugin</artifactId>
			<version>1.10</version>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
				<encoding>UTF-8</encoding>
				<complianceLevel>1.8</complianceLevel>
				<verbose>true</verbose>
				<showWeaveInfo>true</showWeaveInfo>
				<aspectLibraries>
					<aspectLibrary>
						<groupId>com.gitee.l0km</groupId>
						<artifactId>aocache</artifactId>
					</aspectLibrary>
				</aspectLibraries>
    			<!-- 排除 ServiceContextAop.java,避免因为插件对之误织入导致的Spring AOP失效问题 -->
                <excludes>
	                <exclude>**/ServiceContextAop.java</exclude>
                </excludes>
			</configuration>
			<executions>
				<execution>
					<goals>
						<goal>compile</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

参考资料

关于 <exclude></exclude>参数说明参见

《Using includes/excludes》

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值