Spring使用Setter完成依賴注入方式
對依賴注入的理解
依賴:實體間的所有依賴由容器創(chuàng)建
注入:容器負責完成實體間依賴互相注入的任務(wù)
使用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 + '\'' +
'}';
}
}
實體類引用的復(fù)雜類型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" />
復(fù)雜VO類型注入
配置文件中增加復(fù)雜類型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"/>
數(shù)組類型注入
<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>英雄聯(lián)盟</value>
<value>穿越火線</value>
<value>刺激戰(zhàn)場</value>
</set>
</property>
Map鍵值對類型注入
<property name="card">
<map>
<entry key="學(xué)生卡" 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" />
<!--復(fù)雜VO類型注入-->
<property name="address" ref="address"/>
<!--數(shù)組類型注入-->
<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>英雄聯(lián)盟</value>
<value>穿越火線</value>
<value>刺激戰(zhàn)場</value>
</set>
</property>
<!--Map鍵值對類型注入-->
<property name="card">
<map>
<entry key="學(xué)生卡" 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());
}
}
輸出結(jié)果:

Spring解決setter方式的循環(huán)依賴的原理
1.通過構(gòu)造函數(shù)創(chuàng)建A對象 (A對象是半成品,還沒有注入屬性和調(diào)用init方法)
2.將半成品A對象封裝成工廠對象存入三級緩存
3.A對象需要注入B對象,發(fā)現(xiàn)緩存里還沒有B對象,開始創(chuàng)建B對象
4.通過構(gòu)造函數(shù)創(chuàng)建B對象(B對象是半成品,還沒有注入屬性和調(diào)用init方法)同樣在三級緩存中創(chuàng)建B工廠對象
5.B對象需要注入A對象;從三級緩存中獲取A工廠對象,使用工廠對象獲取半成品A對象同時放入
二級緩存中,提前曝光A對象,同時刪除A工廠對象
6.B對象繼續(xù)注入其它屬性和初始化,之后將完成品B對象放入完成品緩存一級緩存,同時刪除B工廠對象
7.A對象獲取單例B的引用完成屬性注入
8.B對象繼續(xù)注入其它屬性和初始化,之后將完成品A對象放入完成品緩存一級緩存同時刪除二級緩存中的A
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中的@ConfigurationProperties注解的使用
本文將深入探討@ConfigurationProperties注解的概念、用法、工作原理、配置綁定、類型安全以及如何在實際開發(fā)中應(yīng)用它,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
Maven安裝本地的jar包和創(chuàng)建帶模板的自定義項目的操作過程
這篇文章主要介紹了Maven安裝本地的jar包和創(chuàng)建帶模板的自定義項目,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03
Springboot事件監(jiān)聽與@Async注解詳解
這篇文章主要介紹了Springboot事件監(jiān)聽與@Async注解詳解,在開發(fā)中經(jīng)常可以利用Spring事件監(jiān)聽來實現(xiàn)觀察者模式,進行一些非事務(wù)性的操作,如記錄日志之類的,需要的朋友可以參考下2024-01-01
java實現(xiàn)String字符串處理各種類型轉(zhuǎn)換
在日常的程序開發(fā)中,經(jīng)常會涉及到不同類型之間的轉(zhuǎn)換,本文主要介紹了String字符串處理各種類型轉(zhuǎn)換,具有一定的參考價值,感興趣的可以了解一下2023-10-10
JAVA 數(shù)據(jù)結(jié)構(gòu)之Queue處理實例代碼
這篇文章主要介紹了JAVA 數(shù)據(jù)結(jié)構(gòu)之Queue處理實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02

