1. 程式人生 > >在spring 中如何注入map,set,list,property等引數

在spring 中如何注入map,set,list,property等引數

  1. package cn.dao;  
  2. publicinterface PersonDao {  
  3.     publicabstractvoid add();  
  4. }  

cn.dao.imp
  1. package cn.dao;  
  2. publicinterface PersonDao {  
  3.     publicabstractvoid add();  
  4. }  

cn.service
  1. package cn.service;  
  2. publicinterface PersonService {  
  3.     public
    abstractvoid save();  
  4. }  

cn.service.imp
  1. package cn.service.imp;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.HashSet;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7. import java.util.Properties;  
  8. import java.util.Set;  
  9. import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;  
  10. import cn.service.PersonService;  
  11. publicclass PersonServiceBean implements PersonService {  
  12.     private Set<String> sets=new HashSet<String>();  
  13.     private List<String> lists=new ArrayList<String>();  
  14.     private Properties properties=new Properties();  
  15.     private
     Map<String, String> maps=new HashMap<String, String>();  
  16.     public Map<String, String> getMaps() {  
  17.         return maps;  
  18.     }  
  19.     publicvoid setMaps(Map<String, String> maps) {  
  20.         this.maps = maps;  
  21.     }  
  22.     public Properties getProperties() {  
  23.         return properties;  
  24.     }  
  25.     publicvoid setProperties(Properties properties) {  
  26.         this.properties = properties;  
  27.     }  
  28.     public List<String> getLists() {  
  29.         return lists;  
  30.     }  
  31.     publicvoid setLists(List<String> lists) {  
  32.         this.lists = lists;  
  33.     }  
  34.     public Set<String> getSets() {  
  35.         return sets;  
  36.     }  
  37.     publicvoid setSets(Set<String> sets) {  
  38.         this.sets = sets;  
  39.     }  
  40.     publicvoid save()  
  41.     {  
  42.         System.out.println("執行save()方法");  
  43.     }  
  44. }  


junit.test
  1. package junit.test;  
  2. import org.junit.Test;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.AbstractApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import cn.service.imp.PersonServiceBean;  
  7. publicclass SpringTest   
  8. {  
  9.     @Test
  10.     publicvoid instanceSpring()  
  11.     {  
  12.         AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});  
  13.         PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");  
  14.         System.out.println("-------------set--------------");  
  15.         for(String value:personServiceBean.getSets())  
  16.         {  
  17.             System.out.println(value);  
  18.         }  
  19.         System.out.println("-------------list--------------");  
  20.         for(String value:personServiceBean.getLists())  
  21.         {  
  22.             System.out.println(value);  
  23.         }  
  24.         System.out.println("-------------properties--------------");  
  25.         for(Object key:personServiceBean.getProperties().keySet())  
  26.         {  
  27.             System.out.println(key+"="+(String)personServiceBean.getProperties().getProperty((String)key));  
  28.         }  
  29.         System.out.println("-------------map--------------");  
  30.         for(String key:personServiceBean.getMaps().keySet())  
  31.         {  
  32.             System.out.println(key+"="+(String)personServiceBean.getMaps().get(key));  
  33.         }  
  34.         ctx.close();  
  35.     }  
  36. }  

beans.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6.         <beanid="personService"class="cn.service.imp.PersonServiceBean">
  7.             <propertyname="sets">
  8.                 <