2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
@RequiredArgsConstructor implementiert die Konstruktorinjektion
@Autowired
Und@Resource
Anmerkung@Autowired
@Autowired
Es handelt sich um eine vom Spring-Framework bereitgestellte Annotation, mit der Abhängigkeiten automatisch zusammengestellt werden.@Autowired
private ISysUserService userService;
@Resource
@Resource
Es handelt sich um eine von Java EE bereitgestellte Annotation, die von Spring auch für die automatische Zusammenstellung von Abhängigkeiten unterstützt wird.@Resource
private ISysUserService userService;
@RequiredArgsConstructor
@RequiredArgsConstructor
ist eine von Lombok bereitgestellte Annotation, die alle automatisch generiertfinal
Feldkonstruktor.@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
Und@Resource
)@RequiredArgsConstructor
Diese Belastung kann gemildert werden.@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;
// 其他代码
}
Durch diese Methode der Konstruktorinjektion können nicht nur die Robustheit und Wartbarkeit des Codes verbessert werden, sondern auch die Abhängigkeitsinjektionsfunktionen von Spring und die Vorteile des vereinfachten Codes von Lombok besser genutzt werden.
Spring's Constructor Injection_spring Constructor Injection-CSDN Blog
Bei Verwendung der Konstruktorinjektion müssen keine zusätzlichen Anmerkungen hinzugefügt werden, Sie müssen lediglich den Konstruktor bereitstellen. Spring erkennt Ihren Konstruktor automatisch und führt eine Abhängigkeitsinjektion durch.
@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;
}
}
Beispielcode
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 diesem Beispiel ist es nicht erforderlich, zusätzliche Anmerkungen zum Kommentieren des Konstruktors zu verwenden. Spring erkennt die Abhängigkeit automatisch und fügt sie ein. (Wenn markiert, wird natürlich kein Fehler gemeldet, aber nur @Autowired kann verwendet werden, @Resouce kann nicht verwendet werden.@Resource
Anmerkungen werden normalerweise für die Injektion von Feld- oder Setter-Methoden verwendet.
@Autowired
Annotation, um explizit anzugeben, welcher Konstruktor verwendet werden soll.Beispielcode (mehrere Konstruktoren)
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;
}
// 你的控制器方法
}
Da es in diesem Beispiel mehrere Konstruktoren gibt, müssen Sie diese verwenden @Autowired
Anmerkungen, um explizit anzugeben, welchen Konstruktor Spring für die Abhängigkeitsinjektion verwendet.