SpringBoot private final注入Service报默认构造器未初始化错误
问题描述
SpringBoot初学者开发缓存相关应用时,项目构建阶段持续抛出如下错误:
error: variable myApplicationService not initialized in the default constructor
对照参考教程无法理解对应字段的初始化逻辑,曾尝试添加@Autowired注解注入,但该方式已不被推荐,且添加后项目构建依然失败。
PersonController 代码
package com.springbootredis.controllers; import com.springbootredis.service.MyApplicationService; import lombok.AllArgsConstructor; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController @AllArgsConstructor @RequestMapping(value = "/person", produces = { MediaType.APPLICATION_JSON_VALUE }) public class PersonController { private final MyApplicationService myApplicationService;// 报错位置 @GetMapping("/uuid") public String generateRandomUUID() { return UUID.randomUUID().toString(); } @GetMapping("/addperson/{name}") public String addPerson(@PathVariable String name) { String ret = myApplicationService.addNewPerson(name); return "Added person with name: " + name + " and id: " + ret; } @GetMapping("/deleteperson/{id}") public String deletePerson(@PathVariable String id) { String ret = myApplicationService.delete(id); return "Deleted person. ID:" + id + " Name: " + ret; } @GetMapping("/updateperson/{id}/{name}") public String updatePerson(@PathVariable String id, @PathVariable String name) { myApplicationService.updatePerson(id, name); return "Updated person. ID:" + id + " with Name: " + name; } @GetMapping("/getperson/{id}") public String getPerson(@PathVariable String id) { String ret = myApplicationService.findById(id); return "Got person. ID:" + id + " Name: " + ret; } }
build.gradle 配置
plugins { id 'org.springframework.boot' version '2.7.1' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' id 'war' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '11' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-data-redis:2.7.0") implementation("org.springframework.boot:spring-boot-starter-cache:2.7.1") implementation("org.projectlombok:lombok:1.18.24") implementation("org.springframework.boot:spring-boot-dependencies:2.7.1") runtimeOnly("mysql:mysql-connector-java:8.0.29") providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
排查与解决思路
报错核心原因是Lombok的@AllArgsConstructor注解没有在编译期正常生成包含所有final字段的全参构造器,Spring无法通过构造器完成依赖注入,只能调用默认无参构造实例化对象,导致final字段未被初始化。按以下步骤逐一排查即可:
- 确认IDE的Lombok环境配置正常:安装对应IDE版本的Lombok插件,同时开启注解处理开关。以IDEA为例,路径为
Settings > Build, Execution, Deployment > Compiler > Annotation Processors,勾选Enable annotation processing。 - 修正Lombok依赖配置:当前配置中Lombok仅声明为
implementation作用域,Gradle编译阶段不会触发Lombok的注解处理逻辑。需要将原有Lombok依赖替换为如下配置,覆盖编译、测试全阶段的注解处理需求:
compileOnly "org.projectlombok:lombok:1.18.24" annotationProcessor "org.projectlombok:lombok:1.18.24" testCompileOnly "org.projectlombok:lombok:1.18.24" testAnnotationProcessor "org.projectlombok:lombok:1.18.24"
- 移除多余依赖:删除dependencies中
implementation("org.springframework.boot:spring-boot-dependencies:2.7.1")配置,该依赖集已经通过io.spring.dependency-management插件自动完成版本管理,手动引入容易引发依赖冲突,间接导致注解处理器运行异常。 - 确认待注入的Service被Spring容器管理:检查
MyApplicationService的实现类上是否添加了@Service、@Component等构造型注解,且类所在包路径在SpringBoot启动类的组件扫描范围内,避免修复编译问题后出现Bean找不到的运行时错误。 - 清理构建缓存重新编译:在项目根目录执行
./gradlew clean build命令,清除之前的错误编译产物后重新构建项目。
内容的提问来源于stack exchange,提问作者Ken
相关产品推荐
相关产品推荐

