2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
@RequiredArgsConstructor implements constructor injection
@Autowired
and@Resource
annotation@Autowired
@Autowired
It is an annotation provided by the Spring framework for automatically assembling dependencies.@Autowired
private ISysUserService userService;
@Resource
@Resource
It is an annotation provided by Java EE and also supported by Spring for automatic assembly of dependencies.@Resource
private ISysUserService userService;
@RequiredArgsConstructor
@RequiredArgsConstructor
It is an annotation provided by Lombok, which automatically generates allfinal
The constructor for the field.@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 字段
}
@Autowired
and@Resource
)@RequiredArgsConstructor
This burden can be alleviated.@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;
// 其他代码
}
@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;
// 其他代码
}
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.
Spring's Constructor Injection_Spring Constructor Injection-CSDN Blog
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.
@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;
}
}
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;
}
// 你的控制器方法
}
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)
@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;
}
// 你的控制器方法
}
In this example, since there are multiple constructors, you need to use @Autowired
Annotation to explicitly specify which constructor Spring uses for dependency injection.