use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method populatedJavaUtilOptionalBean.
@Test
public void populatedJavaUtilOptionalBean() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(Optional.class);
bd.setFactoryMethodName("of");
bd.getConstructorArgumentValues().addGenericArgumentValue("CONTENT");
bf.registerBeanDefinition("optionalBean", bd);
assertEquals(Optional.of("CONTENT"), bf.getBean(Optional.class));
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testPrototypeCreationWithConstructorArgumentsIsFastEnough.
/**
* @Test
* public void testPrototypeCreationIsFastEnough2() throws Exception {
* if (factoryLog.isTraceEnabled() || factoryLog.isDebugEnabled()) {
* // Skip this test: Trace logging blows the time limit.
* return;
* }
* DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
* Method setBeanNameMethod = TestBean.class.getMethod("setBeanName", String.class);
* Method setBeanFactoryMethod = TestBean.class.getMethod("setBeanFactory", BeanFactory.class);
* StopWatch sw = new StopWatch();
* sw.start("prototype");
* for (int i = 0; i < 100000; i++) {
* TestBean tb = TestBean.class.newInstance();
* setBeanNameMethod.invoke(tb, "test");
* setBeanFactoryMethod.invoke(tb, lbf);
* }
* sw.stop();
* // System.out.println(sw.getTotalTimeMillis());
* assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 500);
* }
*/
@Test
// TODO re-enable when ConstructorResolver TODO sorted out
@Ignore
public void testPrototypeCreationWithConstructorArgumentsIsFastEnough() {
Assume.group(TestGroup.PERFORMANCE);
Assume.notLogging(factoryLog);
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
rbd.getConstructorArgumentValues().addGenericArgumentValue("juergen");
rbd.getConstructorArgumentValues().addGenericArgumentValue("99");
lbf.registerBeanDefinition("test", rbd);
StopWatch sw = new StopWatch();
sw.start("prototype");
for (int i = 0; i < 100000; i++) {
TestBean tb = (TestBean) lbf.getBean("test");
assertEquals("juergen", tb.getName());
assertEquals(99, tb.getAge());
}
sw.stop();
// System.out.println(sw.getTotalTimeMillis());
assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000);
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testCustomEditor.
@Test
public void testCustomEditor() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
}
});
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,1");
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("testBean", bd);
TestBean testBean = (TestBean) lbf.getBean("testBean");
assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testCustomTypeConverter.
@Test
public void testCustomTypeConverter() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
lbf.setTypeConverter(new CustomTypeConverter(nf));
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,1");
ConstructorArgumentValues cav = new ConstructorArgumentValues();
cav.addIndexedArgumentValue(0, "myName");
cav.addIndexedArgumentValue(1, "myAge");
lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, cav, pvs));
TestBean testBean = (TestBean) lbf.getBean("testBean");
assertEquals("myName", testBean.getName());
assertEquals(5, testBean.getAge());
assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testReregisterBeanDefinition.
@Test
public void testReregisterBeanDefinition() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
lbf.registerBeanDefinition("testBean", bd1);
assertTrue(lbf.getBean("testBean") instanceof TestBean);
RootBeanDefinition bd2 = new RootBeanDefinition(NestedTestBean.class);
bd2.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
lbf.registerBeanDefinition("testBean", bd2);
assertTrue(lbf.getBean("testBean") instanceof NestedTestBean);
}
Aggregations