(5) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入

  1. package cn.itm.service.impl; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.HashMap; 
  5. import java.util.HashSet; 
  6. import java.util.List; 
  7. import java.util.Map; 
  8. import java.util.Properties;
  9. import java.util.Set; 
  10.  
  11. import cn.itm.service.PersonService; 
  12.  
  13. public class PersonServiceBeanimplements PersonService{ 
  14.  
  15.     private Set<String> sets =new HashSet<String>(); 
  16.      
  17.     private List<String> lists =new ArrayList<String>(); 
  18.  
  19.     private Properties properties =new Properties(); 
  20.  
  21.     private Map<String, String> maps =new HashMap<String, String>(); 
  22.      
  23.      
  24.  
  25.     public Map<String, String> getMaps() { 
  26.         return maps; 
  27.     } 
  28.  
  29.  
  30.     public void setMaps(Map<String, String> maps) { 
  31.         this.maps = maps; 
  32.     } 
  33.  
  34.  
  35.     public Properties getProperties() { 
  36.         return properties; 
  37.     } 
  38.  
  39.  
  40.     public void setProperties(Properties properties) { 
  41.         this.properties = properties; 
  42.     } 
  43.  
  44.     public List<String> getLists() { 
  45.         return lists; 
  46.     } 
  47.  
  48.  
  49.     public void setLists(List<String> lists) { 
  50.         this.lists = lists; 
  51.     } 
  52.      
  53.     public Set<String> getSets() { 
  54.         return sets; 
  55.     } 
  56.  
  57.  
  58.     public void setSets(Set<String> sets) { 
  59.         this.sets = sets; 
  60.     } 
  61.      
  62.      
  63.  
  64.  
  65.      



  1. import java.util.List; 
  2. import java.util.Map; 
  3. import java.util.Properties; 
  4. import java.util.Set; 
  5.  
  6. public interface PersonService { 
  7.  
  8.     public Set<String> getSets(); 
  9.      
  10.     public List<String> getLists(); 
  11.      
  12.     public Properties getProperties(); 
  13.      
  14.     public Map<String, String> getMaps() ; 
  15.      
  16.      
  17.  

配置 Spring容器:

  1. <?xmlversion="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.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  6.             
  7.            <beanid="personService"class="cn.itm.service.impl.PersonServiceBean"> 
  8.             
  9.                 <propertyname="sets"> 
  10.                         <set> 
  11.                             <value>第一个</value> 
  12.                             <value>第二个</value> 
  13.                             <value>第三个</value> 
  14.                         </set> 
  15.                 </property> 
  16.                  
  17.                 <propertyname="lists"> 
  18.                         <list> 
  19.                             <value>第一个list</value> 
  20.                             <value>第二个list</value> 
  21.                             <value>第三个list</value> 
  22.                         </list> 
  23.                 </property> 
  24.                  
  25.                 <propertyname="properties"> 
  26.                         <props> 
  27.                                 <propkey="key1">value1</prop> 
  28.                                 <propkey="key2">value2</prop> 
  29.                                 <propkey="key3">value3</prop> 
  30.                         </props> 
  31.                 </property> 
  32.                  
  33.                 <propertyname="maps"> 
  34.                         <map> 
  35.                             <entrykey="key1"value="value1"/> 
  36.                             <entrykey="key2"value="value2"/> 
  37.                             <entrykey="key3"value="value3"/> 
  38.                         </map> 
  39.                 </property> 
  40.                  
  41.                  
  42.            </bean> 
  43.     
  44. </beans> 

测试类:

  1. package junit.test; 
  2.  
  3.  
  4. import org.junit.BeforeClass; 
  5. import org.junit.Test; 
  6. import org.springframework.context.ApplicationContext; 
  7. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  8.  
  9. import cn.itm.service.PersonService; 
  10.  
  11. public class SpringTest { 
  12.  
  13.     @BeforeClass 
  14.     public staticvoid setUpBeforeClass() throws Exception { 
  15.     } 
  16.  
  17.     // 专门用来实例化  Spring 容器的。 
  18.     @Test publicvoid instanceSpring(){ 
  19.          
  20.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  21.         PersonService personService = (PersonService) ctx.getBean("personService"); 
  22.          
  23.  
  24.          
  25.         for(String value: personService.getSets()){ 
  26.             System.out.println(value); 
  27.         } 
  28.         System.out.println("-------------------------------"); 
  29.         for(String value: personService.getLists()){ 
  30.             System.out.println(value); 
  31.         } 
  32.         System.out.println("-------------Properties()------------------"); 
  33.         for(Object key: personService.getProperties().keySet()){ 
  34.             System.out.println(key + "=" + personService.getProperties().getProperty((String)key)); 
  35.         } 
  36.          
  37.         System.out.println("-------------map------------------"); 
  38.         for(Object key: personService.getMaps().keySet()){ 
  39.             System.out.println(key + "=" + personService.getMaps().get(key)); 
  40.         } 
  41.     } 


二:使用  使用构造器注入


  1. package cn.itm.service.impl; 
  2.  
  3. import cn.itm.dao.PersonDao; 
  4. import cn.itm.service.PersonService; 
  5.  
  6. public class PersonServiceBeanimplements PersonService{ 
  7.  
  8.     private PersonDao personDao; 
  9.     private String name; 
  10.      
  11.     public PersonServiceBean(PersonDao personDao, String name) { 
  12.         this.personDao = personDao; 
  13.         this.name = name; 
  14.     } 
  15.  
  16.     public PersonDao getPersonDao() { 
  17.             return personDao; 
  18.     } 
  19.  
  20.     public void setPersonDao(PersonDao personDao) { 
  21.         this.personDao = personDao; 
  22.     } 
  23.  
  24.     public String getName() { 
  25.         return name; 
  26.     } 
  27.  
  28.     public void setName(String name) { 
  29.         this.name = name; 
  30.     } 
  31.  
  32. public void save(){ 
  33.     personDao.add(); 
  34.     System.out.println(name); 
  35.      


  1. package cn.itm.service; 
  2.  
  3.  
  4. public interface PersonService { 
  5.  
  6.     public void save(); 

  1. package cn.itm.dao.impl; 
  2.  
  3. import cn.itm.dao.PersonDao; 
  4.  
  5.  
  6. public class PersonDaoBeanimplements PersonDao { 
  7.  
  8.      
  9.     public void add(){ 
  10.         System.out.println("执行PersonDaoBean的add方法。。。"); 
  11.     } 

  1. package cn.itm.dao; 
  2.  
  3. public interface PersonDao { 
  4.  
  5.     public abstractvoid add(); 
  6.  


配置文件:

  1. <?xmlversion="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.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  6.             
  7.            <beanid="personDao"class="cn.itm.dao.impl.PersonDaoBean"></bean> 
  8.            <beanid="personService"class="cn.itm.service.impl.PersonServiceBean"> 
  9.             
  10.                 <constructor-argindex="0"type="cn.itm.dao.PersonDao"ref="personDao"/> 
  11.                 <constructor-argindex="1"value="大家好。"></constructor-arg> 
  12.            </bean> 
  13.     
  14. </beans> 

测试类:

  1. package junit.test; 
  2.  
  3.  
  4. import org.junit.BeforeClass; 
  5. import org.junit.Test; 
  6. import org.springframework.context.ApplicationContext; 
  7. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  8.  
  9. import cn.itm.service.PersonService; 
  10.  
  11. public class SpringTest { 
  12.  
  13.     @BeforeClass 
  14.     public staticvoid setUpBeforeClass() throws Exception { 
  15.     } 
  16.  
  17.     // 专门用来实例化  Spring 容器的。 
  18.     @Test publicvoid instanceSpring(){ 
  19.          
  20.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  21.         PersonService personService = (PersonService) ctx.getBean("personService"); 
  22.         personService.save(); 
  23.     } 



打印结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值