Example usage for org.springframework.beans.factory.support ChildBeanDefinition getResourceDescription

List of usage examples for org.springframework.beans.factory.support ChildBeanDefinition getResourceDescription

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support ChildBeanDefinition getResourceDescription.

Prototype

@Override
@Nullable
public String getResourceDescription() 

Source Link

Document

Return a description of the resource that this bean definition came from (for the purpose of showing context in case of errors).

Usage

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

/**
 * Return a RootBeanDefinition for the given top-level bean, by merging with
 * the parent if the given bean's definition is a child bean definition.
 * @param beanName the name of the bean definition
 * @param bd the original bean definition (Root/ChildBeanDefinition)
 * @return a (potentially merged) RootBeanDefinition for the given bean
 * @throws BeanDefinitionStoreException in case of an invalid bean definition
 *///from  w w  w . j  av  a  2s  .com
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd)
        throws BeanDefinitionStoreException {

    if (bd instanceof RootBeanDefinition) {
        // Return root bean definition as-is.
        return (RootBeanDefinition) bd;
    }

    else if (bd instanceof ChildBeanDefinition) {
        // Child bean definition: needs to be merged with parent.
        ChildBeanDefinition cbd = (ChildBeanDefinition) bd;
        RootBeanDefinition pbd = null;
        try {
            if (!beanName.equals(cbd.getParentName())) {
                pbd = getMergedBeanDefinition(cbd.getParentName(), true);
            } else {
                if (getParentBeanFactory() instanceof AbstractBeanFactory) {
                    AbstractBeanFactory parentFactory = (AbstractBeanFactory) getParentBeanFactory();
                    pbd = parentFactory.getMergedBeanDefinition(cbd.getParentName(), true);
                } else {
                    throw new NoSuchBeanDefinitionException(cbd.getParentName(),
                            "Parent name '" + cbd.getParentName() + "' is equal to bean name '" + beanName
                                    + "': cannot be resolved without an AbstractBeanFactory parent");
                }
            }
        } catch (NoSuchBeanDefinitionException ex) {
            throw new BeanDefinitionStoreException(cbd.getResourceDescription(), beanName,
                    "Could not resolve parent bean definition '" + cbd.getParentName() + "'", ex);
        }

        // Deep copy with overridden values.
        RootBeanDefinition rbd = new RootBeanDefinition(pbd);
        rbd.overrideFrom(cbd);

        // Validate merged definition: mainly to prepare method overrides.
        try {
            rbd.validate();
        } catch (BeanDefinitionValidationException ex) {
            throw new BeanDefinitionStoreException(rbd.getResourceDescription(), beanName,
                    "Validation of bean definition failed", ex);
        }

        return rbd;
    }

    else {
        throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName,
                "Definition is neither a RootBeanDefinition nor a ChildBeanDefinition");
    }
}