Technology Sharing

@RequiredArgsConstructor implements constructor injection

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

@RequiredArgsConstructor implements constructor injection

1. @Autowired and@Resource annotation

@Autowired
  • @Autowired It is an annotation provided by the Spring framework for automatically assembling dependencies.
  • Can be used on fields, constructors, and setter methods.
@Autowired
private ISysUserService userService;
  • 1
  • 2
@Resource
  • @Resource It is an annotation provided by Java EE and also supported by Spring for automatic assembly of dependencies.
  • Generally used for fields and setter methods.
@Resource
private ISysUserService userService;
  • 1
  • 2

2. Constructor Injection

Using Lombok @RequiredArgsConstructor

The concept and use of springboot @RequiredArgsConstructor - Tencent Cloud Developer Community - Tencent Cloud (tencent.com)

  • @RequiredArgsConstructor It is an annotation provided by Lombok, which automatically generates allfinal The constructor for the field.
  • Using constructor injection ensures that dependency injection is done when the object is created, ensuring that all dependencies are non-null.
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final ISysPostService postService;
    private final ISysDeptService deptService;
    private final ISysUserPostService userPostService;
    
    // 构造函数由 Lombok 自动生成,注入所有 final 字段
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3. Comparison and advantages

Field injection (@Autowired and@Resource
  • advantage:The code is concise and annotates directly on the fields.
  • shortcoming
    • Difficult to unit test because mock objects need to be injected via reflection or other means.
    • Dependency injection occurs after the object is created, which may cause problems where dependencies are not fully initialized.
    • The dependency inversion principle is violated, and the class depends directly on the container.
Constructor Injection
  • advantage
    • Force dependencies to be fully initialized when the object is created, ensuring that all dependencies are non-null.
    • Easier to unit test because mock objects can be injected through the constructor.
    • It is more in line with the Dependency Inversion Principle and makes the class more independent of the container.
    • Improves code readability and maintainability, especially when there are many dependencies.
  • shortcoming: Requires additional constructor code, but uses Lombok's @RequiredArgsConstructor This burden can be alleviated.

4. Example comparison

use @Autowired
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    
    @Autowired
    private ISysUserService userService;
    
    @Autowired
    private ISysRoleService roleService;
    
    @Autowired
    private ISysPostService postService;
    
    @Autowired
    private ISysDeptService deptService;
    
    @Autowired
    private ISysUserPostService userPostService;

    // 其他代码
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
Using Constructor Injection
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final ISysPostService postService;
    private final ISysDeptService deptService;
    private final ISysUserPostService userPostService;

    // 其他代码
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

This method of constructor injection can not only enhance the robustness and maintainability of the code, but also better utilize the dependency injection features of Spring and the advantages of Lombok's simplified code.

benefit

Spring's Constructor Injection_Spring Constructor Injection-CSDN Blog
Please add a description of the image

5. About Annotations

When using constructor injection, you don’t need to add additional annotations, just provide a constructor. Spring will automatically detect your constructor and perform dependency injection.

Use constructor injection, no additional annotations required
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final ISysPostService postService;
    private final ISysDeptService deptService;
    private final ISysUserPostService userPostService;

    // 自己编写构造函数
    public SysUserController(ISysUserService userService, 
                             ISysRoleService roleService, 
                             ISysPostService postService, 
                             ISysDeptService deptService, 
                             ISysUserPostService userPostService) {
        this.userService = userService;
        this.roleService = roleService;
        this.postService = postService;
        this.deptService = deptService;
        this.userPostService = userPostService;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
Key points for using constructor injection
  1. Define the constructor:
    • Define a constructor in the class that includes all the required dependencies.
  2. Spring automatically injects:
    • When Spring creates a Bean instance, it automatically recognizes and calls the constructor and injects the required dependencies.

Sample Code

package com.example.demo.controller;

import com.example.demo.service.ISysUserService;
import com.example.demo.service.ISysRoleService;
import com.example.demo.service.ISysPostService;
import com.example.demo.service.ISysDeptService;
import com.example.demo.service.ISysUserPostService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/system/user")
public class SysUserController {

    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final ISysPostService postService;
    private final ISysDeptService deptService;
    private final ISysUserPostService userPostService;

    // 构造函数注入
    public SysUserController(ISysUserService userService, 
                             ISysRoleService roleService, 
                             ISysPostService postService, 
                             ISysDeptService deptService, 
                             ISysUserPostService userPostService) {
        this.userService = userService;
        this.roleService = roleService;
        this.postService = postService;
        this.deptService = deptService;
        this.userPostService = userPostService;
    }

    // 你的控制器方法
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

In this example, you do not need to use any additional annotations to annotate the constructor, Spring will automatically identify and inject dependencies. (Of course, there will be no error if you annotate, but you can only use @Autowired, not @Resouce.@Resource Annotations are usually used for field or setter method injection)

Additional circumstances
  1. Multiple constructors:
    • If a class has multiple constructors, and only one of them has injection parameters, Spring will use this constructor for injection.
    • If there are multiple constructors with injection parameters, you need to use @Autowired Annotation to explicitly specify which constructor to use.

Example code (multiple constructors)

package com.example.demo.controller;

import com.example.demo.service.ISysUserService;
import com.example.demo.service.ISysRoleService;
import com.example.demo.service.ISysPostService;
import com.example.demo.service.ISysDeptService;
import com.example.demo.service.ISysUserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/system/user")
public class SysUserController {

    private final ISysUserService userService;
    private final ISysRoleService roleService;
    private final ISysPostService postService;
    private final ISysDeptService deptService;
    private final ISysUserPostService userPostService;

    // 使用 @Autowired 明确指定使用哪个构造函数
    @Autowired
    public SysUserController(ISysUserService userService, 
                             ISysRoleService roleService, 
                             ISysPostService postService, 
                             ISysDeptService deptService, 
                             ISysUserPostService userPostService) {
        this.userService = userService;
        this.roleService = roleService;
        this.postService = postService;
        this.deptService = deptService;
        this.userPostService = userPostService;
    }

    // 另一个构造函数
    public SysUserController(ISysUserService userService) {
        this.userService = userService;
        this.roleService = null;
        this.postService = null;
        this.deptService = null;
        this.userPostService = null;
    }

    // 你的控制器方法
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

In this example, since there are multiple constructors, you need to use @Autowired Annotation to explicitly specify which constructor Spring uses for dependency injection.