spring:為javabean的集合對象注入屬性值,springjavabean

來源:互聯網
上載者:User

spring:為javabean的集合對象注入屬性值,springjavabean
spring:為JavaBean的集合對象注入屬性值

 

在 spring 中可以對List、Set、Map 等集合進行配置,不過根據集合類型的不同,需要使用不同的標籤配置對應相應的集合。

1.建立 TsetUtil 類,在該類中定義List、Set、Map 類型的屬性,並設定getter 和 setter 方法。代碼如下

package com.importnew;import java.util.List;import java.util.Map;import java.util.Set;public class TestUtil {    private List list;    private Map map;    private Set set;    public List getList() {        return list;    }    public void setList(List list) {        this.list = list;    }        public Map getMap() {        return map;    }    public void setMap(Map map) {        this.map = map;    }    public Set getSet() {        return set;    }    public void setSet(Set set) {        this.set = set;    }       }

 

2.在 spring 設定檔中對 TsetUtil 進行配置,並通過<list>,<set>,<map>為 TsetUtil 的List、Set、Map集合屬性賦值。applicationContext.xml 設定檔代碼如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd">        <bean id="testUtil" class="com.importnew.TestUtil"  >         <property name="list">             <list>                 <value>list 集合的第一個元素</value>                 <value>list 集合的第二個元素</value>                 <value>list 集合的第三個元素</value>             </list>         </property>         <property name="set">             <set>                 <value>張三</value>                 <value>李四</value>             </set>         </property>         <property name="map">             <map>                 <entry key="key1" value="java從基礎到項目死戰" />                 <entry key="key2" value="java開發" />             </map>         </property>     </bean>     <bean id="user" class="com.importnew.User"></bean></beans>

 

3.編寫測試類別 TestSpring ,代碼如下:

package test;import java.util.List;import java.util.Map;import java.util.Set;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.importnew.TestUtil;public class TestSpring {    public static void main(String[] args) {             ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        TestUtil testUtil = (TestUtil) context.getBean("testUtil");                List lists = testUtil.getList();        for(Object ss:lists){            System.out.println(ss.toString());        }                Set sets = testUtil.getSet();        for(Object ss:sets){            System.out.println(ss.toString());        }                Map<String,String> maps = testUtil.getMap();for (Map.Entry<String, String> entry : maps.entrySet()) {               System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());              }     }}

 

———————————————————————————————————————————————————

備忘:向集合中添加物件類型的元素時,<list>,<set>,<map>不僅可以添加 Stirng 類型的元素,而且可以添加物件類型的元素。如下代碼實現了向集合中添加了對象user:

<bean id="testUtil" class="com.importnew.TestUtil"  >         <property name="list">             <list>                 <value>list 集合的第一個元素</value>                 <value>list 集合的第二個元素</value>                 <value>list 集合的第三個元素</value>                 <ref bean="user"/>             </list>         </property>         <property name="set">             <set>                 <value>張三</value>                 <value>李四</value>                 <ref bean="user"/>             </set>         </property>         <property name="map">             <map>                 <entry key="key1" value="java從基礎到項目死戰" />                 <entry key="key2" value="java開發" />                 <entry key="user">                     <ref bean="user"/>                 </entry>             </map>         </property>     </bean>     <bean id="user" class="com.importnew.User"></bean>    

 

////end

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.