Spring中怎么注入集合值-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)Spring中怎么注入集合值,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)提供從項(xiàng)目策劃、軟件開發(fā),軟件安全維護(hù)、網(wǎng)站優(yōu)化(SEO)、網(wǎng)站分析、效果評估等整套的建站服務(wù),主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站制作,成都App制作以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。成都創(chuàng)新互聯(lián)公司深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

一 配置

<?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個(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對 -->           <entry key="數(shù)學(xué)" value="87"/>           <entry key="英語" value="89"/>           <entry key="語文" value="82"/>        </map>      </property>      <property name="phaseAxes">        <!-- 為調(diào)用setPhaseAxes()方法配置Map集合作為參數(shù)值 -->        <map>           <!-- 每個(gè)entry配置一個(gè)key-value對 -->           <entry key="原始社會" value-ref="stoneAxe"/>           <entry key="農(nóng)業(yè)社會" 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>              <!-- 再次為List集合配置一個(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>輕量級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;   }   // 訪問上面全部的集合類型的成員變量   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,并調(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, 英語=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è)應(yīng)用實(shí)戰(zhàn)]

看完上述內(nèi)容,你們對Spring中怎么注入集合值有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前文章:Spring中怎么注入集合值-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article20/dpsgco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、服務(wù)器托管、微信公眾號、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化