Search in sources :

Example 6 with PropertyEditorRegistrar

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

the class AbstractBeanFactory method registerCustomEditors.

/**
	 * Initialize the given PropertyEditorRegistry with the custom editors
	 * that have been registered with this BeanFactory.
	 * <p>To be called for BeanWrappers that will create and populate bean
	 * instances, and for SimpleTypeConverter used for constructor argument
	 * and factory method type conversion.
	 * @param registry the PropertyEditorRegistry to initialize
	 */
protected void registerCustomEditors(PropertyEditorRegistry registry) {
    PropertyEditorRegistrySupport registrySupport = (registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
    if (registrySupport != null) {
        registrySupport.useConfigValueEditors();
    }
    if (!this.propertyEditorRegistrars.isEmpty()) {
        for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
            try {
                registrar.registerCustomEditors(registry);
            } catch (BeanCreationException ex) {
                Throwable rootCause = ex.getMostSpecificCause();
                if (rootCause instanceof BeanCurrentlyInCreationException) {
                    BeanCreationException bce = (BeanCreationException) rootCause;
                    if (isCurrentlyInCreation(bce.getBeanName())) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() + "] failed because it tried to obtain currently created bean '" + ex.getBeanName() + "': " + ex.getMessage());
                        }
                        onSuppressedException(ex);
                        continue;
                    }
                }
                throw ex;
            }
        }
    }
    if (!this.customEditors.isEmpty()) {
        for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
            Class<?> requiredType = entry.getKey();
            Class<? extends PropertyEditor> editorClass = entry.getValue();
            registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));
        }
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyEditorRegistrySupport(org.springframework.beans.PropertyEditorRegistrySupport) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) PropertyEditor(java.beans.PropertyEditor) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with PropertyEditorRegistrar

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

the class CustomEditorConfigurer method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (this.propertyEditorRegistrars != null) {
        for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) {
            beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);
        }
    }
    if (this.customEditors != null) {
        for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
            Class<?> requiredType = entry.getKey();
            Class<? extends PropertyEditor> propertyEditorClass = entry.getValue();
            beanFactory.registerCustomEditor(requiredType, propertyEditorClass);
        }
    }
}
Also used : PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) PropertyEditor(java.beans.PropertyEditor) Map(java.util.Map)

Example 8 with PropertyEditorRegistrar

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

the class DefaultListableBeanFactoryTests method testCustomEditorWithBeanReference.

@Test
public void testCustomEditorWithBeanReference() {
    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", new RuntimeBeanReference("myFloat"));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("testBean", bd);
    lbf.registerSingleton("myFloat", "1,1");
    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) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Aggregations

PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)8 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)6 Test (org.junit.Test)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 CustomNumberEditor (org.springframework.beans.propertyeditors.CustomNumberEditor)4 HashMap (java.util.HashMap)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 PropertyEditor (java.beans.PropertyEditor)2 NumberFormat (java.text.NumberFormat)2 AbstractCollection (java.util.AbstractCollection)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 CustomDateEditor (org.springframework.beans.propertyeditors.CustomDateEditor)2 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)2 GenericBean (org.springframework.tests.sample.beans.GenericBean)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2