Search in sources :

Example 41 with MutablePropertyValues

use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testConfigureBeanWithAutowiring.

@Test
public void testConfigureBeanWithAutowiring() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("age", "99");
    RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
    tbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_NAME);
    lbf.registerBeanDefinition("test", tbd);
    TestBean tb = new TestBean();
    lbf.configureBean(tb, "test");
    assertSame(lbf, tb.getBeanFactory());
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertEquals(spouse, tb.getSpouse());
}
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) Test(org.junit.Test)

Example 42 with MutablePropertyValues

use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testCustomConverter.

@Test
public void testCustomConverter() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    GenericConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<String, Float>() {

        @Override
        public Float convert(String source) {
            try {
                NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
                return nf.parse(source).floatValue();
            } catch (ParseException ex) {
                throw new IllegalArgumentException(ex);
            }
        }
    });
    lbf.setConversionService(conversionService);
    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 : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) 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) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ParseException(java.text.ParseException) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Example 43 with MutablePropertyValues

use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testExpressionInStringArray.

@Test
public void testExpressionInStringArray() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
    when(beanExpressionResolver.evaluate(eq("#{foo}"), ArgumentMatchers.any(BeanExpressionContext.class))).thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties");
    bf.setBeanExpressionResolver(beanExpressionResolver);
    RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("locations", new String[] { "#{foo}" });
    rbd.setPropertyValues(pvs);
    bf.registerBeanDefinition("myProperties", rbd);
    Properties properties = (Properties) bf.getBean("myProperties");
    assertEquals("bar", properties.getProperty("foo"));
}
Also used : BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Properties(java.util.Properties) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext) Test(org.junit.Test)

Example 44 with MutablePropertyValues

use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testAutowireWithSatisfiedJavaBeanDependency.

@Test
public void testAutowireWithSatisfiedJavaBeanDependency() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "Rod");
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("rod", bd);
    assertEquals(1, lbf.getBeanDefinitionCount());
    // Depends on age, name and spouse (TestBean)
    Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    assertEquals(1, lbf.getBeanDefinitionCount());
    DependenciesBean kerry = (DependenciesBean) registered;
    TestBean rod = (TestBean) lbf.getBean("rod");
    assertSame(rod, kerry.getSpouse());
}
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) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Example 45 with MutablePropertyValues

use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testAutowireWithSatisfiedConstructorDependency.

@Test
public void testAutowireWithSatisfiedConstructorDependency() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "Rod");
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("rod", bd);
    assertEquals(1, lbf.getBeanDefinitionCount());
    Object registered = lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
    assertEquals(1, lbf.getBeanDefinitionCount());
    ConstructorDependency kerry = (ConstructorDependency) registered;
    TestBean rod = (TestBean) lbf.getBean("rod");
    assertSame(rod, kerry.spouse);
}
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) Test(org.junit.Test)

Aggregations

MutablePropertyValues (org.springframework.beans.MutablePropertyValues)305 Test (org.junit.Test)269 TestBean (org.springframework.tests.sample.beans.TestBean)86 ITestBean (org.springframework.tests.sample.beans.ITestBean)82 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)73 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)68 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)35 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)24 PropertyEditorSupport (java.beans.PropertyEditorSupport)23 PropertyValue (org.springframework.beans.PropertyValue)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)16 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)15 ScannedGenericBeanDefinition (org.springframework.context.annotation.ScannedGenericBeanDefinition)14 HashMap (java.util.HashMap)13 BeanWrapper (org.springframework.beans.BeanWrapper)12 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)11 ParseException (java.text.ParseException)9 RelaxedDataBinder (org.springframework.boot.bind.RelaxedDataBinder)9 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)9