Example usage for org.springframework.beans.factory.support DefaultListableBeanFactory preInstantiateSingletons

List of usage examples for org.springframework.beans.factory.support DefaultListableBeanFactory preInstantiateSingletons

Introduction

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

Prototype

@Override
    public void preInstantiateSingletons() throws BeansException 

Source Link

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 www .  j  a va2s . 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 {// w w w  .  j a  v  a2 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'"));
    }
}

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

@Test
public void testExtensiveCircularReference() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    for (int i = 0; i < 1000; i++) {
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValue(//from  w ww.j  a  v a  2 s .c o m
                new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
        RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
        bd.setPropertyValues(pvs);
        lbf.registerBeanDefinition("bean" + i, bd);
    }
    lbf.preInstantiateSingletons();
    for (int i = 0; i < 1000; i++) {
        TestBean bean = (TestBean) lbf.getBean("bean" + i);
        TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0));
        assertTrue(bean.getSpouse() == otherBean);
    }
}

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

@Test
public void testCircularReferenceThroughAutowiring() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyBean.class);
    bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    lbf.registerBeanDefinition("test", bd);
    try {/*from  w w  w. jav  a 2s .c om*/
        lbf.preInstantiateSingletons();
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException expected) {
    }
}

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

@Test
public void testCircularReferenceThroughFactoryBeanAutowiring() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class);
    bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    lbf.registerBeanDefinition("test", bd);
    try {//ww  w  . j a  va 2 s . c  o m
        lbf.preInstantiateSingletons();
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException expected) {
    }
}

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

@Test
public void testAvoidCircularReferenceThroughAutowiring() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyFactoryBean.class);
    bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    lbf.registerBeanDefinition("test", bd);
    RootBeanDefinition bd2 = new RootBeanDefinition(String.class);
    bd2.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    lbf.registerBeanDefinition("string", bd2);
    lbf.preInstantiateSingletons();
}

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

@Test
public void testConstructorDependencyWithClassResolution() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class);
    bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.String");
    lbf.registerBeanDefinition("test", bd);
    lbf.preInstantiateSingletons();
}

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

@Test
public void testConstructorDependencyWithUnresolvableClass() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class);
    bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.Strin");
    lbf.registerBeanDefinition("test", bd);
    try {/* ww  w.j  a  v  a2s . c  om*/
        lbf.preInstantiateSingletons();
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException expected) {
        assertTrue(expected.toString().contains("java.lang.Strin"));
    }
}

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

@Test
public void testPrototypeFactoryBeanNotEagerlyCalled() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class));
    lbf.preInstantiateSingletons();
}

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

@Test
public void testLazyInitFactory() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(LazyInitFactory.class));
    lbf.preInstantiateSingletons();
    LazyInitFactory factory = (LazyInitFactory) lbf.getBean("&test");
    assertFalse(factory.initialized);/* w  ww.j a  va  2 s.c o m*/
}