Understanding Spring Dependency Injection and Lombok @RequiredArgsConstructor
This article explains Spring's three dependency injection methods—field, constructor, and setter injection—highlights the advantages of constructor injection for avoiding null pointers and circular dependencies, and demonstrates how Lombok's @RequiredArgsConstructor can simplify bean wiring in Java backend development.
Hello everyone, I'm Chen.
Summary: we replace multiple @Autowired and @Resource annotations on a class with Lombok's @RequiredArgsConstructor, simplifying the code.
Dependency Injection
First, a quick review of Spring's three dependency injection types.
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 first, then by type if no matching bean name is found. Using @Qualifier with @Autowired can also specify a bean name.
IDE warnings such as "Field injection is not recommended" appear because the official recommendation is to use constructor injection, which avoids issues like null pointer exceptions.
1. 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 forces dependencies to be provided at object creation, preventing NullPointerException and allowing final fields. Spring recommends it because it also helps avoid circular dependencies, which would cause startup errors.
Constructor injection cannot resolve circular dependencies because the constructor needs all dependencies before the object can be instantiated.
Illustration of circular dependency handling in Spring (image omitted).
Solutions for circular dependencies when using constructor injection:
Refactor code
Use @Lazy annotation
Switch to field injection
Reference: https://zhuanlan.zhihu.com/p/562691467
2. 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 injection will fail. Note that fields injected via setter or field injection cannot be declared as final.
@RequiredArgsConstructor
Some may argue against Lombok, but it is a useful tool for rapid development.
After covering Spring's three injection methods, we introduce Lombok's @RequiredArgsConstructor.
Lombok provides three constructor-generating annotations: @NoArgsConstructor, @RequiredArgsConstructor, and @AllArgsConstructor; here we focus on @RequiredArgsConstructor.
@Controller
@RequiredArgsConstructor
public class SysUserController extends BaseController {
private final ISysUserService userService;
private final ISysRoleService roleService;
// ----------------------------
}Using @RequiredArgsConstructor generates a private constructor that includes all final fields, allowing us to eliminate @Autowired and @Resource annotations and rely on constructor injection, which reduces boilerplate and avoids null pointer issues.
Even without Lombok, you can manually create a similar constructor or implement a global solution to achieve the same effect.
Source: juejin.cn/post/7146035741234036744
Final Note (Support Requested)
If you find this article helpful, please like, view, share, and bookmark it; your support motivates me to keep writing.
Additional promotional information about paid content and community groups follows, but the technical content above constitutes the core educational value.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.