Search in sources :

Example 66 with BeansException

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

the class XmlListableBeanFactoryTests method setUp.

@Before
public void setUp() {
    parent = new DefaultListableBeanFactory();
    Map m = new HashMap();
    m.put("name", "Albert");
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setPropertyValues(new MutablePropertyValues(m));
    parent.registerBeanDefinition("father", bd1);
    m = new HashMap();
    m.put("name", "Roderick");
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPropertyValues(new MutablePropertyValues(m));
    parent.registerBeanDefinition("rod", bd2);
    this.factory = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
    this.factory.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
            if (bean instanceof TestBean) {
                ((TestBean) bean).setPostProcessed(true);
            }
            if (bean instanceof DummyFactory) {
                ((DummyFactory) bean).setPostProcessed(true);
            }
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
            return bean;
        }
    });
    this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
    this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
//this.factory.preInstantiateSingletons();
}
Also used : HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(org.springframework.beans.BeansException) Before(org.junit.Before)

Example 67 with BeansException

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

the class XmlBeanFactoryTests method testWithDuplicateNameInAlias.

@Test
public void testWithDuplicateNameInAlias() throws Exception {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    try {
        new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(TEST_WITH_DUP_NAME_IN_ALIAS_CONTEXT);
        fail("Duplicate name not detected");
    } catch (BeansException e) {
        assertTrue(e.getMessage().contains("Bean name 'foo'"));
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Example 68 with BeansException

use of org.springframework.beans.BeansException in project spring-boot by spring-projects.

the class ConfigurationPropertiesBindingPostProcessorTests method configurationPropertiesWithFactoryBean.

@Test
public void configurationPropertiesWithFactoryBean() throws Exception {
    ConfigurationPropertiesWithFactoryBean.factoryBeanInit = false;
    this.context = new AnnotationConfigApplicationContext() {

        @Override
        protected void onRefresh() throws BeansException {
            assertThat(ConfigurationPropertiesWithFactoryBean.factoryBeanInit).as("Init too early").isFalse();
            super.onRefresh();
        }
    };
    this.context.register(ConfigurationPropertiesWithFactoryBean.class);
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(FactoryBeanTester.class);
    beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
    this.context.registerBeanDefinition("test", beanDefinition);
    this.context.refresh();
    assertThat(ConfigurationPropertiesWithFactoryBean.factoryBeanInit).as("No init").isTrue();
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Example 69 with BeansException

use of org.springframework.beans.BeansException 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 70 with BeansException

use of org.springframework.beans.BeansException 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

BeansException (org.springframework.beans.BeansException)126 Test (org.junit.Test)24 ApplicationContext (org.springframework.context.ApplicationContext)22 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)18 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)13 BeanCreationException (org.springframework.beans.factory.BeanCreationException)11 HashMap (java.util.HashMap)10 BeanWrapper (org.springframework.beans.BeanWrapper)10 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)10 MalformedURLException (java.net.MalformedURLException)9 File (java.io.File)8 IOException (java.io.IOException)8 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)8 List (java.util.List)7 Method (java.lang.reflect.Method)6 LinkedHashSet (java.util.LinkedHashSet)6