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>abcd 
        value> <idref bean="myBean"> 
         idref>  
            
          list> 
           property> 
             <property name="myMap"> <map> <entry> <key> <value>hello 
            value> 
             key> <value>1234 
              value> 
               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>test 
                       value> <ref bean="myBean"> 
                        ref> 
                         set> 
                          property> <property name="myProperties"> <props> 
                            <prop key="abcd">value1 
                           prop> <prop key="prop">myProp 
                            prop> 
                             props> 
                              property> <property name="myArray"> <list> <value>myArray 
                               value> <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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值