When working on a Spring Boot project, you might encounter the error “variable not initialized in the default constructor”. This issue is common, especially when using Lombok annotations like @RequiredArgsConstructor
or when dependency injection isn’t set up properly. In this post, we’ll walk through the root cause of the error and provide two solutions to get your code running without a hitch.
Understanding the Error
The error message “variable not initialized in the default constructor” occurs when a final
variable isn’t initialized in the constructor. In many Spring Boot applications, this error is linked to Lombok’s @RequiredArgsConstructor
annotation, which is designed to auto-generate a constructor for all final
fields. If Lombok isn’t correctly configured or there’s a conflict, the expected constructor might not be generated, resulting in the error.
Breaking Down the Problem
Consider the following code snippets from a typical Spring Boot project:
Entity Class
package com.example.demo.entity;
import jakarta.persistence.*;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String email;
private String phone;
private String department;
}
Repository Interface
package com.example.demo.repository;
import com.example.demo.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
Service Class
package com.example.demo.service;
import com.example.demo.entity.Employee;
import com.example.demo.repository.EmployeeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class EmployeeService {
private final EmployeeRepository employeeRepository;
public Employee postEmployee(Employee employee) {
return employeeRepository.save(employee);
}
}
Controller Class
package com.example.demo.controller;
import com.example.demo.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.example.demo.entity.Employee;
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class EmployeeController {
private final EmployeeService employeeService;
@PostMapping("/employee")
public Employee postEmployee(@RequestBody Employee employee) {
System.out.println(employee);
return employeeService.postEmployee(employee);
}
}
In this example, the error is triggered because Lombok’s @RequiredArgsConstructor
isn’t generating the constructor for EmployeeService
and EmployeeController
. This leaves the final
dependencies uninitialized.
The Solutions
There are two approaches to fix this issue:
Solution 1: Manually Define Constructors
If Lombok is not working as expected, you can explicitly define constructors using @Autowired
to ensure that dependencies are properly injected.
Updated Service Class
package com.example.demo.service;
import com.example.demo.entity.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
private final EmployeeRepository employeeRepository;
@Autowired
public EmployeeService(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
public Employee postEmployee(Employee employee) {
return employeeRepository.save(employee);
}
}
Updated Controller Class
package com.example.demo.controller;
import com.example.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.demo.entity.Employee;
@RestController
@RequestMapping("/api")
public class EmployeeController {
private final EmployeeService employeeService;
@Autowired
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@PostMapping("/employee")
public Employee postEmployee(@RequestBody Employee employee) {
System.out.println(employee);
return employeeService.postEmployee(employee);
}
}
Solution 2: Correct Lombok Configuration
If you prefer to leverage Lombok, ensure it’s set up correctly in your project:
Maven Dependency in pom.xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.36</version>
<scope>provided</scope>
</dependency>
Enable Annotation Processing in Your IDE
- IntelliJ IDEA: Navigate to
File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors
and enable annotation processing. - Eclipse: Install the Lombok plugin and enable annotation processing in your project settings.
Why These Solutions Work
- Manual Constructor Definition: By explicitly defining constructors and using
@Autowired
, you ensure that Spring Boot injects the required dependencies, thereby avoiding the uninitializedfinal
variable error. - Proper Lombok Setup: When Lombok is correctly configured,
@RequiredArgsConstructor
generates the necessary constructors, keeping your code concise and error-free.
Best Practices
- Constructor Injection: Always prefer constructor injection for your Spring Boot projects—it enhances testability and minimizes the risk of circular dependencies.
- Verify Lombok Configuration: Regularly check your IDE settings and project dependencies to ensure Lombok is correctly integrated.
- Immutable Dependencies: Declaring dependencies as
final
improves code robustness by promoting immutability and thread safety.
Conclusion
The “variable not initialized in the default constructor” error is a common stumbling block in Spring Boot applications, especially when using Lombok. Whether you choose to manually create constructors or adjust your Lombok configuration, addressing this issue will make your dependency injection seamless and your application more stable.
If you found this guide useful, share it with fellow developers and stay tuned for more Spring Boot tips and tutorials!
Keywords
- Spring Boot variable not initialized
- Lombok @RequiredArgsConstructor error
- Fix default constructor error in Spring Boot
- Spring Boot dependency injection
- Lombok configuration in Spring Boot
- Constructor injection in Spring Boot
- Spring Boot best practices
Happy coding! 🚀