Example usage for org.springframework.beans.factory BeanCreationException getMostSpecificCause

List of usage examples for org.springframework.beans.factory BeanCreationException getMostSpecificCause

Introduction

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

Prototype

public Throwable getMostSpecificCause() 

Source Link

Document

Retrieve the most specific cause of this exception, that is, either the innermost cause (root cause) or this exception itself.

Usage

From source file:org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.java

/**
 * Find all eligible Advisor beans in the current bean factory,
 * ignoring FactoryBeans and excluding beans that are currently in creation.
 * @return the list of {@link org.springframework.aop.Advisor} beans
 * @see #isEligibleBean//from ww  w.jav a2 s . c  o m
 */
public List<Advisor> findAdvisorBeans() {
    // Determine list of advisor bean names, if not cached already.
    String[] advisorNames = null;
    synchronized (this) {
        advisorNames = this.cachedAdvisorBeanNames;
        if (advisorNames == null) {
            // Do not initialize FactoryBeans here: We need to leave all regular beans
            // uninitialized to let the auto-proxy creator apply to them!
            advisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Advisor.class,
                    true, false);
            this.cachedAdvisorBeanNames = advisorNames;
        }
    }
    if (advisorNames.length == 0) {
        return new LinkedList<>();
    }

    List<Advisor> advisors = new LinkedList<>();
    for (String name : advisorNames) {
        if (isEligibleBean(name)) {
            if (this.beanFactory.isCurrentlyInCreation(name)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Skipping currently created advisor '" + name + "'");
                }
            } else {
                try {
                    advisors.add(this.beanFactory.getBean(name, Advisor.class));
                } catch (BeanCreationException ex) {
                    Throwable rootCause = ex.getMostSpecificCause();
                    if (rootCause instanceof BeanCurrentlyInCreationException) {
                        BeanCreationException bce = (BeanCreationException) rootCause;
                        String bceBeanName = bce.getBeanName();
                        if (bceBeanName != null && this.beanFactory.isCurrentlyInCreation(bceBeanName)) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Skipping advisor '" + name
                                        + "' with dependency on currently created bean: " + ex.getMessage());
                            }
                            // Ignore: indicates a reference back to the bean we're trying to advise.
                            // We want to find advisors other than the currently created bean itself.
                            continue;
                        }
                    }
                    throw ex;
                }
            }
        }
    }
    return advisors;
}

From source file:org.springframework.beans.factory.wiring.BeanConfigurerSupport.java

/**
 * Configure the bean instance.//  w w w .ja  va2s  . c  om
 * <p>Subclasses can override this to provide custom configuration logic.
 * Typically called by an aspect, for all bean instances matched by a pointcut.
 * @param beanInstance the bean instance to configure (must <b>not</b> be {@code null})
 */
public void configureBean(Object beanInstance) {
    if (this.beanFactory == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("BeanFactory has not been set on " + ClassUtils.getShortName(getClass()) + ": "
                    + "Make sure this configurer runs in a Spring container. Unable to configure bean of type ["
                    + ClassUtils.getDescriptiveType(beanInstance) + "]. Proceeding without injection.");
        }
        return;
    }

    BeanWiringInfoResolver bwiResolver = this.beanWiringInfoResolver;
    Assert.state(bwiResolver != null, "No BeanWiringInfoResolver available");
    BeanWiringInfo bwi = bwiResolver.resolveWiringInfo(beanInstance);
    if (bwi == null) {
        // Skip the bean if no wiring info given.
        return;
    }

    ConfigurableListableBeanFactory beanFactory = this.beanFactory;
    Assert.state(beanFactory != null, "No BeanFactory available");
    try {
        if (bwi.indicatesAutowiring() || (bwi.isDefaultBeanName() && bwi.getBeanName() != null
                && !beanFactory.containsBean(bwi.getBeanName()))) {
            // Perform autowiring (also applying standard factory / post-processor callbacks).
            beanFactory.autowireBeanProperties(beanInstance, bwi.getAutowireMode(), bwi.getDependencyCheck());
            beanFactory.initializeBean(beanInstance, bwi.getBeanName());
        } else {
            // Perform explicit wiring based on the specified bean definition.
            beanFactory.configureBean(beanInstance, bwi.getBeanName());
        }
    } catch (BeanCreationException ex) {
        Throwable rootCause = ex.getMostSpecificCause();
        if (rootCause instanceof BeanCurrentlyInCreationException) {
            BeanCreationException bce = (BeanCreationException) rootCause;
            String bceBeanName = bce.getBeanName();
            if (bceBeanName != null && beanFactory.isCurrentlyInCreation(bceBeanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to create target bean '" + bce.getBeanName()
                            + "' while configuring object of type [" + beanInstance.getClass().getName()
                            + "] - probably due to a circular reference. This is a common startup situation "
                            + "and usually not fatal. Proceeding without injection. Original exception: " + ex);
                }
                return;
            }
        }
        throw ex;
    }
}

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

@Test
public void testNonLenientDependencyMatching() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf.getBeanDefinition("lenientDependencyTestBean");
    bd.setLenientConstructorResolution(false);
    try {/* w  ww  . ja  v a2  s.c  o  m*/
        xbf.getBean("lenientDependencyTestBean");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
    }
}

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

@Test
public void testNonLenientDependencyMatchingFactoryMethod() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    AbstractBeanDefinition bd = (AbstractBeanDefinition) xbf
            .getBeanDefinition("lenientDependencyTestBeanFactoryMethod");
    bd.setLenientConstructorResolution(false);
    try {/*  w  w  w. j  a va2s.c  om*/
        xbf.getBean("lenientDependencyTestBeanFactoryMethod");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
    }
}