您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在Spring中注入集合值,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 定義2個(gè)普通Axe Bean --> <bean id="stoneAxe" class="org.crazyit.service.impl.StoneAxe"/> <bean id="steelAxe" class="org.crazyit.service.impl.SteelAxe"/> <!-- 定義chinese Bean --> <bean id="chinese" class="org.crazyit.service.impl.Chinese"> <property name="schools"> <!-- 為調(diào)用setSchools()方法配置List集合作為參數(shù)值 --> <list> <!-- 每個(gè)value、ref、bean...都配置一個(gè)List元素 --> <value>小學(xué)</value> <value>中學(xué)</value> <value>大學(xué)</value> </list> </property> <property name="scores"> <!-- 為調(diào)用setScores()方法配置Map集合作為參數(shù)值 --> <map> <!-- 每個(gè)entry配置一個(gè)key-value對(duì) --> <entry key="數(shù)學(xué)" value="87"/> <entry key="英語(yǔ)" value="89"/> <entry key="語(yǔ)文" value="82"/> </map> </property> <property name="phaseAxes"> <!-- 為調(diào)用setPhaseAxes()方法配置Map集合作為參數(shù)值 --> <map> <!-- 每個(gè)entry配置一個(gè)key-value對(duì) --> <entry key="原始社會(huì)" value-ref="stoneAxe"/> <entry key="農(nóng)業(yè)社會(huì)" value-ref="steelAxe"/> </map> </property> <property name="health"> <!-- 為調(diào)用setHealth()方法配置Properties集合作為參數(shù)值 --> <props> <!-- 每個(gè)prop元素配置一個(gè)屬性項(xiàng),其中key指定屬性名 --> <prop key="血壓">正常</prop> <prop key="身高">175</prop> </props> <!-- <value> pressure=normal height=175 </value> --> </property> <property name="axes"> <!-- 為調(diào)用setAxes()方法配置Set集合作為參數(shù)值 --> <set> <!-- 每個(gè)value、ref、bean..都配置一個(gè)Set元素 --> <value>普通的字符串</value> <bean class="org.crazyit.service.impl.SteelAxe"/> <ref bean="stoneAxe"/> <!-- 為Set集合配置一個(gè)List集合作為元素 --> <list> <value>20</value> <!-- 再次為L(zhǎng)ist集合配置一個(gè)Set集合作為元素 --> <set> <value type="int">30</value> </set> </list> </set> </property> <property name="books"> <!-- 為調(diào)用setBooks()方法配置數(shù)組作為參數(shù)值 --> <list> <!-- 每個(gè)value、ref、bean...都配置一個(gè)數(shù)組元素 --> <value>瘋狂Java講義</value> <value>瘋狂Android講義</value> <value>輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)</value> </list> </property> </bean> </beans>
二 接口
Axe
package org.crazyit.app.service; public interface Axe { // Axe接口里有個(gè)砍的方法 public String chop(); }
Person
package org.crazyit.service; public interface Person { public void test(); }
三 實(shí)現(xiàn)
Chinese
package org.crazyit.service.impl; import java.util.*; import org.crazyit.service.*; public class Chinese implements Person { // 下面是系列集合類型的成員變量 private List<String> schools; private Map scores; private Map<String , Axe> phaseAxes; private Properties health; private Set axes; private String[] books; public Chinese() { System.out.println("Spring實(shí)例化主調(diào)bean:Chinese實(shí)例..."); } // schools的setter方法 public void setSchools(List schools) { this.schools = schools; } // scores的setter方法 public void setScores(Map scores) { this.scores = scores; } // phaseAxes的setter方法 public void setPhaseAxes(Map<String , Axe> phaseAxes) { this.phaseAxes = phaseAxes; } // health的setter方法 public void setHealth(Properties health) { this.health = health; } // axes的setter方法 public void setAxes(Set axes) { this.axes = axes; } // books的setter方法 public void setBooks(String[] books) { this.books = books; } // 訪問(wèn)上面全部的集合類型的成員變量 public void test() { System.out.println(schools); System.out.println(scores); System.out.println(phaseAxes); System.out.println(health); System.out.println(axes); System.out.println(java.util.Arrays.toString(books)); } }
StoneAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class StoneAxe implements Axe { public String chop() { return "石斧砍柴好慢"; } }
SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public String chop() { return "鋼斧砍柴真快"; } }
四 測(cè)試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.service.*; public class BeanTest { public static void main(String[] args)throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 獲取容器中Bean,并調(diào)用方法。 Person p = ctx.getBean("chinese" , Person.class); p.test(); } }
五 運(yùn)行
Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...
[小學(xué), 中學(xué), 大學(xué)]
{數(shù)學(xué)=87, 英語(yǔ)=89, 語(yǔ)文=82}
{原始社會(huì)=org.crazyit.service.impl.StoneAxe@6e1567f1, 農(nóng)業(yè)社會(huì)=org.crazyit.service.impl.SteelAxe@56235b8e}
{血壓=正常, 身高=175}
[普通的字符串, org.crazyit.service.impl.SteelAxe@59494225, org.crazyit.service.impl.StoneAxe@6e1567f1, [20, [30]]]
[瘋狂Java講義, 瘋狂Android講義, 輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)]
以上就是怎么在Spring中注入集合值,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。