public void preInstantiateSingletons() throws BeansException { if (logger.isDebugEnabled()) { logger.debug("Pre-instantiating singletons in factory [" + this + "]"); } // The superclass's variable by this name is declared private. String[] beanDefinitionNames = getBeanDefinitionNames(); String beanName = null; // Remember in case of an exception try { // for (Iterator it = this.beanDefinitionNames.iterator(); // it.hasNext();) { for (int i = 0; i < beanDefinitionNames.length; i++) { beanName = beanDefinitionNames[i]; if (!containsSingleton(beanName) && containsBeanDefinition(beanName)) { RootBeanDefinition bd = (RootBeanDefinition) getMergedBeanDefinition(beanName); 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. logger.error( "Failed to preinstantiate the singleton named " + beanName + ". Destroying all Spring beans.", ex); try { destroySingletons(); } catch (Exception ex2) { logger.error( "Pre-instantiating singletons failed, and couldn't destroy already created singletons: " + ex2, ex2); } throw ex; } }