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