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)文章
Java 中 synchronized的用法詳解(四種用法)
Java語言的關(guān)鍵字,當(dāng)它用來修飾一個(gè)方法或者一個(gè)代碼塊的時(shí)候,能夠保證在同一時(shí)刻最多只有一個(gè)線程執(zhí)行該段代碼。本文給大家介紹java中 synchronized的用法,對本文感興趣的朋友一起看看吧2015-11-11
Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法
本文主要介紹了 Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Springboot項(xiàng)目使用Slf4j將日志保存到本地目錄的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot項(xiàng)目使用Slf4j將日志保存到本地目錄的實(shí)現(xiàn)方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Springboot集成百度地圖實(shí)現(xiàn)定位打卡的示例代碼
本文主要介紹了Springboot集成百度地圖實(shí)現(xiàn)定位打卡的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
springboot集成activemq的實(shí)例代碼
本篇文章主要介紹了springboot集成activemq的實(shí)例代碼,詳細(xì)的介紹了ActiveMQ和Spring-Boot 集成 ActiveMQ,有興趣的可以了解下。2017-05-05

