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

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

Introduction

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

Prototype

@Nullable
public String getBeanName() 

Source Link

Document

Return the name of the bean requested, if any.

Usage

From source file:com.googlecode.ehcache.annotations.integration.ConfigurationFailureTest.java

/**
 * Test verifies behavior when no {@link Ehcache} is defined for
 * the cacheName attribute on the  {@link Cacheable} annotated method AND
 * 'createMissingCaches' is false./*  ww  w  .j a va  2 s  . c o m*/
 */
@Test
public void testNoCache() {
    try {
        new ClassPathXmlApplicationContext("/noCacheTestContext.xml");
        Assert.fail("Test should have failed with no Cache defined");
    } catch (BeanCreationException bce) {
        Assert.assertEquals("missingCacheNameImpl", bce.getBeanName());
        final CacheNotFoundException cnfe = (CacheNotFoundException) bce.getCause();
        Assert.assertEquals("nonexistent", cnfe.getCacheName());
    }
}

From source file:com.googlecode.ehcache.annotations.integration.ConfigurationFailureTest.java

/**
 * Test verifies behavior when no {@link CacheKeyGenerator} is defined for
 * the cacheKeyGenerator attribute on the {@link Cacheable} annotated method.
 *///from   w  ww  .j a  va  2  s  .  c o  m
@Test
public void testNoCacheKeyGenerator() {
    try {
        new ClassPathXmlApplicationContext("/noCacheKeyGeneratorTestContext.xml");
        Assert.fail("Test should have failed with no CacheKeyGenerator defined");
    } catch (BeanCreationException bce) {
        Assert.assertEquals("cacheConfigurationTestImpl", bce.getBeanName());
    }
}

From source file:ru.apertum.qsystem.server.Spring.java

private Spring() {
    try {//  www .  j a  v  a 2s.  co  m
        factory = new ClassPathXmlApplicationContext("/ru/apertum/qsystem/spring/qsContext.xml");
    } catch (BeanCreationException ex) {
        throw new ServerException(
                " ?? ??- ? ?: \""
                        + ex.getCause().getMessage() + "\"\n" + " ?  \"" + ex.getBeanName()
                        + "\"" + "  : \"" + ex.getCause().getMessage()
                        + "\"\n" + ex);
    } catch (BeansException ex) {
        throw new ServerException(
                " ??- ? ?: \""
                        + ex.getCause().getMessage() + "\"\n" + "  : \""
                        + ex.getCause().getMessage() + "\"\n" + ex);
    } catch (Exception ex) {
        throw new ServerException(
                " ?? ? ?: " + ex);
    }
    //sessionFactory = factory.getBean("mySessionFactory", SessionFactoryImpl.class);
    //ht = new HibernateTemplate(sessionFactory);

    final ComboPooledDataSource bds = (ComboPooledDataSource) factory.getBean("c3p0DataSource");
    driverClassName = bds.getDriverClass();
    url = bds.getJdbcUrl();
    username = bds.getUser();
    password = bds.getPassword();
}

From source file:org.archive.crawler.framework.CrawlJob.java

/**
 * Return a short useful message for common BeansExceptions. 
 * @param ex BeansException// w  w w .j av  a 2 s. com
 * @return String short descriptive message
 */
protected String shortMessage(BeansException ex) {
    if (ex instanceof NoSuchBeanDefinitionException) {
        NoSuchBeanDefinitionException nsbde = (NoSuchBeanDefinitionException) ex;
        return "Missing required bean: "
                + (nsbde.getBeanName() != null ? "\"" + nsbde.getBeanName() + "\" " : "")
                + (nsbde.getBeanType() != null ? "\"" + nsbde.getBeanType() + "\" " : "");
    }
    if (ex instanceof BeanCreationException) {
        BeanCreationException bce = (BeanCreationException) ex;
        return bce.getBeanName() == null ? "" : "Can't create bean '" + bce.getBeanName() + "'";
    }
    return ex.getMessage().replace('\n', ' ');
}

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/*ww w  . j  a v a2 s  . com*/
 */
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.DefaultListableBeanFactoryTests.java

@Test
public void testBeanDefinitionWithInterface() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(ITestBean.class));
    try {/*from   www.j a va  2s.  c o m*/
        lbf.getBean("test");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        assertEquals("test", ex.getBeanName());
        assertTrue(ex.getMessage().toLowerCase().contains("interface"));
    }
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testBeanDefinitionWithAbstractClass() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(AbstractBeanFactory.class));
    try {//from   w ww. ja  v a  2  s.co m
        lbf.getBean("test");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        assertEquals("test", ex.getBeanName());
        assertTrue(ex.getMessage().toLowerCase().contains("abstract"));
    }
}

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

/**
 * Configure the bean instance./*from  w ww.ja  va  2 s  .co m*/
 * <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 that if a custom initializer throws an exception, it's handled correctly
 *///from w w  w .jav a 2  s  .  co m
@Test
public void testInitMethodThrowsException() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
    try {
        xbf.getBean("init-method2");
        fail();
    } catch (BeanCreationException ex) {
        assertTrue(ex.getResourceDescription().indexOf("initializers.xml") != -1);
        assertEquals("init-method2", ex.getBeanName());
        assertTrue(ex.getCause() instanceof IOException);
    }
}