1. 程式人生 > 程式設計 >Spring實戰之注入集合值操作示例

Spring實戰之注入集合值操作示例

本文例項講述了Spring實戰之注入集合值操作。分享給大家供大家參考,具體如下:

一 配置

<?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企業應用實戰]

更多關於java相關內容感興趣的讀者可檢視本站專題:《Spring框架入門與進階教程》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》、《Java檔案與目錄操作技巧彙總》和《Java快取操作技巧彙總》

希望本文所述對大家java程式設計有所幫助。