對依賴注入的理解
依賴:實體間的所有依賴由容器創建
注入:容器負責完成實體間依賴互相注入的任務
使用Setter完成不同類型屬性的注入
實體類Student
package indi.stitch.pojo; import java.util.*; public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Set<String> games; private Map<String, String> card; private Properties info; private String wife; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } 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 Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } 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 + '\'' + "\n" + ", address=" + address.toString() + "\n" + ", books=" + Arrays.toString(books) + "\n" + ", hobbys=" + hobbys + "\n" + ", games=" + games + "\n" + ", card=" + card + "\n" + ", info=" + info + "\n" + ", wife='" + wife + '\'' + '}'; } }
實體類引用的復雜類型Address
package indi.stitch.pojo; public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
String字符串類型注入
<property name="name" value = "stitch" />
復雜VO類型注入
配置文件中增加復雜類型bean(Address)的依賴配置
<bean id = "address" class="indi.stitch.pojo.Address"> <property name="address" value="北京" /> </bean> <bean id = "student" class = "indi.stitch.pojo.Student">
實體類Student的bean屬性依賴對其進行引用
<property name="address" ref="address"/>
數組類型注入
<property name="books"> <array> <value>西游記</value> <value>三國演義</value> <value>紅樓夢</value> <value>水滸傳</value> </array> </property>
List集合類型注入
<property name="hobbys"> <list> <value>唱歌</value> <value>跳舞</value> <value>打籃球</value> </list> </property>
Set集合類型注入
<property name="games"> <set> <value>英雄聯盟</value> <value>穿越火線</value> <value>刺激戰場</value> </set> </property>
Map鍵值對類型注入
<property name="card"> <map> <entry key="學生卡" value="123456"/> <entry key="身份證" value="111111222222223333" /> </map> </property>
Properties類型注入
<property name="info"> <props> <prop key="sex">男</prop> <prop key="age">18</prop> </props> </property>
null類型注入
<property name="wife"> <null /> </property>
整體配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "address" class="indi.stitch.pojo.Address"> <property name="address" value="北京" /> </bean> <bean id = "student" class = "indi.stitch.pojo.Student"> <!-- String字符串類型注入--> <property name="name" value = "stitch" /> <!--復雜VO類型注入--> <property name="address" ref="address"/> <!--數組類型注入--> <property name="books"> <array> <value>西游記</value> <value>三國演義</value> <value>紅樓夢</value> <value>水滸傳</value> </array> </property> <!--List集合類型注入--> <property name="hobbys"> <list> <value>唱歌</value> <value>跳舞</value> <value>打籃球</value> </list> </property> <!--Set集合類型注入--> <property name="games"> <set> <value>英雄聯盟</value> <value>穿越火線</value> <value>刺激戰場</value> </set> </property> <!--Map鍵值對類型注入--> <property name="card"> <map> <entry key="學生卡" value="123456"/> <entry key="身份證" value="111111222222223333" /> </map> </property> <!--Properties類型注入--> <property name="info"> <props> <prop key="sex">男</prop> <prop key="age">18</prop> </props> </property> <!--null類型注入--> <property name="wife"> <null /> </property> </bean> </beans>
測試類
import indi.stitch.pojo.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.toString()); } }
輸出結果:
Spring解決setter方式的循環依賴的原理
1.通過構造函數創建A對象 (A對象是半成品,還沒有注入屬性和調用init方法)
2.將半成品A對象封裝成工廠對象存入三級緩存
3.A對象需要注入B對象,發現緩存里還沒有B對象,開始創建B對象
4.通過構造函數創建B對象(B對象是半成品,還沒有注入屬性和調用init方法)同樣在三級緩存中創建B工廠對象
5.B對象需要注入A對象;從三級緩存中獲取A工廠對象,使用工廠對象獲取半成品A對象同時放入
二級緩存中,提前曝光A對象,同時刪除A工廠對象
6.B對象繼續注入其它屬性和初始化,之后將完成品B對象放入完成品緩存一級緩存,同時刪除B工廠對象
7.A對象獲取單例B的引用完成屬性注入
8.B對象繼續注入其它屬性和初始化,之后將完成品A對象放入完成品緩存一級緩存同時刪除二級緩存中的A
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_39209361/article/details/114047114