List of usage examples for org.springframework.beans.factory BeanCurrentlyInCreationException BeanCurrentlyInCreationException
public BeanCurrentlyInCreationException(String beanName)
From source file:org.springframework.beans.factory.support.AbstractBeanFactory.java
/** * Return an instance, which may be shared or independent, of the specified bean. * @param name the name of the bean to retrieve * @param requiredType the required type of the bean to retrieve * @param args arguments to use if creating a prototype using explicit arguments to a * static factory method. It is invalid to use a non-null args value in any other case. * @return an instance of the bean// w ww .j a va 2s. c o m * @throws BeansException if the bean could not be created */ public Object getBean(String name, Class requiredType, Object[] args) throws BeansException { String beanName = transformedBeanName(name); Object bean = null; // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = null; synchronized (this.singletonCache) { sharedInstance = this.singletonCache.get(beanName); } if (sharedInstance != null) { if (isSingletonCurrentlyInCreation(beanName)) { if (logger.isDebugEnabled()) { logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } } else { if (logger.isDebugEnabled()) { logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); } } bean = getObjectForSharedInstance(name, sharedInstance); } else { // Fail if we're already creating this singleton instance: // We're assumably within a circular reference. if (isSingletonCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // Check if bean definition exists in this factory. if (getParentBeanFactory() != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. if (getParentBeanFactory() instanceof AbstractBeanFactory) { // Delegation to parent with args only possible for AbstractBeanFactory. return ((AbstractBeanFactory) getParentBeanFactory()).getBean(name, requiredType, args); } else if (args == null) { // No args -> delegate to standard getBean method. return getParentBeanFactory().getBean(name, requiredType); } else { throw new NoSuchBeanDefinitionException(beanName, "Cannot delegate to parent BeanFactory because it does not supported passed-in arguments"); } } RootBeanDefinition mergedBeanDefinition = getMergedBeanDefinition(beanName, false); checkMergedBeanDefinition(mergedBeanDefinition, beanName, requiredType, args); // Create bean instance. if (mergedBeanDefinition.isSingleton()) { synchronized (this.singletonCache) { // Re-check singleton cache within synchronized block. sharedInstance = this.singletonCache.get(beanName); if (sharedInstance == null) { if (logger.isDebugEnabled()) { logger.debug("Creating shared instance of singleton bean '" + beanName + "'"); } this.currentlyInCreation.add(beanName); try { sharedInstance = createBean(beanName, mergedBeanDefinition, args); addSingleton(beanName, sharedInstance); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. destroyDisposableBean(beanName); throw ex; } finally { this.currentlyInCreation.remove(beanName); } } } bean = getObjectForSharedInstance(name, sharedInstance); } else { // It's a prototype -> create a new instance. bean = createBean(beanName, mergedBeanDefinition, args); } } // Check if required type matches the type of the actual bean instance. if (requiredType != null && !requiredType.isAssignableFrom(bean.getClass())) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return bean; }
From source file:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.java
/** * Callback before singleton creation./* w w w. j a v a 2s . c o m*/ * <p>The default implementation register the singleton as currently in creation. * @param beanName the name of the singleton about to be created * @see #isSingletonCurrentlyInCreation */ protected void beforeSingletonCreation(String beanName) { if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } }