Search in sources :

Example 1 with PropertyEditorRegistrar

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

the class BeanFactoryGenericsTests method testGenericMapWithCollectionValueFactoryMethod.

@Test
public void testGenericMapWithCollectionValueFactoryMethod() throws MalformedURLException {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {

        @Override
        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
        }
    });
    RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
    rbd.setFactoryMethodName("createInstance");
    Map<String, AbstractCollection<?>> input = new HashMap<>();
    HashSet<Integer> value1 = new HashSet<>();
    value1.add(new Integer(1));
    input.put("1", value1);
    ArrayList<Boolean> value2 = new ArrayList<>();
    value2.add(Boolean.TRUE);
    input.put("2", value2);
    rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
    rbd.getConstructorArgumentValues().addGenericArgumentValue(input);
    bf.registerBeanDefinition("genericBean", rbd);
    GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
    assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
    assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
Also used : CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) HashMap(java.util.HashMap) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) AbstractCollection(java.util.AbstractCollection) ArrayList(java.util.ArrayList) GenericBean(org.springframework.tests.sample.beans.GenericBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with PropertyEditorRegistrar

use of org.springframework.beans.PropertyEditorRegistrar 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 3 with PropertyEditorRegistrar

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

the class ConcurrentBeanFactoryTests method setUp.

@Before
public void setUp() throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CONTEXT);
    factory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {

        @Override
        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(Date.class, new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false));
        }
    });
    this.factory = factory;
}
Also used : PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) CustomDateEditor(org.springframework.beans.propertyeditors.CustomDateEditor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) Date(java.util.Date) Before(org.junit.Before)

Example 4 with PropertyEditorRegistrar

use of org.springframework.beans.PropertyEditorRegistrar in project geronimo-xbean by apache.

the class XBeanNamespaceHandler method registerCustomEditors.

/**
 * Registers whatever custom editors we need
 */
public static void registerCustomEditors(DefaultListableBeanFactory beanFactory) {
    PropertyEditorRegistrar registrar = new PropertyEditorRegistrar() {

        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(java.io.File.class, new org.apache.xbean.spring.context.impl.FileEditor());
            registry.registerCustomEditor(java.net.URI.class, new org.apache.xbean.spring.context.impl.URIEditor());
            registry.registerCustomEditor(java.util.Date.class, new org.apache.xbean.spring.context.impl.DateEditor());
            registry.registerCustomEditor(javax.management.ObjectName.class, new org.apache.xbean.spring.context.impl.ObjectNameEditor());
        }
    };
    beanFactory.addPropertyEditorRegistrar(registrar);
}
Also used : PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar)

Example 5 with PropertyEditorRegistrar

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

the class BeanFactoryGenericsTests method testGenericMapWithCollectionValueConstructor.

@Test
public void testGenericMapWithCollectionValueConstructor() throws MalformedURLException {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {

        @Override
        public void registerCustomEditors(PropertyEditorRegistry registry) {
            registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
        }
    });
    RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
    Map<String, AbstractCollection<?>> input = new HashMap<>();
    HashSet<Integer> value1 = new HashSet<>();
    value1.add(new Integer(1));
    input.put("1", value1);
    ArrayList<Boolean> value2 = new ArrayList<>();
    value2.add(Boolean.TRUE);
    input.put("2", value2);
    rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
    rbd.getConstructorArgumentValues().addGenericArgumentValue(input);
    bf.registerBeanDefinition("genericBean", rbd);
    GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");
    assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
    assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
Also used : CustomNumberEditor(org.springframework.beans.propertyeditors.CustomNumberEditor) PropertyEditorRegistry(org.springframework.beans.PropertyEditorRegistry) HashMap(java.util.HashMap) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) AbstractCollection(java.util.AbstractCollection) ArrayList(java.util.ArrayList) GenericBean(org.springframework.tests.sample.beans.GenericBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)8 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)6 Test (org.junit.Test)4 CustomNumberEditor (org.springframework.beans.propertyeditors.CustomNumberEditor)4 HashMap (java.util.HashMap)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 PropertyEditor (java.beans.PropertyEditor)2 NumberFormat (java.text.NumberFormat)2 AbstractCollection (java.util.AbstractCollection)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)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 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 Date (java.util.Date)1