Spring Boot 3+IntelliJ+Lombok编译报错:找不到log符号
问题解决:Spring Boot 3 + Lombok @Log4j2 构建时报"cannot find symbol log"
问题分析
测试类中使用@Log4j2注解后,IntelliJ编辑器无报错,但执行Gradle构建时提示找不到log符号,核心原因是测试代码的Lombok注解处理器未配置——主代码的annotationProcessor仅处理生产代码,测试代码需要单独配置注解处理器才能让Lombok生成log实例。
解决方案
1. 完善Gradle依赖配置
修改build.gradle的dependencies块,添加测试代码的注解处理器依赖,并统一Lombok版本(避免版本不一致问题):
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' implementation 'org.springdoc:springdoc-openapi-starter-common:2.2.0' runtimeOnly group: 'com.oracle.database.jdbc', name: 'ojdbc11', version: '21.9.0.0' // Lombok 配置:统一版本,添加测试注解处理器 compileOnly 'org.projectlombok:lombok:1.18.28' annotationProcessor 'org.projectlombok:lombok:1.18.28' testImplementation 'org.projectlombok:lombok:1.18.28' testAnnotationProcessor 'org.projectlombok:lombok:1.18.28' testImplementation 'org.springframework.security:spring-security-test' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
2. 检查IntelliJ注解处理配置
确保IntelliJ已启用注解处理:
- 打开
Settings(Windows/Linux:File → Settings;Mac:IntelliJ IDEA → Settings) - 导航到
Build, Execution, Deployment → Compiler → Annotation Processors - 勾选
Enable annotation processing选项
3. 清理并重新构建
- 执行Gradle的
clean任务清除旧构建缓存 - 同步Gradle项目(点击右侧Gradle面板的刷新按钮)
- 重新执行构建任务
内容的提问来源于stack exchange,提问作者robert trudel
相关产品推荐
相关产品推荐

