zoukankan      html  css  js  c++  java
  • Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件

    (1)下边的一个Java类包含了所有Map、Set、List、数组、属性集合等这些容器,主要用于演示spring的注入配置;

     

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.lc.collection;  
    2.   
    3. import java.util.List;  
    4. import java.util.Map;  
    5. import java.util.Properties;  
    6. import java.util.Set;  
    7.   
    8. public class Department {  
    9.   
    10.     private String name;  
    11.     private String [] empName;//数组  
    12.     private List<Employee> empList;//list集合  
    13.     private Set<Employee> empsets;//set集合  
    14.     private Map<String,Employee> empMaps;//map集合  
    15.     private Properties pp;//Properties的使用  
    16.   
    17.       
    18.     public Set<Employee> getEmpsets() {  
    19.         return empsets;  
    20.     }  
    21.     public void setEmpsets(Set<Employee> empsets) {  
    22.         this.empsets = empsets;  
    23.     }  
    24.     public String[] getEmpName() {  
    25.         return empName;  
    26.     }  
    27.     public void setEmpName(String[] empName) {  
    28.         this.empName = empName;  
    29.     }  
    30.     public String getName() {  
    31.         return name;  
    32.     }  
    33.     public void setName(String name) {  
    34.         this.name = name;  
    35.     }  
    36.     public List<Employee> getEmpList() {  
    37.         return empList;  
    38.     }  
    39.     public void setEmpList(List<Employee> empList) {  
    40.         this.empList = empList;  
    41.     }  
    42.     public Map<String, Employee> getEmpMaps() {  
    43.         return empMaps;  
    44.     }  
    45.     public void setEmpMaps(Map<String, Employee> empMaps) {  
    46.         this.empMaps = empMaps;  
    47.     }  
    48.     public Properties getPp() {  
    49.         return pp;  
    50.     }  
    51.     public void setPp(Properties pp) {  
    52.         this.pp = pp;  
    53.     }  
    54.   
    55. }  


    (2)Spring配置文件beans.xml文件

     

     

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.         xmlns:context="http://www.springframework.org/schema/context"  
    5.         xmlns:tx="http://www.springframework.org/schema/tx"  
    6.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
    8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    9.   
    10. <bean id="department" class="com.hsp.collection.Department">  
    11. <property name="name" value="财务部"/>  
    12.   
    13. <!-- 给数组注入值 -->  
    14. <property name="empName">  
    15.     <list>  
    16.         <value>小明</value>  
    17.         <value>小明小明</value>  
    18.         <value>小明小明小明小明</value>  
    19.     </list>  
    20. </property>  
    21.   
    22. <!-- 给list注入值 list 中可以有相当的对象 -->  
    23. <property name="empList">  
    24.     <list>  
    25.         <ref bean="emp2" />  
    26.         <ref bean="emp1"/>  
    27.         <ref bean="emp1"/>  
    28.         <ref bean="emp1"/>  
    29.         <ref bean="emp1"/>  
    30.         <ref bean="emp1"/>  
    31.         <ref bean="emp1"/>  
    32.     </list>  
    33. </property>  
    34.   
    35. <!-- 给set注入值 set不能有相同的对象 -->  
    36. <property name="empsets">  
    37.     <set>  
    38.         <ref bean="emp1" />  
    39.         <ref bean="emp2"/>  
    40.         <ref bean="emp2"/>  
    41.         <ref bean="emp2"/>  
    42.         <ref bean="emp2"/>  
    43.     </set>  
    44. </property>  
    45.   
    46. <!-- 给map注入值 map只有key不一样,就可以装配value -->  
    47. <property name="empMaps">  
    48.     <map>  
    49.         <entry key="11" value-ref="emp1" />   
    50.         <entry key="22" value-ref="emp2"/>  
    51.         <entry key="22" value-ref="emp1"/>  
    52.     </map>  
    53. </property>  
    54.   
    55. <!-- 给属性集合配置 -->  
    56. <property name="pp">  
    57.     <props>  
    58.         <prop key="pp1">abcd</prop>  
    59.         <prop key="pp2">hello</prop>  
    60.     </props>  
    61. </property>  
    62. </bean>  
    63.   
    64. <bean id="emp1" class="com.hsp.collection.Employee">  
    65.     <property name="name" value="北京"/>  
    66.     <property name="id" value="1"/>  
    67. </bean>  
    68. <bean id="emp2" class="com.hsp.collection.Employee">  
    69.     <property name="name" value="天津"/>  
    70.     <property name="id" value="2"/>  
    71. </bean>  
    72.   
    73. </beans>  

     

    (3)如何使用

     

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.lc.collection;  
    2.   
    3. import java.util.Enumeration;  
    4. import java.util.Iterator;  
    5. import java.util.Map;  
    6. import java.util.Properties;  
    7. import java.util.Map.Entry;  
    8.   
    9. import org.springframework.context.ApplicationContext;  
    10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    11.   
    12. public class App1 {  
    13.   
    14.       
    15.     public static void main(String[] args) {  
    16.   
    17.         ApplicationContext ac=new ClassPathXmlApplicationContext("com/lc/collection/beans.xml");  
    18.         Department department=(Department) ac.getBean("department");  
    19.         System.out.println(department.getName());  
    20.         for(String emName:department.getEmpName()){  
    21.             System.out.println(emName);  
    22.         }  
    23.         /* 
    24.          * 通过list集合取出数据 
    25.          */  
    26.         System.out.println("**********通过list集合取出数据*****");  
    27.         for(Employee e : department.getEmpList()){  
    28.             System.out.println("name="+e.getName()+" "+e.getId());  
    29.         }  
    30.         /* 
    31.          * 通过set集合取出数据 
    32.          */  
    33.         System.out.println("**********通过set集合取出数据*****");  
    34.         for(Employee e : department.getEmpsets()){  
    35.               
    36.             System.out.println("name="+e.getName());  
    37.         }  
    38.         /* 
    39.          * 通过map集合取出数据 迭代器 
    40.          */  
    41.         System.out.println("*******通过map集合取出数据 迭代器****");  
    42.           
    43.         //1.迭代器  
    44.         Map<String,Employee> empmaps=department.getEmpMaps();  
    45.         Iterator it=empmaps.keySet().iterator();  
    46.         while(it.hasNext()){  
    47.             String key=(String) it.next();  
    48.             Employee emp=empmaps.get(key);  
    49.             System.out.println("key="+key+" "+emp.getName());  
    50.         }  
    51.           
    52.         System.out.println("*******通过map集合取出数据 简洁方法****");  
    53.         //2.简洁方法  
    54.         for(Entry<String,Employee> entry1:department.getEmpMaps().entrySet()){  
    55.               
    56.             System.out.println(entry1.getKey()+" "+entry1.getValue().getName());  
    57.         }  
    58.           
    59.         System.out.println("*****通过Propertis取出数据*****");  
    60.         Properties pp=department.getPp();  
    61.         for(Entry<Object,Object> entry:pp.entrySet()){  
    62.             System.out.println(entry.getKey().toString()+" "+entry.getValue().toString());  
    63.         }  
    64.         System.out.println("*****通过Enumeration取出*****");  
    65.         Enumeration en= pp.keys();  
    66.         while(en.hasMoreElements()){  
    67.             String key=(String) en.nextElement();  
    68.             System.out.println(key+" "+pp.getProperty(key));  
    69.         }  
    70.     }  
    71.   
    72. }  



     

    (4)以后那些不知道的粘贴拷贝即可

  • 相关阅读:
    ArcGIS Pro获得一个要素图层一种方法
    ArcGIS Pro layout clone
    ActiveMapViewChanged和选择变化
    ArcGIS Pro 改变栅格的数据源
    ArcGIS Pro自定义图标
    Windows Server 2016 路由和远程访问
    IIS应用程序池_缓存回收
    asp.net RSA密钥之C#格式与Java格式转换(PEM格式)
    MD5和Hash
    C# list与数组的转换
  • 原文地址:https://www.cnblogs.com/lxl57610/p/6813647.html
Copyright © 2011-2022 走看看