Search in sources :

Example 91 with BeanCreationException

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

the class XmlBeanFactoryTests method testRelatedCausesFromConstructorResolution.

@Test
public void testRelatedCausesFromConstructorResolution() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    try {
        xbf.getBean("rod2Accessor");
    } catch (BeanCreationException ex) {
        assertTrue(ex.toString().indexOf("touchy") != -1);
        ex.printStackTrace();
        assertNull(ex.getRelatedCauses());
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 92 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 93 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 94 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 95 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)

Aggregations

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