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);
}
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);
}
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 & is an illegal character in
* Java method names. In other words, you can't name a method
* {@code set&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);
}
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
}
}
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);
}
Aggregations