- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 詳解SpringBean基于XML的裝配
1.設值注入:通過(guò)反射調用setXxx注入屬性值
package com.itheima.assemble; import java.util.List; public class User { private String username; private Integer password; private List<String> list; /** * 設值注入 * 提供默認空參構造方法 ; * 為所有屬性提供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); // 構造方式輸出結果 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.使用設值注入方式裝配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.構造注入:用+其value屬性注入屬性值
package com.itheima.assemble; import java.util.List; public class User { private String username; private Integer password; private List<String> list; /** * 用構造注入 * 創(chuàng )建帶所有參數的有參構造方法。 */ 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); // 構造方式輸出結果 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.使用構造注入方式裝配User實(shí)例user1,裝配后user1則內含多個(gè)注入數據的屬性 --> <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裝配內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站