Spring 注解实例--@Autowired 注入到List和Map

[java]  view plain  copy
 print ?
  1. package com.Autowired.ListMap;  
  2.   
  3. import org.springframework.core.annotation.Order;  
  4. import org.springframework.stereotype.Component;  
  5. /** 
  6.  * order:把实现类排序输出 只适合List 
  7.  * @author liu 
  8.  * 
  9.  */  
  10. @Order(2)  
  11. @Component  
  12. public class BeanImplOne implements BeanInterface {  
  13.   
  14. }  

================================

[java]  view plain  copy
 print ?
  1. package com.Autowired.ListMap;  
  2.   
  3. import org.springframework.core.annotation.Order;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Order(1)  
  7. @Component  
  8. public class BeanImplTwo implements BeanInterface {  
  9.   
  10. }  

=====================================


BeanInterface只是一个接口无方法


======================================

[java]  view plain  copy
 print ?
  1. package com.Autowired.ListMap;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6.   
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.beans.factory.annotation.Qualifier;  
  9. import org.springframework.stereotype.Component;  
  10.   
  11. @Component  
  12. public class BeanInvoke {  
  13.       
  14.     @Autowired  
  15.       private List<BeanInterface> list;  
  16.         
  17.     @Autowired  
  18.     private Map<String,BeanInterface> map;  
  19.       
  20.     /** @Autowired默认为byType的  所以有两个相同类型的bean   
  21.      * 如果不使用 @Qualifier指定具体的bean就会抛出异常 
  22.      *  private BeanInterface beaninterface; 
  23.      */  
  24.     @Autowired  
  25.    @Qualifier("beanImplOne")  
  26.     private BeanInterface beaninterface;  
  27.     public void say(){  
  28.         System.out.println("list...");  
  29.         if(null !=list &&0!=list.size()){  
  30.             for(BeanInterface bean :list){  
  31.                 System.out.println(bean.getClass().getName());  
  32.             }  
  33.               
  34.         }else{  
  35.             System.out.println("List<BeanInterface> list is null !!!!");  
  36.         }  
  37.         System.out.println();  
  38.         System.out.println("map...");  
  39.         if(null !=map &&0!=map.size()){  
  40.             for(Map.Entry<String, BeanInterface> m:map.entrySet()){  
  41.                   System.out.println(m.getKey()+"    "+m.getValue().getClass().getName());  
  42.             }  
  43.         }else{  
  44.             System.out.println("Map<String,BeanInterface> map is null !!!!");  
  45.   
  46.         }  
  47.         System.out.println("-------------------------");  
  48.         if(null !=beaninterface){  
  49.             System.out.println(beaninterface.getClass().getName());  
  50.         }else{  
  51.             System.out.println("beaninterface is null !!!");  
  52.         }  
  53.     }  
  54.       
  55.       
  56. }  

------------------------------------------------------

配置文件:

[java]  view plain  copy
 print ?
  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.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context  
  8.         http://www.springframework.org/schema/context/spring-context.xsd" >  
  9.           
  10.         <context:component-scan base-package="package com.Autowired.ListMap;"></context:component-scan>  
  11.           
  12.  </beans>  


-----------------------------------------------------

测试类:

[java]  view plain  copy
 print ?
  1. package com.Autowired.ListMap;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. import com.imooc.test.base.UnitTestBase;  
  6.   
  7.   
  8. public class TestListMap  extends UnitTestBase{  
  9.     public TestListMap(){  
  10.         super("classpath*:spring-beanannotation3.xml");  
  11.     }  
  12.       
  13.     @Test  
  14.     public void test(){  
  15.         BeanInvoke  bean=super.getBean("beanInvoke");  
  16.         bean.say();  
  17.     }  
  18. }  

结果:

[java]  view plain  copy
 print ?
  1. 2017-6-4 15:38:26 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@58a17083: startup date [Sun Jun 04 15:38:26 CST 2017]; root of context hierarchy  
  3. 2017-6-4 15:38:26 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. 信息: Loading XML bean definitions from URL [file:/E:/myeclipse/workspace/Spring2/bin/spring-beanannotation3.xml]  
  5. 2017-6-4 15:38:27 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>  
  6. 信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring  
  7. list...  
  8. 2017-6-4 15:38:27 org.springframework.context.support.AbstractApplicationContext doClose  
  9. 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@58a17083: startup date [Sun Jun 04 15:38:26 CST 2017]; root of context hierarchy  
  10. com.Autowired.ListMap.BeanImplTwo  
  11. com.Autowired.ListMap.BeanImplOne  
  12.   
  13. map...  
  14. beanImplOne    com.Autowired.ListMap.BeanImplOne  
  15. beanImplTwo    com.Autowired.ListMap.BeanImplTwo  
  16. -------------------------  
  17. com.Autowired.ListMap.BeanImplOne  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值