Example usage for org.springframework.beans.factory.support RootBeanDefinition setDependsOn

List of usage examples for org.springframework.beans.factory.support RootBeanDefinition setDependsOn

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support RootBeanDefinition setDependsOn.

Prototype

@Override
public void setDependsOn(@Nullable String... dependsOn) 

Source Link

Document

Set the names of the beans that this bean depends on being initialized.

Usage

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

@Test
public void testDependsOnCycle() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setDependsOn("tb2");
    lbf.registerBeanDefinition("tb1", bd1);
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setDependsOn("tb1");
    lbf.registerBeanDefinition("tb2", bd2);
    try {/*from  w ww  .  j a  v a2  s  .com*/
        lbf.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        assertTrue(ex.getMessage().contains("Circular"));
        assertTrue(ex.getMessage().contains("'tb2'"));
        assertTrue(ex.getMessage().contains("'tb1'"));
    }
}

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

@Test
public void testImplicitDependsOnCycle() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setDependsOn("tb2");
    lbf.registerBeanDefinition("tb1", bd1);
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setDependsOn("tb3");
    lbf.registerBeanDefinition("tb2", bd2);
    RootBeanDefinition bd3 = new RootBeanDefinition(TestBean.class);
    bd3.setDependsOn("tb1");
    lbf.registerBeanDefinition("tb3", bd3);
    try {//from  w ww.  ja  v  a 2 s .c om
        lbf.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        assertTrue(ex.getMessage().contains("Circular"));
        assertTrue(ex.getMessage().contains("'tb3'"));
        assertTrue(ex.getMessage().contains("'tb1'"));
    }
}