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

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

Introduction

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

Prototype

public BeanDefinitionStoreException(@Nullable String resourceDescription, String beanName, String msg,
        @Nullable Throwable cause) 

Source Link

Document

Create a new BeanDefinitionStoreException.

Usage

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

/**
 * Central method of this class: creates a bean instance,
 * populates the bean instance, applies post-processors, etc.
 * @see #doCreateBean/*w ww.  j  av  a  2s.c om*/
 */
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
        throws BeanCreationException {

    if (logger.isTraceEnabled()) {
        logger.trace("Creating instance of bean '" + beanName + "'");
    }
    RootBeanDefinition mbdToUse = mbd;

    // Make sure bean class is actually resolved at this point, and
    // clone the bean definition in case of a dynamically resolved Class
    // which cannot be stored in the shared merged bean definition.
    Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
    if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
        mbdToUse = new RootBeanDefinition(mbd);
        mbdToUse.setBeanClass(resolvedClass);
    }

    // Prepare method overrides.
    try {
        mbdToUse.prepareMethodOverrides();
    } catch (BeanDefinitionValidationException ex) {
        throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName,
                "Validation of method overrides failed", ex);
    }

    try {
        // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
        Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
        if (bean != null) {
            return bean;
        }
    } catch (Throwable ex) {
        throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                "BeanPostProcessor before instantiation of bean failed", ex);
    }

    try {
        Object beanInstance = doCreateBean(beanName, mbdToUse, args);
        if (logger.isTraceEnabled()) {
            logger.trace("Finished creating instance of bean '" + beanName + "'");
        }
        return beanInstance;
    } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
        // A previously detected exception with proper bean creation context already,
        // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
        throw ex;
    } catch (Throwable ex) {
        throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                "Unexpected exception during bean creation", ex);
    }
}

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
 *///  w ww  .  j a v  a 2  s. c  o m
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");
    }
}

From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java

/**
 * Parse the BeanDefinition itself, without regard to name or aliases.
 *//*from   w w  w  .  ja v a 2s  . c o m*/
protected BeanDefinition parseBeanDefinitionElement(Element ele, String beanName)
        throws BeanDefinitionStoreException {

    String className = null;
    if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
        className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
    }
    String parent = null;
    if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
        parent = ele.getAttribute(PARENT_ATTRIBUTE);
    }

    try {
        ConstructorArgumentValues cargs = parseConstructorArgElements(ele, beanName);
        MutablePropertyValues pvs = parsePropertyElements(ele, beanName);

        AbstractBeanDefinition bd = BeanDefinitionReaderUtils.createBeanDefinition(className, parent, cargs,
                pvs, getBeanDefinitionReader().getBeanClassLoader());

        if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
            String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
            bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, BEAN_NAME_DELIMITERS));
        }

        if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
            bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
        }
        if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
            bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
        }

        String dependencyCheck = ele.getAttribute(DEPENDENCY_CHECK_ATTRIBUTE);
        if (DEFAULT_VALUE.equals(dependencyCheck)) {
            dependencyCheck = getDefaultDependencyCheck();
        }
        bd.setDependencyCheck(getDependencyCheck(dependencyCheck));

        String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
        if (DEFAULT_VALUE.equals(autowire)) {
            autowire = getDefaultAutowire();
        }
        bd.setAutowireMode(getAutowireMode(autowire));

        if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
            String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
            if (!"".equals(initMethodName)) {
                bd.setInitMethodName(initMethodName);
            }
        } else {
            if (getDefaultInitMethod() != null) {
                bd.setInitMethodName(getDefaultInitMethod());
                bd.setEnforceInitMethod(false);
            }
        }

        if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
            String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
            if (!"".equals(destroyMethodName)) {
                bd.setDestroyMethodName(destroyMethodName);
            }
        } else {
            if (getDefaultDestroyMethod() != null) {
                bd.setDestroyMethodName(getDefaultDestroyMethod());
                bd.setEnforceDestroyMethod(false);
            }
        }

        parseLookupOverrideSubElements(ele, beanName, bd.getMethodOverrides());
        parseReplacedMethodSubElements(ele, beanName, bd.getMethodOverrides());

        bd.setResourceDescription(getResource().getDescription());

        if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
            bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
        }

        if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
            bd.setSingleton(TRUE_VALUE.equals(ele.getAttribute(SINGLETON_ATTRIBUTE)));
        }

        String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
        if (DEFAULT_VALUE.equals(lazyInit) && bd.isSingleton()) {
            // Just apply default to singletons, as lazy-init has no meaning for prototypes.
            lazyInit = getDefaultLazyInit();
        }
        bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

        return bd;
    }

    catch (BeanDefinitionStoreException ex) {
        throw ex;
    } catch (ClassNotFoundException ex) {
        throw new BeanDefinitionStoreException(getResource(), beanName,
                "Bean class [" + className + "] not found", ex);
    } catch (NoClassDefFoundError err) {
        throw new BeanDefinitionStoreException(getResource(), beanName,
                "Class that bean class [" + className + "] depends on not found", err);
    } catch (Throwable ex) {
        throw new BeanDefinitionStoreException(getResource(), beanName,
                "Unexpected failure during bean definition parsing", ex);
    }
}

From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java

/**
 * Parse a value, ref or collection sub-element of a property or
 * constructor-arg element.//from   w  w  w . j  ava 2 s .  c o  m
 * @param ele subelement of property element; we don't know which yet
 */
protected Object parsePropertySubElement(Element ele, String beanName) throws BeanDefinitionStoreException {
    if (ele.getTagName().equals(BEAN_ELEMENT)) {
        try {
            return parseBeanDefinitionElement(ele, true);
        } catch (BeanDefinitionStoreException ex) {
            throw new BeanDefinitionStoreException(getResource(), beanName,
                    "Could not parse inner bean definition", ex);
        }
    } else if (ele.getTagName().equals(REF_ELEMENT)) {
        // A generic reference to any name of any bean.
        String refName = ele.getAttribute(BEAN_REF_ATTRIBUTE);
        boolean toParent = false;
        if (!StringUtils.hasLength(refName)) {
            // A reference to the id of another bean in the same XML file.
            refName = ele.getAttribute(LOCAL_REF_ATTRIBUTE);
            if (!StringUtils.hasLength(refName)) {
                // A reference to the id of another bean in a parent context.
                refName = ele.getAttribute(PARENT_REF_ATTRIBUTE);
                toParent = true;
                if (!StringUtils.hasLength(refName)) {
                    throw new BeanDefinitionStoreException(getResource(), beanName,
                            "'bean', 'local' or 'parent' is required for <ref> element");
                }
            }
        }
        if (!StringUtils.hasText(refName)) {
            throw new BeanDefinitionStoreException(getResource(), beanName,
                    "<ref> element contains empty target attribute");
        }
        return new RuntimeBeanReference(refName, toParent);
    } else if (ele.getTagName().equals(IDREF_ELEMENT)) {
        // A generic reference to any name of any bean.
        String beanRef = ele.getAttribute(BEAN_REF_ATTRIBUTE);
        if (!StringUtils.hasLength(beanRef)) {
            // A reference to the id of another bean in the same XML file.
            beanRef = ele.getAttribute(LOCAL_REF_ATTRIBUTE);
            if (!StringUtils.hasLength(beanRef)) {
                throw new BeanDefinitionStoreException(getResource(), beanName,
                        "Either 'bean' or 'local' is required for <idref> element");
            }
        }
        return beanRef;
    } else if (ele.getTagName().equals(VALUE_ELEMENT)) {
        // It's a literal value.
        String value = DomUtils.getTextValue(ele);
        if (ele.hasAttribute(TYPE_ATTRIBUTE)) {
            String typeClassName = ele.getAttribute(TYPE_ATTRIBUTE);
            try {
                Class typeClass = ClassUtils.forName(typeClassName,
                        this.beanDefinitionReader.getBeanClassLoader());
                return new TypedStringValue(value, typeClass);
            } catch (ClassNotFoundException ex) {
                throw new BeanDefinitionStoreException(getResource(), beanName,
                        "Type class [" + typeClassName + "] not found for <value> element", ex);
            }
        }
        return value;
    } else if (ele.getTagName().equals(NULL_ELEMENT)) {
        // It's a distinguished null value.
        return null;
    } else if (ele.getTagName().equals(LIST_ELEMENT)) {
        return parseListElement(ele, beanName);
    } else if (ele.getTagName().equals(SET_ELEMENT)) {
        return parseSetElement(ele, beanName);
    } else if (ele.getTagName().equals(MAP_ELEMENT)) {
        return parseMapElement(ele, beanName);
    } else if (ele.getTagName().equals(PROPS_ELEMENT)) {
        return parsePropsElement(ele, beanName);
    }
    throw new BeanDefinitionStoreException(getResource(), beanName,
            "Unknown property sub-element: <" + ele.getTagName() + ">");
}