Using Lombok @RequiredArgsConstructor to Replace @Autowired and @Resource for Spring Dependency Injection
This article explains how Lombok's @RequiredArgsConstructor can replace multiple @Autowired and @Resource annotations by promoting constructor injection, compares field, constructor, and setter injection in Spring, and demonstrates the resulting cleaner code with practical examples.
Direct summary: we use Lombok's @RequiredArgsConstructor annotation on a class to replace multiple occurrences of @Autowired and @Resource .
Dependency Injection
First, a quick review of Spring's three types of dependency injection.
Field Injection
public class SysUserController extends BaseController {
@Autowired
private ISysUserService userService;
@Resource
private ISysRoleService roleService;
}@Autowired injects by type by default, while @Resource injects by name and falls back to type when no matching bean name is found.
Using @Qualifier together with @Autowired can also specify a bean name for name‑based injection.
IDEA will warn when @Autowired is placed on a field: "Field injection is not recommended".
The official recommendation is to use constructor injection because field injection has obvious drawbacks, such as the injected object cannot be declared final and potential NullPointerException issues.
Constructor Injection
public class SysUserController extends BaseController {
private final ISysUserService userService;
private final ISysRoleService roleService;
public SysUserController(ISysUserService userService, ISysRoleService roleService) {
this.userService = userService;
this.roleService = roleService;
}
}Constructor injection triggers the container to call the class constructor, guaranteeing that required dependencies are provided and preventing NullPointerException .
Spring officially recommends constructor injection not only because member fields can be marked final , but also because it helps avoid circular dependencies; if a circular dependency exists, Spring will fail to start.
Why is it avoidance rather than resolution?
Because constructor injection creates objects via the constructor, which must obtain all dependencies before the object can be instantiated; such circular dependencies cannot be resolved.
The following diagram (omitted) illustrates whether Spring can resolve circular dependencies when A and B depend on each other using different injection methods.
Ways to handle circular dependencies when using constructor injection:
Refactor the code
Use the @Lazy annotation
Switch to field injection
Reference: https://zhuanlan.zhihu.com/p/562691467
Setter Injection
public class SysUserController extends BaseController {
private ISysUserService userService;
@Autowired
public void setUserService(ISysUserService userService) {
this.userService = userService;
}
}When using setter injection, you must annotate the setter with @Autowired or @Resource ; otherwise the injection will fail.
Note that both field injection and setter injection variables cannot be declared final .
@RequiredArgsConstructor
Some may argue against using Lombok, but when we understand its purpose it becomes a valuable tool for rapid development.
After covering Spring's three injection methods, we introduce Lombok's @RequiredArgsConstructor annotation.
Lombok provides three constructor‑generating annotations: @NoArgsConstructor , @RequiredArgsConstructor , and @AllArgsConstructor . This article focuses on @RequiredArgsConstructor .
@Controller
@RequiredArgsConstructor
public class SysUserController extends BaseController {
private final ISysUserService userService;
private final ISysRoleService roleService;
// ----------------------------
}Applying @RequiredArgsConstructor generates a private constructor that includes all final fields, eliminating the need for explicit @Autowired or @Resource annotations.
Thus, we can avoid field injection, simplify the code, and prevent potential null‑pointer problems.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.