1 16 17 package org.springframework.beans.factory; 18 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.springframework.beans.BeansException; 27 import org.springframework.util.Assert; 28 import org.springframework.util.StringUtils; 29 30 44 public abstract class BeanFactoryUtils { 45 46 50 public static final String GENERATED_BEAN_NAME_SEPARATOR = "#"; 51 52 53 58 public static boolean isFactoryDereference(String name) { 59 return name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX); 60 } 61 62 67 public static String transformedBeanName(String name) { 68 Assert.notNull(name, "'name' must not be null"); 69 return (name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) ? 70 name.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()) : name); 71 } 72 73 80 public static String originalBeanName(String name) { 81 Assert.notNull(name, "'name' must not be null"); 82 int separatorIndex = name.indexOf(GENERATED_BEAN_NAME_SEPARATOR); 83 return (separatorIndex != -1 ? name.substring(0, separatorIndex) : name); 84 } 85 86 87 95 public static int countBeansIncludingAncestors(ListableBeanFactory lbf) { 96 return beanNamesIncludingAncestors(lbf).length; 97 } 98 99 105 public static String [] beanNamesIncludingAncestors(ListableBeanFactory lbf) { 106 return beanNamesForTypeIncludingAncestors(lbf, Object .class); 107 } 108 109 110 122 public static String [] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class type) { 123 Assert.notNull(lbf, "ListableBeanFactory must not be null"); 124 String [] result = lbf.getBeanNamesForType(type); 125 if (lbf instanceof HierarchicalBeanFactory) { 126 HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; 127 if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { 128 String [] parentResult = beanNamesForTypeIncludingAncestors( 129 (ListableBeanFactory) hbf.getParentBeanFactory(), type); 130 List resultList = new ArrayList (); 131 resultList.addAll(Arrays.asList(result)); 132 for (int i = 0; i < parentResult.length; i++) { 133 String beanName = parentResult[i]; 134 if (!resultList.contains(beanName) && !hbf.containsLocalBean(beanName)) { 135 resultList.add(beanName); 136 } 137 } 138 result = StringUtils.toStringArray(resultList); 139 } 140 } 141 return result; 142 } 143 144 164 public static String [] beanNamesForTypeIncludingAncestors( 165 ListableBeanFactory lbf, Class type, boolean includePrototypes, boolean allowEagerInit) { 166 167 Assert.notNull(lbf, "ListableBeanFactory must not be null"); 168 String [] result = lbf.getBeanNamesForType(type, includePrototypes, allowEagerInit); 169 if (lbf instanceof HierarchicalBeanFactory) { 170 HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; 171 if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { 172 String [] parentResult = beanNamesForTypeIncludingAncestors( 173 (ListableBeanFactory) hbf.getParentBeanFactory(), type, includePrototypes, allowEagerInit); 174 List resultList = new ArrayList (); 175 resultList.addAll(Arrays.asList(result)); 176 for (int i = 0; i < parentResult.length; i++) { 177 String beanName = parentResult[i]; 178 if (!resultList.contains(beanName) && !hbf.containsLocalBean(beanName)) { 179 resultList.add(beanName); 180 } 181 } 182 result = StringUtils.toStringArray(resultList); 183 } 184 } 185 return result; 186 } 187 188 200 public static Map beansOfTypeIncludingAncestors(ListableBeanFactory lbf, Class type) 201 throws BeansException { 202 203 Assert.notNull(lbf, "ListableBeanFactory must not be null"); 204 Map result = new HashMap (); 205 result.putAll(lbf.getBeansOfType(type)); 206 if (lbf instanceof HierarchicalBeanFactory) { 207 HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; 208 if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { 209 Map parentResult = beansOfTypeIncludingAncestors( 210 (ListableBeanFactory) hbf.getParentBeanFactory(), type); 211 for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) { 212 Map.Entry entry = (Map.Entry ) it.next(); 213 String beanName = (String ) entry.getKey(); 214 if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) { 215 result.put(beanName, entry.getValue()); 216 } 217 } 218 } 219 } 220 return result; 221 } 222 223 245 public static Map beansOfTypeIncludingAncestors( 246 ListableBeanFactory lbf, Class type, boolean includePrototypes, boolean allowEagerInit) 247 throws BeansException { 248 249 Assert.notNull(lbf, "ListableBeanFactory must not be null"); 250 Map result = new HashMap (); 251 result.putAll(lbf.getBeansOfType(type, includePrototypes, allowEagerInit)); 252 if (lbf instanceof HierarchicalBeanFactory) { 253 HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; 254 if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { 255 Map parentResult = beansOfTypeIncludingAncestors( 256 (ListableBeanFactory) hbf.getParentBeanFactory(), type, includePrototypes, allowEagerInit); 257 for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) { 258 Map.Entry entry = (Map.Entry ) it.next(); 259 String beanName = (String ) entry.getKey(); 260 if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) { 261 result.put(beanName, entry.getValue()); 262 } 263 } 264 } 265 } 266 return result; 267 } 268 269 270 289 public static Object beanOfTypeIncludingAncestors(ListableBeanFactory lbf, Class type) 290 throws BeansException { 291 292 Map beansOfType = beansOfTypeIncludingAncestors(lbf, type); 293 if (beansOfType.size() == 1) { 294 return beansOfType.values().iterator().next(); 295 } 296 else { 297 throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size()); 298 } 299 } 300 301 328 public static Object beanOfTypeIncludingAncestors( 329 ListableBeanFactory lbf, Class type, boolean includePrototypes, boolean allowEagerInit) 330 throws BeansException { 331 332 Map beansOfType = beansOfTypeIncludingAncestors(lbf, type, includePrototypes, allowEagerInit); 333 if (beansOfType.size() == 1) { 334 return beansOfType.values().iterator().next(); 335 } 336 else { 337 throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size()); 338 } 339 } 340 341 359 public static Object beanOfType(ListableBeanFactory lbf, Class type) throws BeansException { 360 Assert.notNull(lbf, "ListableBeanFactory must not be null"); 361 Map beansOfType = lbf.getBeansOfType(type); 362 if (beansOfType.size() == 1) { 363 return beansOfType.values().iterator().next(); 364 } 365 else { 366 throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size()); 367 } 368 } 369 370 396 public static Object beanOfType( 397 ListableBeanFactory lbf, Class type, boolean includePrototypes, boolean allowEagerInit) 398 throws BeansException { 399 400 Assert.notNull(lbf, "ListableBeanFactory must not be null"); 401 Map beansOfType = lbf.getBeansOfType(type, includePrototypes, allowEagerInit); 402 if (beansOfType.size() == 1) { 403 return beansOfType.values().iterator().next(); 404 } 405 else { 406 throw new NoSuchBeanDefinitionException(type, "expected single bean but found " + beansOfType.size()); 407 } 408 } 409 410 } 411 | Popular Tags |