《Pro Spring》学习笔记之集合注入

来源:互联网 发布:所有当当读书软件 编辑:程序博客网 时间:2024/04/20 22:19

配置文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans
    
xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="collectionTest" class="ch4_CollectionDI.CollectionTest">
  
  
<property name="map">
    
<map>
      
<entry key="someValue">
        
<value>somekey</value>
      
</entry>
      
<entry key="someBean">
        
<ref bean="course"/>
      
</entry>
    
</map>
  
</property>
  
  
<property name="list">
    
<list>
      
<value>listvalue</value>
      
<ref bean="course"/>
    
</list>
  
</property>
  
  
<property name="set">
    
<set>
     
<value>setvalue</value>
     
<ref bean="course"/>
    
</set>
  
</property>
  
  
<!-- properties只能存放String类型数据 -->
  
<property name="properties">
    
<props>
      
<prop key="someValue1">
        1
      
</prop>
      
<prop key="someValue2">
        2
      
</prop>
    
</props>
  
</property>
</bean>

<bean id="course" class="ch4_CollectionDI.Course">
</bean>
</beans>

 javaBean:

 

package ch4_CollectionDI;

public class Course {
  
private String courseName;

public String getCourseName() {
    
return courseName;
}


public void setCourseName(String courseName) {
    
this.courseName = courseName;
}

}

 

测试代码:

 

package ch4_CollectionDI;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;



public class TestSpring {
  
public static void main(String args[])  throws Exception{
      
//获取bean factory
      BeanFactory factory=(BeanFactory)getBeanFactory();
      
      CollectionTest collectionTest
=(CollectionTest)factory.getBean("collectionTest");
      
      Map map
=collectionTest.getMap();
      List list
=collectionTest.getList();
      Set set
=collectionTest.getSet();
      Properties properties
=collectionTest.getProperties();
      
      System.out.println(
"Map content:");
      Iterator iter
=map.keySet().iterator();
      
while (iter.hasNext()) {
         Object element 
=  iter.next();
         System.out.println(
"key:"+element+"-value:"+map.get(element));
      }

      
      System.out.println(
"Set content:");
      iter
=set.iterator();
      
while (iter.hasNext()) {
         Object element 
=  iter.next();
         System.out.println(
"value:"+element);
      }

      
      System.out.println(
"List content:");
      iter
=list.iterator();
      
while (iter.hasNext()) {
         Object element 
=  iter.next();
         System.out.println(
"value:"+element);
      }

      
      
//properties迭代后排序的
      System.out.println("Properties content:");
      iter
=properties.keySet().iterator();
      
while (iter.hasNext()) {
         Object element 
=  iter.next();
         System.out.println(
"key:"+element+"-value:"+properties.get(element));
      }

      
  }

  
public static BeanDefinitionRegistry getBeanFactory() {
      
//获取bean factory
      String realpath="";
         
//加载配置项
       realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch4_CollectionDI"+File.separator+"applicationContext.xml";
      XmlBeanFactory  factory
=new XmlBeanFactory(new FileSystemResource(realpath));
     
      
      
return factory;
  }

}

 

运行结果:

Map content:
key:someValue-value:somekey
key:someBean-value:ch4_CollectionDI.Course@a8c488
Set content:
value:setvalue
value:ch4_CollectionDI.Course@a8c488
List content:
value:listvalue
value:ch4_CollectionDI.Course@a8c488
Properties content:
key:someValue2-value:2
key:someValue1-value:1

原创粉丝点击