@RequiredArgsConstructor如何實(shí)現(xiàn)構(gòu)造器注入
@RequiredArgsConstructor實(shí)現(xiàn)構(gòu)造器注入
1.@Autowired和@Resource 注解
@Autowired
@Autowired
是 Spring 框架提供的注解,用于自動(dòng)裝配依賴。- 可以用于字段、構(gòu)造函數(shù)和 setter 方法。
@Autowired private ISysUserService userService;
@Resource
@Resource
是 Java EE 提供的注解,Spring 也支持它,用于自動(dòng)裝配依賴。- 一般用于字段和 setter 方法。
@Resource private ISysUserService userService;
2.構(gòu)造函數(shù)注入
使用 Lombok 的 @RequiredArgsConstructor
springboot @RequiredArgsConstructor的概念與使用
@RequiredArgsConstructor
是 Lombok 提供的注解,它會(huì)自動(dòng)生成包含所有final
字段的構(gòu)造函數(shù)。- 使用構(gòu)造函數(shù)注入可以確保依賴注入在對(duì)象創(chuàng)建時(shí)完成,確保所有依賴都是非空的。
@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; // 構(gòu)造函數(shù)由 Lombok 自動(dòng)生成,注入所有 final 字段 }
3.比較和優(yōu)點(diǎn)
字段注入(@Autowired
和 @Resource
)
優(yōu)點(diǎn):代碼簡(jiǎn)潔,直接在字段上注解。
缺點(diǎn):
- 難以進(jìn)行單元測(cè)試,因?yàn)樾枰ㄟ^(guò)反射或其他方式注入 mock 對(duì)象。
- 依賴注入發(fā)生在對(duì)象創(chuàng)建之后,可能導(dǎo)致依賴未完全初始化的問(wèn)題。
- 違反了依賴倒置原則,類直接依賴于容器。
構(gòu)造函數(shù)注入
優(yōu)點(diǎn):
- 強(qiáng)制依賴在對(duì)象創(chuàng)建時(shí)就完全初始化,確保所有依賴非空。
- 更容易進(jìn)行單元測(cè)試,因?yàn)榭梢酝ㄟ^(guò)構(gòu)造函數(shù)注入 mock 對(duì)象。
- 更符合依賴倒置原則,使類更獨(dú)立于容器。
- 提升了代碼的可讀性和可維護(hù)性,尤其是當(dāng)依賴較多時(shí)。
缺點(diǎn):
- 需要額外的構(gòu)造函數(shù)代碼,但使用 Lombok 的
@RequiredArgsConstructor
可以減輕這個(gè)負(fù)擔(dān)。
4. 示例對(duì)比
使用 @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; // 其他代碼 }
使用構(gòu)造函數(shù)注入
@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; // 其他代碼 }
通過(guò)這種構(gòu)造函數(shù)注入的方式,不僅可以增強(qiáng)代碼的健壯性和可維護(hù)性,還可以更好地利用 Spring 的依賴注入特性和 Lombok 的簡(jiǎn)化代碼的優(yōu)勢(shì)。
好處
5.關(guān)于注解
在使用構(gòu)造函數(shù)注入時(shí),不需要額外添加注解,只需要提供構(gòu)造函數(shù)即可。Spring 會(huì)自動(dòng)檢測(cè)到你的構(gòu)造函數(shù)并進(jìn)行依賴注入。
使用構(gòu)造函數(shù)注入,不需要額外注解
@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; // 自己編寫構(gòu)造函數(shù) 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; } }
使用構(gòu)造函數(shù)注入的要點(diǎn)
1.定義構(gòu)造函數(shù):
在類中定義一個(gè)包含所有需要依賴的構(gòu)造函數(shù)。
2.Spring 自動(dòng)注入:
Spring 在創(chuàng)建 Bean 實(shí)例時(shí),會(huì)自動(dòng)識(shí)別并調(diào)用該構(gòu)造函數(shù),同時(shí)注入所需的依賴。
示例代碼
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; // 構(gòu)造函數(shù)注入 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; } // 你的控制器方法 }
在這個(gè)例子中,不需要使用任何額外的注解來(lái)標(biāo)注構(gòu)造函數(shù),Spring 會(huì)自動(dòng)識(shí)別并注入依賴。(當(dāng)然,標(biāo)注了也不會(huì)報(bào)錯(cuò),但是只能使用@Autowired,不能使用@Resouce,@Resource
注解通常用于字段或 setter 方法注入)
額外情況
多個(gè)構(gòu)造函數(shù):
- 如果一個(gè)類中有多個(gè)構(gòu)造函數(shù),并且其中只有一個(gè)構(gòu)造函數(shù)有注入?yún)?shù),Spring 會(huì)使用這個(gè)構(gòu)造函數(shù)進(jìn)行注入。
- 如果有多個(gè)構(gòu)造函數(shù)都有注入?yún)?shù),則需要使用
@Autowired
注解來(lái)明確指定使用哪個(gè)構(gòu)造函數(shù)。
示例代碼(多個(gè)構(gòu)造函數(shù))
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 明確指定使用哪個(gè)構(gòu)造函數(shù) @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; } // 另一個(gè)構(gòu)造函數(shù) public SysUserController(ISysUserService userService) { this.userService = userService; this.roleService = null; this.postService = null; this.deptService = null; this.userPostService = null; } // 你的控制器方法 }
在這個(gè)例子中,由于存在多個(gè)構(gòu)造函數(shù),需要使用 @Autowired
注解來(lái)明確指定 Spring 使用哪個(gè)構(gòu)造函數(shù)進(jìn)行依賴注入。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- spring中的特殊注解@RequiredArgsConstructor詳解
- 使用@RequiredArgsConstructor注解來(lái)取代繁瑣的@Autowrired
- 解讀@NoArgsConstructor,@AllArgsConstructor,@RequiredArgsConstructor的區(qū)別及在springboot常用地方
- Java中的@RequiredArgsConstructor注解詳解
- Java中@RequiredArgsConstructor注解的基本用法
- springboot @RequiredArgsConstructor的概念與使用方式
- Java中@RequiredArgsConstructor使用詳解
相關(guān)文章
java如何將map數(shù)據(jù)存入到實(shí)體類對(duì)象中
在Java編程中,經(jīng)常需要將Map集合中的數(shù)據(jù)轉(zhuǎn)換為實(shí)體類對(duì)象,這可以通過(guò)反射機(jī)制實(shí)現(xiàn),即通過(guò)遍歷Map對(duì)象,使用反射根據(jù)鍵名對(duì)應(yīng)實(shí)體類的屬性名,動(dòng)態(tài)調(diào)用setter方法將值設(shè)置到實(shí)體對(duì)象中,這樣的操作使得數(shù)據(jù)從Map結(jié)構(gòu)轉(zhuǎn)移到了具體的JavaBean中,便于后續(xù)的操作和管理2024-09-09Java替換int數(shù)組中重復(fù)數(shù)據(jù)的方法示例
這篇文章主要介紹了Java替換int數(shù)組中重復(fù)數(shù)據(jù)的方法,涉及java針對(duì)數(shù)組的遍歷、轉(zhuǎn)換、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06ConcurrentHashMap線程安全及實(shí)現(xiàn)原理實(shí)例解析
這篇文章主要介紹了ConcurrentHashMap線程安全及實(shí)現(xiàn)原理實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11使用SpringBoot中web項(xiàng)目推薦目錄結(jié)構(gòu)的問(wèn)題
這篇文章主要介紹了SpringBoot中web項(xiàng)目推薦目錄結(jié)構(gòu)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Spring?Boot?實(shí)現(xiàn)Redis分布式鎖原理
這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)Redis分布式鎖原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08淺談Spring Boot 2.0遷移指南主要注意點(diǎn)
Spring官方的Spring Boot 2變動(dòng)指南,主要是幫助您將應(yīng)用程序遷移到Spring Boot 2.0,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Java集成和使用dl4j實(shí)現(xiàn)通過(guò)掃描圖片識(shí)別快遞單信息
這篇文章主要為大家詳細(xì)介紹了Java如何使用DL4J搭建一個(gè)簡(jiǎn)單的圖像識(shí)別模型,并將其集成到Spring?Boot后端中從而實(shí)現(xiàn)通過(guò)掃描圖片識(shí)別快遞單信息,需要的可以參考下2024-12-12