Search in sources :

Example 6 with PropertyValue

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

the class PropertyOverrideConfigurer method applyPropertyValue.

/**
	 * Apply the given property value to the corresponding bean.
	 */
protected void applyPropertyValue(ConfigurableListableBeanFactory factory, String beanName, String property, String value) {
    BeanDefinition bd = factory.getBeanDefinition(beanName);
    while (bd.getOriginatingBeanDefinition() != null) {
        bd = bd.getOriginatingBeanDefinition();
    }
    PropertyValue pv = new PropertyValue(property, value);
    pv.setOptional(this.ignoreInvalidKeys);
    bd.getPropertyValues().addPropertyValue(pv);
}
Also used : PropertyValue(org.springframework.beans.PropertyValue)

Example 7 with PropertyValue

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

the class DefaultListableBeanFactoryTests method testExtensiveCircularReference.

@Test
public void testExtensiveCircularReference() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    for (int i = 0; i < 1000; i++) {
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
        RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
        bd.setPropertyValues(pvs);
        lbf.registerBeanDefinition("bean" + i, bd);
    }
    lbf.preInstantiateSingletons();
    for (int i = 0; i < 1000; i++) {
        TestBean bean = (TestBean) lbf.getBean("bean" + i);
        TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0));
        assertTrue(bean.getSpouse() == otherBean);
    }
}
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) PropertyValue(org.springframework.beans.PropertyValue) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Example 8 with PropertyValue

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

the class ScriptFactoryPostProcessor method createConfigInterface.

/**
	 * Create a config interface for the given bean definition, defining setter
	 * methods for the defined property values as well as an init method and
	 * a destroy method (if defined).
	 * <p>This implementation creates the interface via CGLIB's InterfaceMaker,
	 * determining the property types from the given interfaces (as far as possible).
	 * @param bd the bean definition (property values etc) to create a
	 * config interface for
	 * @param interfaces the interfaces to check against (might define
	 * getters corresponding to the setters we're supposed to generate)
	 * @return the config interface
	 * @see org.springframework.cglib.proxy.InterfaceMaker
	 * @see org.springframework.beans.BeanUtils#findPropertyType
	 */
protected Class<?> createConfigInterface(BeanDefinition bd, Class<?>[] interfaces) {
    InterfaceMaker maker = new InterfaceMaker();
    PropertyValue[] pvs = bd.getPropertyValues().getPropertyValues();
    for (PropertyValue pv : pvs) {
        String propertyName = pv.getName();
        Class<?> propertyType = BeanUtils.findPropertyType(propertyName, interfaces);
        String setterName = "set" + StringUtils.capitalize(propertyName);
        Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] { Type.getType(propertyType) });
        maker.add(signature, new Type[0]);
    }
    if (bd instanceof AbstractBeanDefinition) {
        AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
        if (abd.getInitMethodName() != null) {
            Signature signature = new Signature(abd.getInitMethodName(), Type.VOID_TYPE, new Type[0]);
            maker.add(signature, new Type[0]);
        }
        if (StringUtils.hasText(abd.getDestroyMethodName())) {
            Signature signature = new Signature(abd.getDestroyMethodName(), Type.VOID_TYPE, new Type[0]);
            maker.add(signature, new Type[0]);
        }
    }
    return maker.create();
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) InterfaceMaker(org.springframework.cglib.proxy.InterfaceMaker) Signature(org.springframework.cglib.core.Signature) PropertyValue(org.springframework.beans.PropertyValue)

Example 9 with PropertyValue

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

the class MetadataAttachmentTests method propertyMetadata.

@Test
public void propertyMetadata() throws Exception {
    BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("testBean3");
    PropertyValue pv = beanDefinition.getPropertyValues().getPropertyValue("name");
    assertEquals("Harrop", pv.getAttribute("surname"));
}
Also used : PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 10 with PropertyValue

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

the class DataBinderFieldAccessTests method nestedBindingWithDefaultConversionNoErrors.

@Test
public void nestedBindingWithDefaultConversionNoErrors() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    assertTrue(binder.isIgnoreUnknownFields());
    binder.initDirectFieldAccess();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry"));
    pvs.addPropertyValue(new PropertyValue("spouse.jedi", "on"));
    binder.bind(pvs);
    binder.close();
    assertEquals("Kerry", rod.getSpouse().getName());
    assertTrue((rod.getSpouse()).isJedi());
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FieldAccessBean(org.springframework.tests.sample.beans.FieldAccessBean) PropertyValue(org.springframework.beans.PropertyValue) Test(org.junit.Test)

Aggregations

PropertyValue (org.springframework.beans.PropertyValue)41 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 Test (org.junit.Test)15 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 FieldAccessBean (org.springframework.tests.sample.beans.FieldAccessBean)6 ArrayList (java.util.ArrayList)5 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ITestBean (org.springframework.tests.sample.beans.ITestBean)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2