Search in sources :

Example 11 with MutablePropertyValues

use of org.springframework.beans.MutablePropertyValues 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 12 with MutablePropertyValues

use of org.springframework.beans.MutablePropertyValues 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 13 with MutablePropertyValues

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

the class DefaultListableBeanFactoryTests method testSelfReference.

@Test
public void testSelfReference() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("spouse", new RuntimeBeanReference("self"));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("self", bd);
    TestBean self = (TestBean) lbf.getBean("self");
    assertEquals(self, self.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) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Example 14 with MutablePropertyValues

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

the class QuartzJobBean method execute.

/**
	 * This implementation applies the passed-in job data map as bean property
	 * values, and delegates to {@code executeInternal} afterwards.
	 * @see #executeInternal
	 */
@Override
public final void execute(JobExecutionContext context) throws JobExecutionException {
    try {
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValues(context.getScheduler().getContext());
        pvs.addPropertyValues(context.getMergedJobDataMap());
        bw.setPropertyValues(pvs, true);
    } catch (SchedulerException ex) {
        throw new JobExecutionException(ex);
    }
    executeInternal(context);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) SchedulerException(org.quartz.SchedulerException) JobExecutionException(org.quartz.JobExecutionException) MutablePropertyValues(org.springframework.beans.MutablePropertyValues)

Example 15 with MutablePropertyValues

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

the class SpringBeanJobFactory method createJobInstance.

/**
	 * Create the job instance, populating it with property values taken
	 * from the scheduler context, job data map and trigger data map.
	 */
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
    Object job = super.createJobInstance(bundle);
    if (isEligibleForPropertyPopulation(job)) {
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
        MutablePropertyValues pvs = new MutablePropertyValues();
        if (this.schedulerContext != null) {
            pvs.addPropertyValues(this.schedulerContext);
        }
        pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
        pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
        if (this.ignoredUnknownProperties != null) {
            for (String propName : this.ignoredUnknownProperties) {
                if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
                    pvs.removePropertyValue(propName);
                }
            }
            bw.setPropertyValues(pvs);
        } else {
            bw.setPropertyValues(pvs, true);
        }
    }
    return job;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues)

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