Spring?DI依賴注入過程解析
依賴簡介
一個(gè)典型的企業(yè)應(yīng)用程序不是由一個(gè)單一的對象組成(或Spring的說法中的bean)。即使是最簡單的應(yīng)用程序也只有幾個(gè)對象一起工作來呈現(xiàn)最終用戶看作是一個(gè)連貫的應(yīng)用程序。如何從定義許多獨(dú)立的bean定義到完全實(shí)現(xiàn)的應(yīng)用程序,在這些應(yīng)用程序中對象協(xié)作實(shí)現(xiàn)目標(biāo)。
依賴注入
依賴注入(DI)是一個(gè)過程,通過這個(gè)過程,對象可以通過構(gòu)造函數(shù)參數(shù),工廠方法的參數(shù)或者在構(gòu)造或返回對象實(shí)例后設(shè)置的屬性來定義它們的依賴關(guān)系從工廠方法。然后容器在創(chuàng)建bean時(shí)注入這些依賴關(guān)系。這個(gè)過程從根本上說是相反的,因此名為控制反轉(zhuǎn)(IoC),它本身通過使用類的直接構(gòu)造或服務(wù)定位符模式來控制它自己的依賴關(guān)系的實(shí)例化或位置。
代碼與DI原則相比更加清晰,當(dāng)對象提供依賴時(shí),解耦更為有效。該對象不查找它的依賴關(guān)系,不知道依賴關(guān)系的位置或類。因此,您的類變得更容易測試,特別是當(dāng)依賴關(guān)系在接口或抽象基類上時(shí),它們允許在單元測試中使用存根或模擬實(shí)現(xiàn)。
DI存在兩種主要的變體,基于構(gòu)造函數(shù)的依賴注入和基于Setter的依賴注入
Spring DI依賴注入詳解
pojo類:
public class Student { private String name; private Hello hello; private String[] books; private List<String> hobbys; private Map<String, String> games; private String wife; private Properties info; public Student() { } public Student(String name, String wife) { this.name = name; this.wife = wife; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Hello getHello() { return hello; } public void setHello(Hello hello) { this.hello = hello; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbys() { return hobbys; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public Map<String, String> getGames() { return games; } public void setGames(Map<String, String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", hello=" + hello + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
注入普通的String屬性:
<bean id="student" class="top.imustctf.pojo.Student"> <property name="name" value="dahe"/> </bean>
bean注入,適用于其他的實(shí)體類:
<bean id="student" class="top.imustctf.pojo.Student"> <property name="hello" ref="hello"/> </bean>
數(shù)組注入:
<bean id="student" class="top.imustctf.pojo.Student"> <property name="books"> <array> <value>C語言入門到精通</value> <value>Spring底層原理</value> </array> </property> </bean>
List注入:
<bean id="student" class="top.imustctf.pojo.Student"> <property name="hobbys"> <list> <value>編程</value> <value>美女</value> </list> </property> </bean>
Map注入:
<bean id="student" class="top.imustctf.pojo.Student"> <property name="games"> <map> <entry key="王者榮耀" value="30級"/> <entry key="我的世界" value="100級"/> </map> </property> </bean>
空值注入:
<bean id="student" class="top.imustctf.pojo.Student"> <property name="wife"> <null/> </property> </bean>
Properties注入:
<bean id="student" class="top.imustctf.pojo.Student"> <property name="info"> <props> <prop key="學(xué)號">202099166</prop> <prop key="專業(yè)">軟件工程</prop> </props> </property> </bean>
p命名空間注入:
要使用p命名空間,你需要在beans配置頭加入如下語句:
xmlns:p="http://www.springframework.org/schema/p"
例如:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
隨后,就可以使用p方式進(jìn)行注入:
<bean id="student" class="top.imustctf.pojo.Student" p:name="dahe" p:wife="xiaoqian"/>
c命名空間注入:
要使用c命名空間,你需要在beans配置頭加入如下語句:
xmlns:c="http://www.springframework.org/schema/c"
隨后,就可以使用c方式進(jìn)行注入:(c命名空間是通過構(gòu)造器進(jìn)行注入,這就需要pojo類必須存在一個(gè)有參的構(gòu)造方法)
<bean id="student" class="top.imustctf.pojo.Student" c:name="dahe" c:wife="xiaoqian"/>
到此這篇關(guān)于Spring DI依賴注入詳解的文章就介紹到這了,更多相關(guān)Spring DI依賴注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)RabbitMQ監(jiān)聽消息的四種方式
本文介紹了在Spring Boot中實(shí)現(xiàn)RabbitMQ監(jiān)聽消息的幾種方式,包括使用@RabbitListener注解、MessageListenerAdapter、配置連接工廠和隊(duì)列等方式,感興趣的可以了解一下2024-07-07Spring Boot Maven Plugin打包異常解決方案
這篇文章主要介紹了Spring Boot Maven Plugin打包異常解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Java 普通代碼塊靜態(tài)代碼塊執(zhí)行順序(實(shí)例講解)
下面小編就為大家?guī)硪黄狫ava 普通代碼塊靜態(tài)代碼塊執(zhí)行順序(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Activiti工作流學(xué)習(xí)筆記之自動(dòng)生成28張數(shù)據(jù)庫表的底層原理解析
這篇文章主要介紹了Activiti工作流學(xué)習(xí)筆記之自動(dòng)生成28張數(shù)據(jù)庫表的底層原理解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(15)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07spring boot+自定義 AOP 實(shí)現(xiàn)全局校驗(yàn)的實(shí)例代碼
最近公司重構(gòu)項(xiàng)目,重構(gòu)為最熱的微服務(wù)框架 spring boot, 重構(gòu)的時(shí)候遇到幾個(gè)可以統(tǒng)一處理的問題。這篇文章主要介紹了spring boot+自定義 AOP 實(shí)現(xiàn)全局校驗(yàn) ,需要的朋友可以參考下2019-04-04