Spring使用Setter完成依賴注入方式
對(duì)依賴注入的理解
依賴:實(shí)體間的所有依賴由容器創(chuàng)建
注入:容器負(fù)責(zé)完成實(shí)體間依賴互相注入的任務(wù)
使用Setter完成不同類型屬性的注入
實(shí)體類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 + '\'' + '}'; } }
實(shí)體類引用的復(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">
實(shí)體類Student的bean屬性依賴對(duì)其進(jìn)行引用
<property name="address" ref="address"/>
數(shù)組類型注入
<property name="books"> <array> <value>西游記</value> <value>三國(guó)演義</value> <value>紅樓夢(mèng)</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)場(chǎng)</value> </set> </property>
Map鍵值對(duì)類型注入
<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>三國(guó)演義</value> <value>紅樓夢(mèng)</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)場(chǎng)</value> </set> </property> <!--Map鍵值對(duì)類型注入--> <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>
測(cè)試類
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對(duì)象 (A對(duì)象是半成品,還沒有注入屬性和調(diào)用init方法)
2.將半成品A對(duì)象封裝成工廠對(duì)象存入三級(jí)緩存
3.A對(duì)象需要注入B對(duì)象,發(fā)現(xiàn)緩存里還沒有B對(duì)象,開始創(chuàng)建B對(duì)象
4.通過構(gòu)造函數(shù)創(chuàng)建B對(duì)象(B對(duì)象是半成品,還沒有注入屬性和調(diào)用init方法)同樣在三級(jí)緩存中創(chuàng)建B工廠對(duì)象
5.B對(duì)象需要注入A對(duì)象;從三級(jí)緩存中獲取A工廠對(duì)象,使用工廠對(duì)象獲取半成品A對(duì)象同時(shí)放入
二級(jí)緩存中,提前曝光A對(duì)象,同時(shí)刪除A工廠對(duì)象
6.B對(duì)象繼續(xù)注入其它屬性和初始化,之后將完成品B對(duì)象放入完成品緩存一級(jí)緩存,同時(shí)刪除B工廠對(duì)象
7.A對(duì)象獲取單例B的引用完成屬性注入
8.B對(duì)象繼續(xù)注入其它屬性和初始化,之后將完成品A對(duì)象放入完成品緩存一級(jí)緩存同時(shí)刪除二級(jí)緩存中的A
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 8 lambda表達(dá)式引入詳解及實(shí)例
這篇文章主要介紹了Java 8 lambda表達(dá)式引入詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05配置了jdk的環(huán)境idea卻提示找不到j(luò)dk解決辦法
在使用Java編程語言進(jìn)行開發(fā)時(shí),IDEA是一個(gè)非常流行和強(qiáng)大的集成開發(fā)環(huán)境,這篇文章主要給大家介紹了關(guān)于配置了jdk的環(huán)境idea卻提示找不到j(luò)dk的解決辦法,需要的朋友可以參考下2023-12-12SSH框架網(wǎng)上商城項(xiàng)目第22戰(zhàn)之銀行圖標(biāo)以及支付頁面顯示
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第22戰(zhàn)之銀行圖標(biāo)以及支付頁面顯示,感興趣的小伙伴們可以參考一下2016-06-06request.getParameter()方法的簡(jiǎn)單理解與運(yùn)用方式
在JavaWeb開發(fā)中,request對(duì)象扮演著至關(guān)重要的角色,它是HTTP請(qǐng)求的封裝,request.getParameter()用于獲取客戶端通過GET或POST方式發(fā)送的參數(shù),與之相對(duì),request.setAttribute()用于在服務(wù)器端設(shè)置屬性,這些屬性只在一次請(qǐng)求中有效2024-10-10SpringBoot輕松實(shí)現(xiàn)ip解析(含源碼)
IP地址一般以數(shù)字形式表示,如192.168.0.1,IP解析是將這個(gè)數(shù)字IP轉(zhuǎn)換為包含地區(qū)、城市、運(yùn)營(yíng)商等信息的字符串形式,如“廣東省深圳市 電信”,這樣更方便人理解和使用,本文給大家介紹了SpringBoot如何輕松實(shí)現(xiàn)ip解析,需要的朋友可以參考下2023-10-10