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

Spring项目中注解(含Lombok)失效问题求助

Spring项目中注解(含Lombok)失效问题求助

各位大佬,我现在碰到一个棘手的问题:我的Spring基础项目里所有注解都完全失效了,这里我拿Lombok的注解来举例说明,会更直观。

我先拿一个简单的类做示例:

//...
@Service
@RequiredArgsConstructor
public class EmployeeService {
    private final EmployeeRepository employeeRepository;

    public List<Employee> getAllEmployees() {
        return List.of(new Employee().setId(123L));
    }

    public EmployeeService(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }
}

这段代码里,@RequiredArgsConstructor注解被标红了,原因是我手动实现了这个注解本该生成的构造方法——这说明IDE是能识别到这个注解,也明白它的作用的。

但如果我删掉手动写的构造方法,尝试构建项目时就会报错:

java: variable employeeRepository not initialized in the default constructor

也就是说,编译的时候注解生成的构造方法根本没生效。

我已经试过这些解决办法了:

  • IDE里已经勾选了Annotation Processors选项
  • 点击Reload all maven projects重新构建项目
  • 在pom.xml里添加了如下依赖:
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    
  • 执行了File --> Invalidate caches,勾选了「Clear file system cache and local history」和「Clear downloaded shared indexes」,然后Invalidate and restart,之后重新构建了项目

备注:内容来源于stack exchange,提问作者ZhenShen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 11:24:29