You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置aspectj-maven-plugin仅织入依赖切面忽略本地Spring AOP切面

问题

在Maven项目中同时使用AspectJ和Spring AOP,希望对依赖my-aspect-library提供的切面启用编译时织入(compile-time weaving),同时保留Spring基于代理的AOP处理项目内定义的切面。

场景详情:

  • 依赖my-aspect-library中包含如下@Aspect切面:
@Aspect
public class MyAspect {
    @Around("execution(@com.example.MyAnnotation * *(..))")
    public Object execute(ProceedingJoinPoint jp) throws Throwable {
        // some handling
        Object res = jp.proceed();
        // some handling
        return res;
    }
}
  • 使用aspectj-maven-plugin并配置aspectLibrary,仅想为MyAspect启用编译时织入,但构建时发现插件同时织入了本地的Spring AOP切面(A、B、C),日志示例:

Join Point 'method-excecution(java.util.List X.myMethod(String) in Type 'X') advised by around advice from 'com/company/prod//aspects/A (from A.java)'

当前插件配置:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
        <executions>
          <execution>
            <id>weave-main-classes</id>
            <phase>process-classes</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
                <complianceLevel>11</complianceLevel>
                <source>11</source>
                <target>11</target>
                <showWeaveInfo>true</showWeaveInfo>
                <forceAjcCompile>true</forceAjcCompile>
                <weaveDirectories>
                    <weaveDirectory>${project.basedir}/target/classes</weaveDirectory>
                </weaveDirectories>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.example.group</groupId>
                        <artifactId>my-aspect-library</artifactId>
                    </aspectLibrary>
            </aspectLibraries>
            </configuration>
          </execution>
        </executions>
    </plugin>
</plugins>

核心需求:如何配置aspectj-maven-plugin仅织入依赖JAR中的切面,忽略项目内的本地切面?

解决方案

要实现仅织入依赖库中的切面、忽略本地Spring AOP切面,需调整aspectj-maven-plugin的配置,关键是排除本地@Aspect类被AspectJ编译器处理,具体步骤如下:

  • 添加<excludes>配置排除本地切面类
    在插件的<configuration>中添加<excludes>节点,指定本地切面所在的包路径,让AspectJ编译器跳过这些类的织入处理。
  • 调整<forceAjcCompile>为false(可选但推荐)
    原配置中forceAjcCompile=true会强制用AJC编译所有类,改为false后,仅对需要织入的类进行处理,避免不必要的编译操作。

修改后的完整插件配置:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
        <executions>
            <execution>
                <id>weave-main-classes</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
                <configuration>
                    <complianceLevel>11</complianceLevel>
                    <source>11</source>
                    <target>11</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <!-- 关闭强制AJC编译,仅处理需要织入的类 -->
                    <forceAjcCompile>false</forceAjcCompile>
                    <weaveDirectories>
                        <weaveDirectory>${project.basedir}/target/classes</weaveDirectory>
                    </weaveDirectories>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>com.example.group</groupId>
                            <artifactId>my-aspect-library</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <!-- 排除本地切面所在的包,替换为你实际的切面包路径 -->
                    <excludes>
                        <exclude>com/company/prod/aspects/**/*.java</exclude>
                        <!-- 如果有多个切面包,继续添加exclude节点 -->
                    </excludes>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

配置说明

  • <excludes>:通过通配符指定本地切面类的路径,确保这些类不会被AspectJ编译器识别为切面进行织入,而是交给Spring AOP通过代理方式处理。
  • forceAjcCompile=false:让AJC仅对需要织入切面的类进行处理,而非编译所有代码,既提升构建效率,又避免误处理本地切面。

内容的提问来源于stack exchange,提问作者Shadow Hatake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 21:24:49