Example usage for org.springframework.beans.factory BeanIsNotAFactoryException BeanIsNotAFactoryException

List of usage examples for org.springframework.beans.factory BeanIsNotAFactoryException BeanIsNotAFactoryException

Introduction

In this page you can find the example usage for org.springframework.beans.factory BeanIsNotAFactoryException BeanIsNotAFactoryException.

Prototype

public BeanIsNotAFactoryException(String name, Class<?> actualType) 

Source Link

Document

Create a new BeanIsNotAFactoryException.

Usage

From source file:org.springframework.beans.factory.support.AbstractBeanFactory.java

/**
 * Get the object for the given shared bean, either the bean
 * instance itself or its created object in case of a FactoryBean.
 * @param name name that may include factory dereference prefix
 * @param beanInstance the shared bean instance
 * @return the singleton instance of the bean
 *//* ww w . java 2 s .  c  o m*/
protected Object getObjectForSharedInstance(String name, Object beanInstance) throws BeansException {
    String beanName = transformedBeanName(name);

    // Don't let calling code try to dereference the
    // bean factory if the bean isn't a factory.
    if (isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
        throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
    }

    // Now we have the bean instance, which may be a normal bean or a FactoryBean.
    // If it's a FactoryBean, we use it to create a bean instance, unless the
    // caller actually wants a reference to the factory.
    if (beanInstance instanceof FactoryBean) {
        if (!isFactoryDereference(name)) {
            // Return bean instance from factory.
            FactoryBean factory = (FactoryBean) beanInstance;
            if (logger.isDebugEnabled()) {
                logger.debug("Bean with name '" + beanName + "' is a factory bean");
            }
            try {
                beanInstance = factory.getObject();
            } catch (Exception ex) {
                throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
            }
            if (beanInstance == null) {
                throw new FactoryBeanNotInitializedException(beanName, "FactoryBean returned null object: "
                        + "probably not fully initialized (maybe due to circular bean reference)");
            }
        } else {
            // The user wants the factory itself.
            if (logger.isDebugEnabled()) {
                logger.debug("Calling code asked for FactoryBean instance for name '" + beanName + "'");
            }
        }
    }

    return beanInstance;
}