1. 程式人生 > >Spring中使用Map、Set、List、數組、屬性集合的註入方法配置文件

Spring中使用Map、Set、List、數組、屬性集合的註入方法配置文件

查看 main list highlight 配置 spring配置 pla lec while

(1)下邊的一個Java類包含了所有Map、Set、List、數組、屬性集合等這些容器,主要用於演示spring的註入配置;

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


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

[html] view plain copy 技術分享技術分享
  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. <bean id="department" class="com.hsp.collection.Department">
  10. <property name="name" value="財務部"/>
  11. <!-- 給數組註入值 -->
  12. <property name="empName">
  13. <list>
  14. <value>小明</value>
  15. <value>小明小明</value>
  16. <value>小明小明小明小明</value>
  17. </list>
  18. </property>
  19. <!-- 給list註入值 list 中可以有相當的對象 -->
  20. <property name="empList">
  21. <list>
  22. <ref bean="emp2" />
  23. <ref bean="emp1"/>
  24. <ref bean="emp1"/>
  25. <ref bean="emp1"/>
  26. <ref bean="emp1"/>
  27. <ref bean="emp1"/>
  28. <ref bean="emp1"/>
  29. </list>
  30. </property>
  31. <!-- 給set註入值 set不能有相同的對象 -->
  32. <property name="empsets">
  33. <set>
  34. <ref bean="emp1" />
  35. <ref bean="emp2"/>
  36. <ref bean="emp2"/>
  37. <ref bean="emp2"/>
  38. <ref bean="emp2"/>
  39. </set>
  40. </property>
  41. <!-- 給map註入值 map只有key不一樣,就可以裝配value -->
  42. <property name="empMaps">
  43. <map>
  44. <entry key="11" value-ref="emp1" />
  45. <entry key="22" value-ref="emp2"/>
  46. <entry key="22" value-ref="emp1"/>
  47. </map>
  48. </property>
  49. <!-- 給屬性集合配置 -->
  50. <property name="pp">
  51. <props>
  52. <prop key="pp1">abcd</prop>
  53. <prop key="pp2">hello</prop>
  54. </props>
  55. </property>
  56. </bean>
  57. <bean id="emp1" class="com.hsp.collection.Employee">
  58. <property name="name" value="北京"/>
  59. <property name="id" value="1"/>
  60. </bean>
  61. <bean id="emp2" class="com.hsp.collection.Employee">
  62. <property name="name" value="天津"/>
  63. <property name="id" value="2"/>
  64. </bean>
  65. </beans>

(3)如何使用

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



(4)以後那些不知道的粘貼拷貝即可

Spring中使用Map、Set、List、數組、屬性集合的註入方法配置文件