首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Spring 入門(五):集合屬性注入全攻略——List、Set、Map、Props 遍歷實戰

     編輯:Java 相關知識

Spring 支持的集合類型

  • <list> / <array>:有序、可重複 → 注入 List 或數組
  • <set>:無序、不重複 → 注入 Set
  • <map>:鍵值對 → 注入 Map
  • <props>:String 鍵值對 → 注入 Properties

完整配置示例

<bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean" scope="singleton">
    <!-- Set 注入 -->
    <property name="sets">
        <set>
            <value>岑紅軍</value>
            <value>軍哥</value>
            <value>哈哈</value>
            <value>呵呵</value>
            <value>嘿嘿</value>
            <value>洗洗</value>
        </set>
    </property>

    <!-- List/Array 注入(引用其他 Bean) -->
    <property name="users">
        <array>
            <ref bean="u1"/>
            <ref bean="u2"/>
            <ref bean="u3"/>
            <ref bean="u4"/>
        </array>
    </property>

    <!-- Map 注入 -->
    <property name="map">
        <map>
            <entry key="1" value-ref="u1"/>
            <entry key="2"><ref bean="u2"/></entry>
            <entry key="3" value-ref="u3"/>
        </map>
    </property>

    <!-- Properties 注入 -->
    <property name="props">
        <props>
            <prop key="1">jdbc:oracle</prop>
            <prop key="2">jdbc:mysql</prop>
            <prop key="3">jdbc:access</prop>
        </props>
    </property>
</bean>

<!-- 被引用的 User Bean -->
<bean id="u1" class="www.csdn.spring.collection.set.User"><property name="name" value="deep"/><property name="age" value="21"/></bean>
<bean id="u2" class="www.csdn.spring.collection.set.User"><property name="name" value="deepsoul"/><property name="age" value="22"/></bean>
<bean id="u3" class="www.csdn.spring.collection.set.User"><property name="name" value="chrp"/><property name="age" value="23"/></bean>
<bean id="u4" class="www.csdn.spring.collection.set.User"><property name="name" value="redarmy"/><property name="age" value="28"/></bean>

目標類定義

public class User {
    private String name; private Integer age;
    // getter/setter/toString
}

public class CollectionBean {
    private Set sets;
    private List users;
    private Map map;
    private Properties props;
    // getter/setter
}

遍歷測試代碼

public class TestBean {
    @Test
    public void tests() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-collection.xml");
        CollectionBean bean = context.getBean("collectionBean", CollectionBean.class);

        // Set 遍歷
        System.out.println("---set---");
        for (String s : bean.getSets()) System.out.println(s);

        // List 遍歷
        System.out.println("---list---");
        for (User u : bean.getUsers()) System.out.println(u.getName() + " " + u.getAge());

        // Map 遍歷方式 1:keySet
        System.out.println("---map1---");
        for (Integer key : bean.getMap().keySet()) {
            User u = bean.getMap().get(key);
            System.out.println(key + " " + u.getName() + " " + u.getAge());
        }

        // Map 遍歷方式 2:entrySet(推薦)
        System.out.println("---map2---");
        for (Map.Entry entry : bean.getMap().entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue().getName() + " " + entry.getValue().getAge());
        }

        // Properties 遍歷
        System.out.println("---props---");
        for (String key : bean.getProps().stringPropertyNames()) {
            System.out.println(key + " " + bean.getProps().getProperty(key));
        }
    }
}

運行輸出

---set---
岑紅軍
軍哥
哈哈
呵呵
嘿嘿
洗洗
---list---
deep 21
deepsoul 22
chrp 23
redarmy 28
---map1---
1 deep 21
2 deepsoul 22
3 chrp 23
---map2---
1 deep 21
2 deepsoul 22
3 chrp 23
---props---
3 jdbc:access
2 jdbc:mysql
1 jdbc:oracle

關鍵點總結

  • <ref bean="..."/> 引用其他 Bean,<value> 注入字面值
  • <entry> 支持 key/value/value-ref/key-ref 多種組合
  • <props> 專為 Properties 設計,鍵值皆為 String
  • 遍歷 Map 推薦 entrySet(),單次查找避免雜湊計算
  • 集合注入支持合並:多個 <property> 同名會合並(需啟用合並屬性)

掌握四大集合注入語法,配置數據源、緩存配置、模板引擎參數等場景得心應手。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved