Spring2.5学习笔记1-控制反转-DI容器泛型访问

 

  
  
  
  
  1. public class GenericBeanFactoryAccessorMain { 
  2.     protected static final Log log = LogFactory.getLog(GenericBeanFactoryAccessorMain.class); 
  3.  
  4.     /** 
  5.      * @param args 
  6.      */ 
  7.     public static void main(String[] args) { 
  8.         // TODO Auto-generated method stub 
  9. //      JavaConfigApplicationContext context = new JavaConfigApplicationContext(JavaConfigDemo.class); 
  10.         Resource resource = new ClassPathResource("applicationContext.xml"); 
  11.         ListableBeanFactory beanFactory= new XmlBeanFactory(resource); 
  12.         GenericBeanFactoryAccessor gbf = new GenericBeanFactoryAccessor(beanFactory); 
  13.         IHelloWorld hw = gbf.getBean("helloWorld", IHelloWorld.class); 
  14.  
  15.         IHelloWorld hw2 = gbf.getBean("helloWorld"); 
  16.         log.info(hw.getContext()); 
  17.         log.info(hw2.getContext()); 
  18.         /** 
  19.          * 返回类型集合 
  20.          */ 
  21.  
  22.         log.info("=======DI容器中IHelloWorld.class接口实现的类==========="); 
  23.         Map<String ,IHelloWorld> map = gbf.getBeansOfType(IHelloWorld.class); 
  24.         for(String key:map.keySet()){ 
  25.             log.info(key+":"+map.get(key).getContext()); 
  26.         } 
  27.          
  28.  
  29.         Map<String ,Object> map2 = gbf.getBeansWithAnnotation(ForYou.class); 
  30.         log.info("=======DI容器中@ForYou注解的类==========="); 
  31.         for(String key:map2.keySet()){ 
  32.             log.info(key+":"+map2.get(key).getClass().getSimpleName()); 
  33.         } 
  34.     } 
  35.  

 

你可能感兴趣的:(spring,职场,休闲,DI容器泛型访问)