詳解SpringBean基于XML的裝配
1.設(shè)值注入:通過反射調(diào)用setXxx注入屬性值
package com.itheima.assemble; import java.util.List; public class User { private String username; private Integer password; private List<String> list; /** * 設(shè)值注入 * 提供默認(rèn)空參構(gòu)造方法 ; * 為所有屬性提供setter方法。 */ public User() { super(); } public void setUsername(String username) { this.username = username; } public void setPassword(Integer password) { this.password = password; } public void setList(List<String> list) { this.list = list; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", list=" + list + "]"; } }
package com.itheima.assemble; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class XmlBeanAssembleTest { public static void main(String[] args) { String xmlPath = "com/itheima/assemble/beans5.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); // 構(gòu)造方式輸出結(jié)果 System.out.println(applicationContext.getBean("user2")); } }
<?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-4.3.xsd"> <!--2.使用設(shè)值注入方式裝配User實(shí)例 --> <bean id="user2" class="com.itheima.assemble.User"> <property name="username" value="張三"></property> <property name="password" value="654321"></property> <!-- 注入list集合 --> <property name="list"> <list> <value>"值1"</value> <value>"值2"</value> </list> </property> </bean> </beans>
2.構(gòu)造注入:用+其value屬性注入屬性值
package com.itheima.assemble; import java.util.List; public class User { private String username; private Integer password; private List<String> list; /** * 用構(gòu)造注入 * 創(chuàng)建帶所有參數(shù)的有參構(gòu)造方法。 */ public User(String username, Integer password, List<String> list) { super(); this.username = username; this.password = password; this.list = list; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", list=" + list + "]"; } }
package com.itheima.assemble; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class XmlBeanAssembleTest { public static void main(String[] args) { String xmlPath = "com/itheima/assemble/beans5.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); // 構(gòu)造方式輸出結(jié)果 System.out.println(applicationContext.getBean("user1")); } }
<?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-4.3.xsd"> <!--1.使用構(gòu)造注入方式裝配User實(shí)例user1,裝配后user1則內(nèi)含多個注入數(shù)據(jù)的屬性 --> <bean id="user1" class="com.itheima.assemble.User"> <constructor-arg index="0" value="tom" /><!-- 屬性1,即username --> <constructor-arg index="1" value="123456" /> <!-- 屬性2,即password --> <constructor-arg index="2"> <!-- 屬性3 --> <list> <value>"值1"</value> <value>"值2"</value> </list> </constructor-arg> </bean> </beans>
到此這篇關(guān)于詳解SpringBean基于XML的裝配的文章就介紹到這了,更多相關(guān)SpringBean裝配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)輕松處理日期和時間的API小結(jié)
這篇文章主要為大家詳細(xì)介紹了Java中的日期和時間API,可以輕松處理日期和時間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03java如何對map進(jìn)行排序詳解(map集合的使用)
這篇文章主要介紹了java如何對map進(jìn)行排序,java map集合的使用詳解,大家可以參考使用2013-12-12Opencv創(chuàng)建車牌圖片識別系統(tǒng)方法詳解
本文主要介紹了一個基于spring?boot+maven+opencv實(shí)現(xiàn)的圖像識別及訓(xùn)練項(xiàng)目,可以實(shí)現(xiàn)車牌識別功能,感興趣的可以跟隨小編一起試一試2022-01-01Spring?Security?自定義授權(quán)服務(wù)器實(shí)踐記錄
授權(quán)服務(wù)器(Authorization Server)目前并沒有集成在Spring Security項(xiàng)目中,而是作為獨(dú)立項(xiàng)目存在于Spring生態(tài)中,這篇文章主要介紹了Spring?Security?自定義授權(quán)服務(wù)器實(shí)踐,需要的朋友可以參考下2022-08-08Java建造者模式構(gòu)建復(fù)雜對象的最佳實(shí)踐
建造者模式,是一種對象構(gòu)建模式?它可以將復(fù)雜對象的建造過程抽象出來,使這個抽象過程的不同實(shí)現(xiàn)方法可以構(gòu)造出不同表現(xiàn)的對象。本文將通過示例講解建造者模式,需要的可以參考一下2023-04-04java.lang.NumberFormatException異常解決方案詳解
這篇文章主要介紹了java.lang.NumberFormatException異常解決方案詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08RestTemplate發(fā)送get和post請求,下載文件的實(shí)例
這篇文章主要介紹了RestTemplate發(fā)送get和post請求,下載文件的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09