Spring学习笔记10--装配bean集合类型注入值

装配bean——集合类型注入值:

本文介绍数组、list集合、set集合、map集合、properties的注值

源码地址:http://download.csdn.net/detail/tingzhiyi/9593835

1、基本信息

包名:com.beans.collection,包下3个类+beans.xml:
类一:Department.java  (基本类,定义一些变量)
类二:Employee.java    (员工类)
类三:App1.java       (测试类)
beans.xml       (配置文件)

2、公共代码:

Department.java中代码:

public class Department {  
  
    private String name;//表示雇员名字  
    private String [] empName;//数组注值  
    public int[] number;//数组注值,表示雇员工资  
    private List<Employee> emplist;//list集合注值  
    private Set<Employee> empsets;//set集合注值  
    private Map<String,Employee> empmaps;//map集合注值  
    private Properties pp; //properties的注值  
  
    //各个变量的set/get方法,省略...  
    }

Employee.java中代码:

 

public class Employee {  
  
    private String name;  
    private int id;  
  
    各个变量的set/get方法,省略...  
    }  

  

 

 

 

3、数组注值
beans.xml中代码:

<bean id="department" class="com.beans.collection.Department">  
<property name="name" value="财务部"></property>  
<!-- 给数组注值 -->  
<property name="empName" >  
    <list>  
        <value>小明</value>  
        <value>大明</value>  
        <value>老明</value>  
    </list>  
</property>  
<property name="number">  
    <list>  
        <value>5900</value>  
        <value>8800</value>  
        <value>6300</value>  
    </list>  
</property>  
</bean>  

  

App1.java中代码(显示结果):

public static void main(String[] args)  
{  
    ApplicationContext ac=new        ClassPathXmlApplicationContext("com/beans/collection/beans.xml");  
    Department dm=(Department)ac.getBean("department");  
    System.out.println(dm.getName());//获取部门名称  
    System.out.println("
"+"*****通过一般数组取出数据*****");  
    for(String emName:dm.getEmpName())  
    {  
        System.out.print(emName+" ");//获取雇员名称  
    }  
    System.out.println();  
    for(int numbers:dm.getNumber())  
    {  
        System.out.print(numbers+" ");//获取雇员工资  
    }  
}  

  


 

 



4、List注值
beans.xml中代码:

<bean id="department" class="com.beans.collection.Department">  
<property name="name" value="财务部"></property>  
<!-- 给list注值 -->  
<property name="emplist">  
    <list>  
        <ref bean="emp1"/>  
        <ref bean="emp2"/>  
        <ref bean="emp1"/>  
        <ref bean="emp3"/>  
        <ref bean="emp2"/>  
    </list>  
</property>  
</bean>  
<!-- 定义的两个雇员 -->  
<bean id="emp1" class="com.beans.collection.Employee">  
    <property name="name" value="北京" />  
    <property name="id" value="8001" />  
</bean>  
<bean id="emp2" class="com.beans.collection.Employee">  
    <property name="name" value="天津" />  
    <property name="id" value="8002" />  
</bean>  
<bean id="emp3" class="com.beans.collection.Employee">  
    <property name="name" value="唐山" />  
    <property name="id" value="8003" />  
</bean> 

  

App1.java中代码(显示结果):

public static void main(String[] args)  
{  
    ApplicationContext ac=new ClassPathXmlApplicationContext("com/beans/collection/beans.xml");  
    Department dm=(Department)ac.getBean("department");  
    System.out.println(dm.getName());//获取部门名称  
    System.out.println("
"+"*****通过List集合取出数据*****");  
    for(Employee emps:dm.getEmplist())  
    {  
        System.out.print("name="+emps.getName()+"  ");    
    }  
}  

  



5、set注值
beans.xml中代码:

<bean id="department" class="com.beans.collection.Department">  
<property name="name" value="财务部"></property>  
<!-- 给set注值 -->  
<property name="empsets">  
    <set>  
        <ref bean="emp1"/>  
        <ref bean="emp2"/>  
        <ref bean="emp3"/>  
    </set>  
</property>  
</bean>  
<!-- 定义的两个雇员 -->  
<bean id="emp1" class="com.beans.collection.Employee">  
    <property name="name" value="北京" />  
    <property name="id" value="8001" />  
</bean>  
<bean id="emp2" class="com.beans.collection.Employee">  
    <property name="name" value="天津" />  
    <property name="id" value="8002" />  
</bean>  
<bean id="emp3" class="com.beans.collection.Employee">  
    <property name="name" value="唐山" />  
    <property name="id" value="8003" />  
</bean> 

  

App1.java中代码(显示结果):

public static void main(String[] args)  
{  
    ApplicationContext ac=new ClassPathXmlApplicationContext("com/beans/collection/beans.xml");  
    Department dm=(Department)ac.getBean("department");  
    System.out.println(dm.getName());//获取部门名称  
    System.out.println("
"+"*****通过Set集合取出数据*****");  
    for(Employee emps:dm.getEmpsets())  
    {  
        System.out.print("name="+emps.getName()+"  ");    
    }  
} 

  



6、Map注值
beans.xml中代码:

<bean id="department" class="com.beans.collection.Department">  
<property name="name" value="财务部"></property>  
<!-- 给map注值 -->  
<property name="empmaps">  
    <map>  
        <entry key="1" value-ref="emp1" />  
        <entry key="2" value-ref="emp2" />  
        <entry key="3" value-ref="emp3" />  
    </map>  
</property>  
</bean>  
<!-- 定义的两个雇员 -->  
<bean id="emp1" class="com.beans.collection.Employee">  
    <property name="name" value="北京" />  
    <property name="id" value="8001" />  
</bean>  
<bean id="emp2" class="com.beans.collection.Employee">  
    <property name="name" value="天津" />  
    <property name="id" value="8002" />  
</bean>  
<bean id="emp3" class="com.beans.collection.Employee">  
    <property name="name" value="唐山" />  
    <property name="id" value="8003" />  
</bean> 

  

App1.java中代码(显示结果):
这里取出数据有两种方法:1、使用迭代器;2、使用简洁方法

public static void main(String[] args)  
{  
    ApplicationContext ac=new ClassPathXmlApplicationContext("com/beans/collection/beans.xml");  
    Department dm=(Department)ac.getBean("department");  
    System.out.println(dm.getName());//获取部门名称  
    //1、使用迭代器  
    System.out.println("
"+"*****通过Map集合取出数据(迭代器方法取出)*****");  
    Map<String,Employee> empmaps=dm.getEmpmaps();  
    Iterator ite=empmaps.keySet().iterator();  
    while(ite.hasNext())  
    {  
        String key=(String) ite.next();  
        Employee emp=empmaps.get(key);  
        System.out.println("key="+key+" "+emp.getName());  
    }  
    //2、使用简洁方法  
    System.out.println("
"+"*****通过Map集合取出数据(简洁方法取出)*****");  
    for(Entry<String,Employee> entry1:dm.getEmpmaps().entrySet())  
    {  
        System.out.println("key="+entry1.getKey()+" "+entry1.getValue().getName()+" ");   
    }  
}  

  



7、properties注值
beans.xml中代码:

<bean id="department" class="com.beans.collection.Department">  
<property name="name" value="财务部"></property>  
<!-- properties注值 -->  
<property name="pp">  
    <props>  
        <prop key="pp1">abcd</prop>  
        <prop key="pp2">hello</prop>  
        <prop key="pp3">你好</prop>  
    </props>  
</property>  
</bean>  

  

App1.java中代码(显示结果):
这里取出数据有两种方法:1、使用properties取出数据;2、使用Enumeration取出数据

public static void main(String[] args)  
{  
    ApplicationContext ac=new ClassPathXmlApplicationContext("com/beans/collection/beans.xml");  
    Department dm=(Department)ac.getBean("department");  
    System.out.println(dm.getName());//获取部门名称  
    //通过properties取出数据  
    System.out.println("*****通过properties取出数据*****");  
    Properties pp=dm.getPp();  
    //System.out.println(pp.get("pp1").toString());  
    for(Entry<Object,Object> entry:pp.entrySet())  
    {  
        System.out.println(entry.getKey()+" : "+entry.getValue().toString());     
    }  
    //通过Enumeration取出数据  
    System.out.println("*****通过Enumeration取出数据*****");  
    Enumeration en=pp.keys();  
    while(en.hasMoreElements())  
    {  
        String key=(String) en.nextElement();  
        System.out.println(key+" : "+pp.getProperty(key));    
    }  
}  

  



原文地址:https://www.cnblogs.com/huangyangquan/p/8578350.html