Search in sources :

Example 96 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testBeanDefinitionOverridingWithAlias.

@Test
public void testBeanDefinitionOverridingWithAlias() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
    lbf.registerAlias("test", "testAlias");
    lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
    lbf.registerAlias("test", "testAlias");
    assertTrue(lbf.getBean("test") instanceof NestedTestBean);
    assertTrue(lbf.getBean("testAlias") instanceof NestedTestBean);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 97 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testGetBeanByTypeInstanceWithMultiplePrimary.

@Test
public void testGetBeanByTypeInstanceWithMultiplePrimary() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
    RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
    bd1.setPrimary(true);
    bd2.setPrimary(true);
    lbf.registerBeanDefinition("bd1", bd1);
    lbf.registerBeanDefinition("bd2", bd2);
    thrown.expect(NoUniqueBeanDefinitionException.class);
    thrown.expectMessage(containsString("more than one 'primary'"));
    lbf.getBean(ConstructorDependency.class, 42);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 98 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testAutowireBeanWithFactoryBeanByName.

/**
	 * Verifies that a dependency on a {@link FactoryBean} can <strong>not</strong>
	 * be autowired <em>by name</em>, as &amp; is an illegal character in
	 * Java method names. In other words, you can't name a method
	 * {@code set&amp;FactoryBean(...)}.
	 */
@Test(expected = TypeMismatchException.class)
public void testAutowireBeanWithFactoryBeanByName() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(LazyInitFactory.class);
    lbf.registerBeanDefinition("factoryBean", bd);
    LazyInitFactory factoryBean = (LazyInitFactory) lbf.getBean("&factoryBean");
    assertNotNull("The FactoryBean should have been registered.", factoryBean);
    lbf.autowire(FactoryBeanDependentBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 99 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testNameAlreadyBound.

@Test
public void testNameAlreadyBound() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("kerry.(class)", TestBean.class.getName());
    p.setProperty("kerry.age", "35");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    try {
        (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    } catch (BeanDefinitionStoreException ex) {
        assertEquals("kerry", ex.getBeanName());
    // expected
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 100 with DefaultListableBeanFactory

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

the class CallbacksSecurityTests method testInitSecurityAwarePrototypeBean.

@Test
public void testInitSecurityAwarePrototypeBean() {
    final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(NonPrivilegedBean.class).setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE).setInitMethodName("init").setDestroyMethodName("destroy").addConstructorArgValue("user1");
    lbf.registerBeanDefinition("test", bdb.getBeanDefinition());
    final Subject subject = new Subject();
    subject.getPrincipals().add(new TestPrincipal("user1"));
    NonPrivilegedBean bean = Subject.doAsPrivileged(subject, new PrivilegedAction<NonPrivilegedBean>() {

        @Override
        public NonPrivilegedBean run() {
            return lbf.getBean("test", NonPrivilegedBean.class);
        }
    }, null);
    assertNotNull(bean);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Aggregations

DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)635 Test (org.junit.Test)574 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)314 TestBean (org.springframework.tests.sample.beans.TestBean)249 ITestBean (org.springframework.tests.sample.beans.ITestBean)199 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)152 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)101 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)96 ClassPathResource (org.springframework.core.io.ClassPathResource)77 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)74 Properties (java.util.Properties)32 BeanCreationException (org.springframework.beans.factory.BeanCreationException)26 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)26 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)24 HashMap (java.util.HashMap)22 Before (org.junit.Before)22 PropertiesBeanDefinitionReader (org.springframework.beans.factory.support.PropertiesBeanDefinitionReader)22 DependenciesBean (org.springframework.tests.sample.beans.DependenciesBean)17 ConstructorDependenciesBean (org.springframework.beans.factory.xml.ConstructorDependenciesBean)16 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)13