Java之Spring簡單的讀取和存儲對象
Spring 簡單的讀取和存儲對象
獲取 Bean 對象 (對象裝配)
獲取 bean 對象也叫做對象裝配,是把對象取出來放到某個類中,有時候也叫對象注?。
對象裝配(對象注入)的實現(xiàn)方法以下 3 種:
- 屬性注入
- 構(gòu)造方法注入
- Setter 注入
我們先創(chuàng)建如下幾個包和幾個類:
屬性注入
屬性注?是使? @Autowired
實現(xiàn)的,將 Service 類注?到 Controller
類中.
@Controller public class StudentController { // 1.使用屬性注入的方式獲取 Bean @Autowired private StudentService studentService; public void sayHi() { // 調(diào)用 service 方法 studentService.sayHi(); } }
優(yōu)點:
實現(xiàn)簡單, 使用簡單.
缺點:
- 功能性問題: 不能注入不可變 (
final
) 對象.
在 Java 中 final 對象(不可變)要么直接賦值,要么在構(gòu)造方法中賦值,所以當使用屬性注入 final 對象時,它不符合 Java 中 final 的使用規(guī)范,所以就不能注入成功了。
- 通用性問題: 只能適應(yīng)于
IoC
容器. - 設(shè)計原則問題: 更容易違背單一設(shè)計原則. (針對對象是類)
單一設(shè)計原則:
- 針對于類級別
- 針對于方法級別
構(gòu)造方法注入
從 Spring 4.x
之后, Spring 官方推薦使用構(gòu)造方法注入的形式.
@Controller public class StudentController { // 2.構(gòu)造方法注入 private StudentService studentService; // @Autowired 可省略 @Autowired public StudentController(StudentService studentService) { this.studentService = studentService; } public void sayHi() { // 調(diào)用 service 方法 studentService.sayHi(); } }
# 注意 #
@Autowired
可省略.- 但是如果類中有多個構(gòu)造?法,那么需要添加
@Autowired
來明確指定到底使?哪個構(gòu)造?法,否則程序會報錯.
優(yōu)點:
- 可注入不可變對象.
- 注入對象不會被修改.
- 加了
final
修飾符. - 構(gòu)造方法是隨著類加載只執(zhí)行一次的.
- 加了
- 注入對象會被完全初始化. (使用構(gòu)造方法帶來的優(yōu)點)
- 通用性更好.
缺點:
沒有屬性注入實現(xiàn)簡單.
Setter 注入
Setter
注?和屬性的 Setter ?法實現(xiàn)類似,只不過在設(shè)置 set ?法的時候需要加上 @Autowired
注解.
@Controller public class StudentController { // 3.setter 注入 private StudentService studentService; @Autowired public void setStudentService(StudentService studentService) { this.studentService = studentService; } public void sayHi() { // 調(diào)用 service 方法 studentService.sayHi(); } }
優(yōu)點:
更加符合單一設(shè)計原則. (針對對象是方法級別)
缺點:
- 不能注入不可變對象 (
final
修飾的對象). - 注入的對象可被修改.
set
方法是普通 set 方法, 可以被重復(fù)調(diào)用, 有被修改的風(fēng)險.
小結(jié): 日常開發(fā)當中, 使用屬性注入實現(xiàn)更簡單的讀取 Bean, 依然是主流的實現(xiàn)方式.
@Resource 關(guān)鍵字
在進?類注?時,除了可以使? @Autowired 關(guān)鍵字之外,我們還可以使? @Resource 進?注?.
@Controller public class StudentController { @Resource private StudentService studentService; public void sayHi() { // 調(diào)用 service 方法 studentService.sayHi(); } }
@Autowired 和 @Resource 的區(qū)別
相同點: 都是用來實現(xiàn)依賴注入的.
不同點:
- 功能支持不同: @Autowired 支持屬性注入, setter 注入, 構(gòu)造方法注入; @Resource 只支持屬性注入和 setter 注入, 不支持構(gòu)造方法注入.
- 出身不同: @Autowired 來自于 Spring 框架; 而 @Resource 來自于 JDK.
- 參數(shù)支持不同: @Resource 支持更多的參數(shù)設(shè)置; 而 @Autowired 只支持 required 參數(shù).
同?類型多個 @Bean 報錯處理
當出現(xiàn)以下多個 Bean,返回同?對象類型時程序會報錯
此時我們運行:
public class App { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml"); StudentController studentController = applicationContext.getBean("studentController", StudentController.class); studentController.func(); } }
# 注意 # 會報錯, 報錯的原因是,非唯一的 Bean 對象。
同?類型多個 Bean 報錯處理
解決同?個類型,多個 Bean 的解決?案有以下兩個:
- 使?
@Resource(name="student1")
定義. - 使?
@Qualifier
注解定義名稱.
#
使? @Resource(name="student1")
定義.
@Controller public class StudentController { @Resource(name = "student2") private Student student; public void func() { System.out.println(student.toString()); } }
#
使? @Qualifier
注解定義名稱.
@Controller public class StudentController { @Resource @Qualifier("student2") private Student student; public void func() { System.out.println(student.toString()); } }
#
如果我們想用 @Autowired
可以寫成:
@Autowired private Student student1; // 存在有耦合性問題
以上就是今天要講的內(nèi)容了,希望對大家有所幫助,如果有問題歡迎評論指出,會積極改正?。?/p>
到此這篇關(guān)于Java之Spring簡單的讀取和存儲對象的文章就介紹到這了,更多相關(guān)Java Spring讀取和存儲對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot獲取配置文件內(nèi)容的幾種方式總結(jié)
大家都知道SpringBoot獲取配置文件的方法有很多,下面這篇文章主要給大家介紹了關(guān)于SpringBoot獲取配置文件內(nèi)容的幾種方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-02-02基于spring?@Cacheable?注解的spel表達式解析執(zhí)行邏輯
這篇文章主要介紹了spring?@Cacheable?注解的spel表達式解析執(zhí)行邏輯,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01SpringBoot實現(xiàn)使用反射模擬IOC和getBean
這篇文章主要介紹了SpringBoot實現(xiàn)使用反射模擬IOC和getBean,IOC就是spring的核心思想之一——控制反轉(zhuǎn)。這里不再贅述,看此文章即可了解2023-04-04