use of org.springframework.beans.MutablePropertyValues in project spring-framework by spring-projects.
the class SimplePropertyNamespaceHandler method decorate.
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
if (node instanceof Attr) {
Attr attr = (Attr) node;
String propertyName = parserContext.getDelegate().getLocalName(attr);
String propertyValue = attr.getValue();
MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues();
if (pvs.contains(propertyName)) {
parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " + "both <property> and inline syntax. Only one approach may be used per property.", attr);
}
if (propertyName.endsWith(REF_SUFFIX)) {
propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length());
pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue));
} else {
pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue);
}
}
return definition;
}
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());
}
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);
}
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"));
}
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());
}
Aggregations