Search in sources :

Example 11 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testPrototypeFactoryBeanNotEagerlyCalledInCaseOfBeanClassName.

@Test
public void testPrototypeFactoryBeanNotEagerlyCalledInCaseOfBeanClassName() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName(), null, null));
    lbf.preInstantiateSingletons();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 12 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testInitSecurityAwarePrototypeBean.

@SuppressWarnings("unchecked")
@Test
public void testInitSecurityAwarePrototypeBean() {
    final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class);
    bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
    bd.setInitMethodName("init");
    lbf.registerBeanDefinition("test", bd);
    final Subject subject = new Subject();
    subject.getPrincipals().add(new TestPrincipal("user1"));
    TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject, new PrivilegedAction() {

        @Override
        public Object run() {
            return lbf.getBean("test");
        }
    }, null);
    assertNotNull(bean);
    assertEquals("user1", bean.getUserName());
}
Also used : PrivilegedAction(java.security.PrivilegedAction) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 13 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testDoubleArrayConstructorWithAutowiring.

@Test
public void testDoubleArrayConstructorWithAutowiring() throws MalformedURLException {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerSingleton("integer1", new Integer(4));
    bf.registerSingleton("integer2", new Integer(5));
    bf.registerSingleton("resource1", new UrlResource("http://localhost:8080"));
    bf.registerSingleton("resource2", new UrlResource("http://localhost:9090"));
    RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class);
    rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    bf.registerBeanDefinition("arrayBean", rbd);
    ArrayBean ab = (ArrayBean) bf.getBean("arrayBean");
    assertEquals(new Integer(4), ab.getIntegerArray()[0]);
    assertEquals(new Integer(5), ab.getIntegerArray()[1]);
    assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]);
    assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 14 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates.

@Test
public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
    RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
    RootBeanDefinition na1 = createConstructorDependencyBeanDefinition(21);
    na1.setAutowireCandidate(false);
    lbf.registerBeanDefinition("bd1", bd1);
    lbf.registerBeanDefinition("na1", na1);
    // na1 was filtered
    ConstructorDependency actual = lbf.getBean(ConstructorDependency.class, 42);
    assertThat(actual.beanName, equalTo("bd1"));
    lbf.registerBeanDefinition("bd2", bd2);
    try {
        lbf.getBean(TestBean.class, 67);
        fail("Should have thrown NoSuchBeanDefinitionException");
    } catch (NoSuchBeanDefinitionException ex) {
    // expected
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 15 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testGetTypeForAbstractFactoryBean.

@Test
public void testGetTypeForAbstractFactoryBean() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class);
    bd.setAbstract(true);
    lbf.registerBeanDefinition("factoryBean", bd);
    assertNull(lbf.getType("factoryBean"));
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)801 Test (org.junit.jupiter.api.Test)354 Test (org.junit.Test)298 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)229 TestBean (org.springframework.beans.testfixture.beans.TestBean)124 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)100 ITestBean (org.springframework.tests.sample.beans.ITestBean)94 TestBean (org.springframework.tests.sample.beans.TestBean)94 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)91 ClassPathResource (org.springframework.core.io.ClassPathResource)86 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)73 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)69 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)50 BeanCreationException (org.springframework.beans.factory.BeanCreationException)41 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)38 Properties (java.util.Properties)35 HashMap (java.util.HashMap)30 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)29 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)28 BeforeEach (org.junit.jupiter.api.BeforeEach)27