Spring装配集合属性

在Spring中可以装配4种集合类型属性:List、set、Map和Properties。与这四种集合对应的标签是、、、。CollectionBean是一个包含上述4种集合类型的JavaBean,代码如下:

package chapter22;

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

public class CollectionBean {
    private List myList;
    private String myArray[];
    private Set mySet;
    private Map myMap;
    private Properties myProperties;
    
    public List getMyList() {
        return myList;
    }
    public void setMyList(List myList) {
        System.out.println("set List类型:"+myList.getClass().getName());
        this.myList = myList;
    }
    public String[] getMyArray() {
        return myArray;
    }
    public void setMyArray(String[] myArray) {
        this.myArray = myArray;
    }
    public Set getMySet() {
        return mySet;
    }
    public void setMySet(Set mySet) {
        System.out.println("set Set类型:"+mySet.getClass().getName());
        this.mySet = mySet;
    }
    public Map getMyMap() {
        return myMap;
    }
    public void setMyMap(Map myMap) {
        System.out.println("set Map类型:"+myMap.getClass().getName());
        this.myMap = myMap;
    }
    public Properties getMyProperties() {
        return myProperties;
    }
    public void setMyProperties(Properties myProperties) {
        System.out.println("set Properties类型:"+myProperties.getClass().getName());
        this.myProperties = myProperties;
    }
}
下面是装配上述4中集合类型属性的配置代码
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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="helloservice" class="chapter22.HelloServiceImpl">
        <property name="greeting" value="greeting Xu Wei">property>bean>
    <bean id="myBean" class="chapter22.MyBean">
        <property name="hello">
            <ref bean="helloservice" />property>
        <property name="strName" value="Xu Wei">property>bean>
    <bean id="CollectionBean" class="chapter22.CollectionBean">
    <property name="myList">
            <list><value>abcdvalue>
                <idref bean="myBean">idref>
                list>
        property><property name="myMap">
            <map>
                <entry>
                    <key>
                        <value>hellovalue>key>
                    <value>1234value>entry>
                <entry key="abcd">
                    <ref bean="helloservice" />entry>
                <entry>
                    <key>
                        <ref bean="helloservice" >ref>key>
                    <ref bean="myBean" >ref>entry>map>property>
        <property name="mySet">
            <set>
                <value>testvalue>
                <ref bean="myBean">ref>set>property>
                
        <property name="myProperties">
            <props><prop key="abcd">value1prop>
                <prop key="prop">myPropprop>props>property>
        <property name="myArray">
            <list>
                <value>myArrayvalue>
                <idref bean="helloservice" >idref>list>property>bean>
beans>

下面的TestCollectionContext.java代码使用ApplicationContext接口的getBean方法创建了CollectionBean对象,并访问了相关属性。

package chapter22;

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

public class TestCollectionBean {
    public static void main(String args[])
    {
        ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml");
/*在创建ctx对象实例以后,applicationContext.xml配置中涉及到的Bean的set方法都被调用了。
 *单单执行ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml");
 *的输出结果是:
 *log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
 *log4j:WARN Please initialize the log4j system properly.
 *setGreeting()方法被调用
 *setHello()方法被调用
 *setStrName()方法被调用
 *set List类型:java.util.ArrayList
 *set Map类型:java.util.LinkedHashMap
 *set Set类型:java.util.LinkedHashSet
 *set Properties类型:java.util.Properties
 */    
        //创建CollectionBean对象cb。
        CollectionBean cb=(CollectionBean)ctx.getBean("CollectionBean");
        
        System.out.println("------测试List------");
        for(String s:cb.getMyList())
            System.out.println(s);
        System.out.println("------测试Array------");
        for(String s:cb.getMyArray())
            System.out.println(s);
        System.out.println("------测试Set------");
        for(Object obj:cb.getMySet())
            System.out.println(obj.getClass().getName());
        System.out.println("------测试Map------");
        //Map其实就是key-value的键值对。cb.getMyMap().get(key)是要找到key所对应的value。
        for(Object key:cb.getMyMap().keySet())
            System.out.println(key+"="+cb.getMyMap().get(key));
        System.out.println("------测试Properties------");
        for(Object key:cb.getMyProperties().keySet())
            System.out.println(key+"="+cb.getMyProperties().get(key));
    }
}

输出结果:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
setGreeting()方法被调用
setHello()方法被调用
setStrName()方法被调用
set List类型:java.util.ArrayList
set Map类型:java.util.LinkedHashMap
set Set类型:java.util.LinkedHashSet
set Properties类型:java.util.Properties
------测试List------
abcd
myBean
------测试Array------
myArray
helloservice
------测试Set------
java.lang.String
chapter22.MyBean
------测试Map------
hello=1234
abcd=chapter22.HelloServiceImpl@e70e30
chapter22.HelloServiceImpl@e70e30=chapter22.MyBean@154864a
------测试Properties------
abcd=value1
prop=myProp
作者:xwdreamer
欢迎任何形式的转载,但请务必注明出处。
分享到:
原文地址:https://www.cnblogs.com/xwdreamer/p/2297093.html