Search in sources :

Example 46 with RootBeanDefinition

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));
}
Also used : Optional(java.util.Optional) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 47 with RootBeanDefinition

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);
}
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) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) StopWatch(org.springframework.util.StopWatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 48 with RootBeanDefinition

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);
}
Also used : CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) 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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Example 49 with RootBeanDefinition

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);
}
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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NumberFormat(java.text.NumberFormat) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 50 with RootBeanDefinition

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);
}
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)

Aggregations

RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)704 Test (org.junit.Test)522 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)311 TestBean (org.springframework.tests.sample.beans.TestBean)159 ITestBean (org.springframework.tests.sample.beans.ITestBean)143 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)133 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)85 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)80 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)77 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)77 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)47 Element (org.w3c.dom.Element)47 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)46 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)36 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)31 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)30 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)30 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)28 Properties (java.util.Properties)26 ManagedList (org.springframework.beans.factory.support.ManagedList)25