use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testSimpleReference.
@Test
public void testSimpleReference() {
String PREFIX = "beans.";
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
Properties p = new Properties();
p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
p.setProperty(PREFIX + "rod.name", "Rod");
p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
p.setProperty(PREFIX + "kerry.name", "Kerry");
p.setProperty(PREFIX + "kerry.age", "35");
p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");
int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
assertTrue("2 beans registered, not " + count, count == 2);
TestBean kerry = lbf.getBean("kerry", TestBean.class);
assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
ITestBean spouse = kerry.getSpouse();
assertTrue("Kerry spouse is non null", spouse != null);
assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testPrototypeStringCreatedRepeatedly.
@Test
public void testPrototypeStringCreatedRepeatedly() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition stringDef = new RootBeanDefinition(String.class);
stringDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
stringDef.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue("value"));
lbf.registerBeanDefinition("string", stringDef);
String val1 = lbf.getBean("string", String.class);
String val2 = lbf.getBean("string", String.class);
assertEquals("value", val1);
assertEquals("value", val2);
assertNotSame(val1, val2);
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testExplicitScopeInheritanceForChildBeanDefinitions.
@Test
public void testExplicitScopeInheritanceForChildBeanDefinitions() throws Exception {
String theChildScope = "bonanza!";
RootBeanDefinition parent = new RootBeanDefinition();
parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
child.setBeanClass(TestBean.class);
child.setScope(theChildScope);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerBeanDefinition("parent", parent);
factory.registerBeanDefinition("child", child);
AbstractBeanDefinition def = (AbstractBeanDefinition) factory.getBeanDefinition("child");
assertEquals("Child 'scope' not overriding parent scope (it must).", theChildScope, def.getScope());
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testBeanPostProcessorWithWrappedObjectAndDestroyMethod.
@Test
public void testBeanPostProcessorWithWrappedObjectAndDestroyMethod() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(BeanWithDestroyMethod.class);
bd.setDestroyMethodName("close");
lbf.registerBeanDefinition("test", bd);
lbf.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return new TestBean();
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}
});
BeanWithDestroyMethod.closeCount = 0;
lbf.preInstantiateSingletons();
lbf.destroySingletons();
assertEquals("Destroy methods invoked", 1, BeanWithDestroyMethod.closeCount);
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testGetBeanByTypeDefinedInParent.
@Test
public void testGetBeanByTypeDefinedInParent() {
DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
parent.registerBeanDefinition("bd1", bd1);
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(parent);
TestBean bean = lbf.getBean(TestBean.class);
assertThat(bean.getBeanName(), equalTo("bd1"));
}
Aggregations