1 16 17 package org.springframework.beans.factory.generic; 18 19 import java.lang.annotation.Annotation ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.springframework.beans.BeansException; 24 import org.springframework.beans.factory.ListableBeanFactory; 25 import org.springframework.util.Assert; 26 27 38 public class GenericBeanFactoryAccessor { 39 40 43 private final ListableBeanFactory beanFactory; 44 45 46 49 public GenericBeanFactoryAccessor(ListableBeanFactory beanFactory) { 50 Assert.notNull(beanFactory, "Bean factory must not be null"); 51 this.beanFactory = beanFactory; 52 } 53 54 57 public final ListableBeanFactory getBeanFactory() { 58 return this.beanFactory; 59 } 60 61 62 65 public <T> T getBean(String name) throws BeansException { 66 return (T) getBeanFactory().getBean(name); 67 } 68 69 72 public <T> T getBean(String name, Class <T> requiredType) throws BeansException { 73 return (T) getBeanFactory().getBean(name, requiredType); 74 } 75 76 79 public <T> Map <String , T> getBeansOfType(Class <T> type) throws BeansException { 80 return getBeanFactory().getBeansOfType(type); 81 } 82 83 86 public <T> Map <String , T> getBeansOfType(Class <T> type, boolean includePrototypes, boolean allowEagerInit) 87 throws BeansException { 88 89 return getBeanFactory().getBeansOfType(type, includePrototypes, allowEagerInit); 90 } 91 92 98 public Map <String , Object > getBeansWithAnnotation(Class <? extends Annotation > annotationType) { 99 Map <String , Object > results = new HashMap <String , Object >(); 100 for (String beanName : getBeanFactory().getBeanNamesForType(null)) { 101 Class beanType = getBeanFactory().getType(beanName); 102 if (beanType.getAnnotation(annotationType) != null) { 103 results.put(beanName, getBeanFactory().getBean(beanName)); 104 } 105 } 106 return results; 107 } 108 109 } 110 | Popular Tags |