Spring?DI依賴注入過程解析
依賴簡介
一個典型的企業(yè)應用程序不是由一個單一的對象組成(或Spring的說法中的bean)。即使是最簡單的應用程序也只有幾個對象一起工作來呈現(xiàn)最終用戶看作是一個連貫的應用程序。如何從定義許多獨立的bean定義到完全實現(xiàn)的應用程序,在這些應用程序中對象協(xié)作實現(xiàn)目標。
依賴注入
依賴注入(DI)是一個過程,通過這個過程,對象可以通過構(gòu)造函數(shù)參數(shù),工廠方法的參數(shù)或者在構(gòu)造或返回對象實例后設置的屬性來定義它們的依賴關(guān)系從工廠方法。然后容器在創(chuàng)建bean時注入這些依賴關(guān)系。這個過程從根本上說是相反的,因此名為控制反轉(zhuǎn)(IoC),它本身通過使用類的直接構(gòu)造或服務定位符模式來控制它自己的依賴關(guān)系的實例化或位置。
代碼與DI原則相比更加清晰,當對象提供依賴時,解耦更為有效。該對象不查找它的依賴關(guān)系,不知道依賴關(guān)系的位置或類。因此,您的類變得更容易測試,特別是當依賴關(guān)系在接口或抽象基類上時,它們允許在單元測試中使用存根或模擬實現(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注入,適用于其他的實體類:
<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="學號">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方式進行注入:
<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方式進行注入:(c命名空間是通過構(gòu)造器進行注入,這就需要pojo類必須存在一個有參的構(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實現(xiàn)RabbitMQ監(jiān)聽消息的四種方式
本文介紹了在Spring Boot中實現(xiàn)RabbitMQ監(jiān)聽消息的幾種方式,包括使用@RabbitListener注解、MessageListenerAdapter、配置連接工廠和隊列等方式,感興趣的可以了解一下2024-07-07Spring Boot Maven Plugin打包異常解決方案
這篇文章主要介紹了Spring Boot Maven Plugin打包異常解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11Java 普通代碼塊靜態(tài)代碼塊執(zhí)行順序(實例講解)
下面小編就為大家?guī)硪黄狫ava 普通代碼塊靜態(tài)代碼塊執(zhí)行順序(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Activiti工作流學習筆記之自動生成28張數(shù)據(jù)庫表的底層原理解析
這篇文章主要介紹了Activiti工作流學習筆記之自動生成28張數(shù)據(jù)庫表的底層原理解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03spring boot+自定義 AOP 實現(xiàn)全局校驗的實例代碼
最近公司重構(gòu)項目,重構(gòu)為最熱的微服務框架 spring boot, 重構(gòu)的時候遇到幾個可以統(tǒng)一處理的問題。這篇文章主要介紹了spring boot+自定義 AOP 實現(xiàn)全局校驗 ,需要的朋友可以參考下2019-04-04