Spring集合 (List,Set,Map,Properties) 實例

下面例子向您展示Spring如何注入值到集合類型(List, Set, Map, and Properties)。 支持4個主要的集合類型:

  • List –
  • Set –
  • Map –
  • Properties –

Spring beans

一個Customer對象,有四個集合屬性。

package com.yiibai.common;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Customer
{
private List lists;
private Set sets;
private Map<Object, Object> maps;
private Properties pros;

//...

}

在bean配置文件中不同的代碼片段用來聲明集合。

1. List示例

1

2. Set示例

1

3. Map示例

4. Properties示例

[email protected] [email protected]

Spring完整的 bean 配置文件。

<bean id="CustomerBean" class="com.yiibai.common.Customer">

    <!-- java.util.List -->
    <property name="lists">
        <list>
            <value>1</value>
            <ref bean="PersonBean" />
            <bean class="com.yiibai.common.Person">
                <property name="name" value="yiibaiList" />
                <property name="address" value="Hainan Haikou" />
                <property name="age" value="28" />
            </bean>
        </list>
    </property>

    <!-- java.util.Set -->
    <property name="sets">
        <set>
            <value>1</value>
            <ref bean="PersonBean" />
            <bean class="com.yiibai.common.Person">
                <property name="name" value="yiibaiSet" />
                <property name="address" value="Hainan Haikou" />
                <property name="age" value="28" />
            </bean>
        </set>
    </property>

    <!-- java.util.Map -->
    <property name="maps">
        <map>
            <entry key="Key 1" value="1" />
            <entry key="Key 2" value-ref="PersonBean" />
            <entry key="Key 3">
                <bean class="com.yiibai.common.Person">
                    <property name="name" value="yiibaiMap" />
                    <property name="address" value="Hainan Haikou" />
                    <property name="age" value="28" />
                </bean>
            </entry>
        </map>
    </property>

    <!-- java.util.Properties -->
    <property name="pros">
        <props>
            <prop key="admin">[email protected]</prop>
            <prop key="support">[email protected]</prop>
        </props>
    </property>

</bean>

<bean id="PersonBean" class="com.yiibai.common.Person">
    <property name="name" value="yiibai1" />
    <property name="address" value="Hainan Haikou 1" />
    <property name="age" value="28" />
</bean>

執行程序

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    Customer cust = (Customer)context.getBean("CustomerBean");
    System.out.println(cust);

}

}

輸出

Customer [lists=[1, com.yiibai.common.Person@4e4ee70b, com.yiibai.common.Person@1e1867d2], sets=[1, com.yiibai.common.Person@4e4ee70b, com.yiibai.common.Person@52f644b4], maps={Key 1=1, Key 2=com.yiibai.common.Person@4e4ee70b, Key 3=com.yiibai.common.Person@54481b6d}, pros={admin=[email protected], support=[email protected]}]

下載代碼 – http://pan.baidu.com/s/1c0T3i5i


Copyright © 2015-2023 億聚網.