本篇文章給大家分享的是有關怎么在Spring中注入集合值,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

目前創新互聯公司已為上1000+的企業提供了網站建設、域名、網站空間、綿陽服務器托管、企業網站設計、曾都網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
一 配置
<?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個普通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"> <!-- 為調用setSchools()方法配置List集合作為參數值 --> <list> <!-- 每個value、ref、bean...都配置一個List元素 --> <value>小學</value> <value>中學</value> <value>大學</value> </list> </property> <property name="scores"> <!-- 為調用setScores()方法配置Map集合作為參數值 --> <map> <!-- 每個entry配置一個key-value對 --> <entry key="數學" value="87"/> <entry key="英語" value="89"/> <entry key="語文" value="82"/> </map> </property> <property name="phaseAxes"> <!-- 為調用setPhaseAxes()方法配置Map集合作為參數值 --> <map> <!-- 每個entry配置一個key-value對 --> <entry key="原始社會" value-ref="stoneAxe"/> <entry key="農業社會" value-ref="steelAxe"/> </map> </property> <property name="health"> <!-- 為調用setHealth()方法配置Properties集合作為參數值 --> <props> <!-- 每個prop元素配置一個屬性項,其中key指定屬性名 --> <prop key="血壓">正常</prop> <prop key="身高">175</prop> </props> <!-- <value> pressure=normal height=175 </value> --> </property> <property name="axes"> <!-- 為調用setAxes()方法配置Set集合作為參數值 --> <set> <!-- 每個value、ref、bean..都配置一個Set元素 --> <value>普通的字符串</value> <bean class="org.crazyit.service.impl.SteelAxe"/> <ref bean="stoneAxe"/> <!-- 為Set集合配置一個List集合作為元素 --> <list> <value>20</value> <!-- 再次為List集合配置一個Set集合作為元素 --> <set> <value type="int">30</value> </set> </list> </set> </property> <property name="books"> <!-- 為調用setBooks()方法配置數組作為參數值 --> <list> <!-- 每個value、ref、bean...都配置一個數組元素 --> <value>瘋狂Java講義</value> <value>瘋狂Android講義</value> <value>輕量級Java EE企業應用實戰</value> </list> </property> </bean> </beans>
二 接口
Axe
package org.crazyit.app.service;
public interface Axe
{
// Axe接口里有個砍的方法
public String chop();
}Person
package org.crazyit.service;
public interface Person
{
public void test();
}三 實現
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實例化主調bean:Chinese實例...");
}
// 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;
}
// 訪問上面全部的集合類型的成員變量
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 "鋼斧砍柴真快";
}
}四 測試類
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,并調用方法。
Person p = ctx.getBean("chinese" , Person.class);
p.test();
}
}五 運行
Spring實例化主調bean:Chinese實例...
[小學, 中學, 大學]
{數學=87, 英語=89, 語文=82}
{原始社會=org.crazyit.service.impl.StoneAxe@6e1567f1, 農業社會=org.crazyit.service.impl.SteelAxe@56235b8e}
{血壓=正常, 身高=175}
[普通的字符串, org.crazyit.service.impl.SteelAxe@59494225, org.crazyit.service.impl.StoneAxe@6e1567f1, [20, [30]]]
[瘋狂Java講義, 瘋狂Android講義, 輕量級Java EE企業應用實戰]
以上就是怎么在Spring中注入集合值,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創新互聯行業資訊頻道。
網站標題:怎么在Spring中注入集合值
文章鏈接:http://www.yijiale78.com/article10/gsopdo.html
成都網站建設公司_創新互聯,為您提供用戶體驗、做網站、軟件開發、、品牌網站制作、網站營銷
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯