public void preInstantiateSingletons() throws BeansException { if (logger.isInfoEnabled()) { logger.info("Pre-instantiating singletons in factory [" + this + "]"); } try { for (Iterator it = this.beanDefinitionNames.iterator(); it.hasNext(); ) { String beanName = (String) it.next(); if (containsBeanDefinition(beanName)) { RootBeanDefinition bd = getMergedBeanDefinition(beanName, false); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (bd.hasBeanClass() && FactoryBean.class.isAssignableFrom(bd.getBeanClass())) { FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName); if (factory.isSingleton()) { getBean(beanName); } } else { getBean(beanName); } } } } } catch (BeansException ex) { // destroy already created singletons to avoid dangling resources try { destroySingletons(); } catch (Throwable ex2) { logger.error( "Pre-instantiating singletons failed, " + "and couldn't destroy already created singletons", ex2); } throw ex; } }
public String[] getBeanNamesForType( Class type, boolean includePrototypes, boolean includeFactoryBeans) { List result = new ArrayList(); // Check all bean definitions. for (Iterator it = this.beanDefinitionNames.iterator(); it.hasNext(); ) { String beanName = (String) it.next(); RootBeanDefinition rbd = getMergedBeanDefinition(beanName, false); // Only check bean definition if it is complete. if (!rbd.isAbstract()) { // In case of FactoryBean, match object created by FactoryBean. boolean isFactoryBean = rbd.hasBeanClass() && FactoryBean.class.isAssignableFrom(rbd.getBeanClass()); if (isFactoryBean || rbd.getFactoryBeanName() != null) { if (includeFactoryBeans && (includePrototypes || isSingleton(beanName)) && isBeanTypeMatch(beanName, type)) { result.add(beanName); // Match found for this bean: do not match FactoryBean itself anymore. continue; } // We're done for anything but a full FactoryBean. if (!isFactoryBean) { continue; } // In case of FactoryBean, try to match FactoryBean itself next. beanName = FACTORY_BEAN_PREFIX + beanName; } // Match raw bean instance (might be raw FactoryBean). if ((includePrototypes || rbd.isSingleton()) && isBeanTypeMatch(beanName, type)) { result.add(beanName); } } } // Check singletons too, to catch manually registered singletons. String[] singletonNames = getSingletonNames(); for (int i = 0; i < singletonNames.length; i++) { String beanName = singletonNames[i]; // Only check if manually registered. if (!containsBeanDefinition(beanName)) { // In case of FactoryBean, match object created by FactoryBean. if (isFactoryBean(beanName)) { if (includeFactoryBeans && (includePrototypes || isSingleton(beanName)) && isBeanTypeMatch(beanName, type)) { result.add(beanName); // Match found for this bean: do not match FactoryBean itself anymore. continue; } // In case of FactoryBean, try to match FactoryBean itself next. beanName = FACTORY_BEAN_PREFIX + beanName; } // Match raw bean instance (might be raw FactoryBean). if (isBeanTypeMatch(beanName, type)) { result.add(beanName); } } } return (String[]) result.toArray(new String[result.size()]); }