今天就跟大家聊聊有關Spring中怎么注入集合值,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。
一 配置
<?xml version="1.0" encoding="GBK"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="/tupian/20230522/" xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//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集合作為參數(shù)值 --> <list> <!-- 每個value、ref、bean...都配置一個List元素 --> <value>小學</value> <value>中學</value> <value>大學</value> </list> </property> <property name="scores"> <!-- 為調用setScores()方法配置Map集合作為參數(shù)值 --> <map> <!-- 每個entry配置一個key-value對 --> <entry key="數(shù)學" value="87"/> <entry key="英語" value="89"/> <entry key="語文" value="82"/> </map> </property> <property name="phaseAxes"> <!-- 為調用setPhaseAxes()方法配置Map集合作為參數(shù)值 --> <map> <!-- 每個entry配置一個key-value對 --> <entry key="原始社會" value-ref="stoneAxe"/> <entry key="農(nóng)業(yè)社會" value-ref="steelAxe"/> </map> </property> <property name="health"> <!-- 為調用setHealth()方法配置Properties集合作為參數(shù)值 --> <props> <!-- 每個prop元素配置一個屬性項,其中key指定屬性名 --> <prop key="血壓">正常</prop> <prop key="身高">175</prop> </props> <!-- <value> pressure=normal height=175 </value> --> </property> <property name="axes"> <!-- 為調用setAxes()方法配置Set集合作為參數(shù)值 --> <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()方法配置數(shù)組作為參數(shù)值 --> <list> <!-- 每個value、ref、bean...都配置一個數(shù)組元素 --> <value>瘋狂Java講義</value> <value>瘋狂Android講義</value> <value>輕量級Java EE企業(yè)應用實戰(zhàn)</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();}
三 實現(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實例化主調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實例...[小學, 中學, 大學]{數(shù)學=87, 英語=89, 語文=82}{原始社會=org.crazyit.service.impl.StoneAxe@6e1567f1, 農(nóng)業(yè)社會=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企業(yè)應用實戰(zhàn)]
看完上述內容,你們對Spring中怎么注入集合值有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當前文章:Spring中怎么注入集合值-創(chuàng)新互聯(lián)
文章出自:http://aaarwkj.com/article20/dpsgco.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、手機網(wǎng)站建設、網(wǎng)站維護、服務器托管、微信公眾號、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)