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

Spring Boot 2中Lombok与AspectJ集成问题求助

我之前也碰到过这个头疼的问题!Spring Boot里Lombok和AspectJ的编译顺序确实容易踩坑,ajc在编译时看不到Lombok生成的getter/setter,本质是因为Lombok的字节码增强是在javac之后,而ajc默认会直接处理原始源码,没等到Lombok生成代码。下面几个亲测有效的方案,你可以试试:

方案一:先通过Delombok生成完整源码,再交给Ajc编译

这个思路是让Lombok先把带@Data/@Getter/@Setter的源码转换成包含完整getter/setter的普通Java代码,再让AspectJ编译这些生成的代码。具体Maven配置如下:

<!-- 第一步:执行Delombok,生成带完整getter/setter的源码 -->
<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.18.30.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
            <configuration>
                <sourceDirectory>src/main/java</sourceDirectory>
                <outputDirectory>target/generated-sources/delombok</outputDirectory>
                <addOutputDirectory>false</addOutputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

<!-- 第二步:让Ajc编译Delombok生成的源码 -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.14.0</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <source>17</source> <!-- 替换成你实际使用的JDK版本 -->
        <target>17</target>
        <complianceLevel>17</complianceLevel>
        <!-- 指定编译目录包含Delombok生成的源码 + 原始源码 -->
        <sources>
            <source>
                <directory>target/generated-sources/delombok</directory>
            </source>
            <source>
                <directory>src/main/java</directory>
            </source>
        </sources>
        <weaveDirectories>
            <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
        </weaveDirectories>
        <forceAjcCompile>true</forceAjcCompile>
        <showWeaveInfo>true</showWeaveInfo>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.20.1</version>
        </dependency>
    </dependencies>
</plugin>

配置完成后执行mvn clean test,ajc编译的就是已经补全了getter/setter的代码,自然能识别到方法。

方案二:调整编译阶段,让Ajc处理Lombok增强后的字节码

如果不想生成额外的源码,也可以让javac先配合Lombok的注解处理器生成带getter/setter的字节码,再让ajc在后续阶段织入切面。配置如下:

<!-- 先让javac + Lomboker生成完整字节码 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <source>17</source>
        <target>17</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.30</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

<!-- 让Ajc在process-classes阶段处理已编译的字节码 -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.14.0</version>
    <executions>
        <execution>
            <phase>process-classes</phase> <!-- 确保在javac编译之后执行 -->
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <!-- 测试代码同理,调整test-compile的阶段 -->
        <execution>
            <id>test-compile</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <source>17</source>
        <target>17</target>
        <complianceLevel>17</complianceLevel>
        <weaveDirectories>
            <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
        </weaveDirectories>
        <forceAjcCompile>false</forceAjcCompile> <!-- 不强制编译源码,只处理已生成的类 -->
        <showWeaveInfo>true</showWeaveInfo>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.20.1</version>
        </dependency>
    </dependencies>
</plugin>

这个方案的好处是不用生成额外的源码目录,直接处理字节码,适合不想改动源码结构的场景。

额外注意事项

  • 版本兼容性:确保Lombok和AspectJ版本匹配,比如Lombok 1.18.x需要AspectJ 1.9.x及以上,尽量使用最新稳定版避免冲突。
  • Spring Boot插件:如果用了spring-boot-maven-plugin,确保它的repackage目标在Ajc编译之后执行,默认配置一般没问题,但如果出现异常可以检查插件执行顺序。
  • 清理缓存:每次测试前执行mvn clean,避免之前的编译缓存干扰结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:55:16