Search in sources :

Example 96 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testFailsOnInnerBean.

@Test
public void testFailsOnInnerBean() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
    reader.loadBeanDefinitions(REFTYPES_CONTEXT);
    try {
        xbf.getBean("failsOnInnerBean");
    } catch (BeanCreationException ex) {
        // Check whether message contains outer bean name.
        ex.printStackTrace();
        assertTrue(ex.getMessage().contains("failsOnInnerBean"));
        assertTrue(ex.getMessage().contains("someMap"));
    }
    try {
        xbf.getBean("failsOnInnerBeanForConstructor");
    } catch (BeanCreationException ex) {
        // Check whether message contains outer bean name.
        ex.printStackTrace();
        assertTrue(ex.getMessage().contains("failsOnInnerBeanForConstructor"));
        assertTrue(ex.getMessage().contains("constructor argument"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 97 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testNonLenientDependencyMatching.

@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 {
        xbf.getBean("lenientDependencyTestBean");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        ex.printStackTrace();
        assertTrue(ex.getMostSpecificCause().getMessage().contains("Ambiguous"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 98 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class AnnotationDrivenEventListenerTests method eventListenerWorksWithCustomScope.

@Test
public void eventListenerWorksWithCustomScope() throws Exception {
    load(CustomScopeTestBean.class);
    CustomScope customScope = new CustomScope();
    this.context.getBeanFactory().registerScope("custom", customScope);
    CustomScopeTestBean proxy = this.context.getBean(CustomScopeTestBean.class);
    assertTrue("bean should be a cglib proxy", AopUtils.isCglibProxy(proxy));
    this.eventCollector.assertNoEventReceived(proxy.getId());
    this.context.publishEvent(new ContextRefreshedEvent(this.context));
    this.eventCollector.assertNoEventReceived(proxy.getId());
    customScope.active = false;
    this.context.publishEvent(new ContextRefreshedEvent(this.context));
    customScope.active = true;
    this.eventCollector.assertNoEventReceived(proxy.getId());
    TestEvent event = new TestEvent();
    this.context.publishEvent(event);
    this.eventCollector.assertEvent(proxy.getId(), event);
    this.eventCollector.assertTotalEventsCount(1);
    try {
        customScope.active = false;
        this.context.publishEvent(new TestEvent());
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) TestEvent(org.springframework.context.event.test.TestEvent) AnotherTestEvent(org.springframework.context.event.test.AnotherTestEvent) Test(org.junit.Test)

Example 99 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class BeanDefinitionValueResolver method resolveReference.

/**
	 * Resolve a reference to another bean in the factory.
	 */
private Object resolveReference(Object argName, RuntimeBeanReference ref) {
    try {
        String refName = ref.getBeanName();
        refName = String.valueOf(doEvaluate(refName));
        if (ref.isToParent()) {
            if (this.beanFactory.getParentBeanFactory() == null) {
                throw new BeanCreationException(this.beanDefinition.getResourceDescription(), this.beanName, "Can't resolve reference to bean '" + refName + "' in parent factory: no parent factory available");
            }
            return this.beanFactory.getParentBeanFactory().getBean(refName);
        } else {
            Object bean = this.beanFactory.getBean(refName);
            this.beanFactory.registerDependentBean(refName, this.beanName);
            return bean;
        }
    } catch (BeansException ex) {
        throw new BeanCreationException(this.beanDefinition.getResourceDescription(), this.beanName, "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException)

Example 100 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-framework by spring-projects.

the class BeanDefinitionValueResolver method resolveInnerBean.

/**
	 * Resolve an inner bean definition.
	 * @param argName the name of the argument that the inner bean is defined for
	 * @param innerBeanName the name of the inner bean
	 * @param innerBd the bean definition for the inner bean
	 * @return the resolved inner bean instance
	 */
private Object resolveInnerBean(Object argName, String innerBeanName, BeanDefinition innerBd) {
    RootBeanDefinition mbd = null;
    try {
        mbd = this.beanFactory.getMergedBeanDefinition(innerBeanName, innerBd, this.beanDefinition);
        // Check given bean name whether it is unique. If not already unique,
        // add counter - increasing the counter until the name is unique.
        String actualInnerBeanName = innerBeanName;
        if (mbd.isSingleton()) {
            actualInnerBeanName = adaptInnerBeanName(innerBeanName);
        }
        this.beanFactory.registerContainedBean(actualInnerBeanName, this.beanName);
        // Guarantee initialization of beans that the inner bean depends on.
        String[] dependsOn = mbd.getDependsOn();
        if (dependsOn != null) {
            for (String dependsOnBean : dependsOn) {
                this.beanFactory.registerDependentBean(dependsOnBean, actualInnerBeanName);
                this.beanFactory.getBean(dependsOnBean);
            }
        }
        // Actually create the inner bean instance now...
        Object innerBean = this.beanFactory.createBean(actualInnerBeanName, mbd, null);
        if (innerBean instanceof FactoryBean) {
            boolean synthetic = mbd.isSynthetic();
            return this.beanFactory.getObjectFromFactoryBean((FactoryBean<?>) innerBean, actualInnerBeanName, !synthetic);
        } else {
            return innerBean;
        }
    } catch (BeansException ex) {
        throw new BeanCreationException(this.beanDefinition.getResourceDescription(), this.beanName, "Cannot create inner bean '" + innerBeanName + "' " + (mbd != null && mbd.getBeanClassName() != null ? "of type [" + mbd.getBeanClassName() + "] " : "") + "while setting " + argName, ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) BeansException(org.springframework.beans.BeansException)

Aggregations

BeanCreationException (org.springframework.beans.factory.BeanCreationException)142 Test (org.junit.Test)30 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)18 BeansException (org.springframework.beans.BeansException)17 IOException (java.io.IOException)13 Map (java.util.Map)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 Test (org.junit.jupiter.api.Test)11 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)11 HashMap (java.util.HashMap)10 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 Bean (org.springframework.context.annotation.Bean)9 ArrayList (java.util.ArrayList)8 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 PostConstruct (javax.annotation.PostConstruct)7 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)7 Set (java.util.Set)6 HasId (org.apache.camel.spi.HasId)6 BeanCurrentlyInCreationException (org.springframework.beans.factory.BeanCurrentlyInCreationException)6 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)6