use of org.springframework.beans.PropertyEditorRegistry 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);
}
use of org.springframework.beans.PropertyEditorRegistry in project spring-framework by spring-projects.
the class ArgumentConvertingMethodInvoker method registerCustomEditor.
/**
* Register the given custom property editor for all properties of the given type.
* <p>Typically used in conjunction with the default
* {@link org.springframework.beans.SimpleTypeConverter}; will work with any
* TypeConverter that implements the PropertyEditorRegistry interface as well.
* @param requiredType type of the property
* @param propertyEditor editor to register
* @see #setTypeConverter
* @see org.springframework.beans.PropertyEditorRegistry#registerCustomEditor
*/
public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {
TypeConverter converter = getTypeConverter();
if (!(converter instanceof PropertyEditorRegistry)) {
throw new IllegalStateException("TypeConverter does not implement PropertyEditorRegistry interface: " + converter);
}
((PropertyEditorRegistry) converter).registerCustomEditor(requiredType, propertyEditor);
}
use of org.springframework.beans.PropertyEditorRegistry 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);
}
use of org.springframework.beans.PropertyEditorRegistry 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;
}
use of org.springframework.beans.PropertyEditorRegistry in project grails-core by grails.
the class GrailsWebRequest method getPropertyEditorRegistry.
/**
* Obtains the PropertyEditorRegistry instance.
* @return The PropertyEditorRegistry
*/
public PropertyEditorRegistry getPropertyEditorRegistry() {
final HttpServletRequest servletRequest = getCurrentRequest();
PropertyEditorRegistry registry = (PropertyEditorRegistry) servletRequest.getAttribute(GrailsApplicationAttributes.PROPERTY_REGISTRY);
if (registry == null) {
registry = new PropertyEditorRegistrySupport();
PropertyEditorRegistryUtils.registerCustomEditors(this, registry, RequestContextUtils.getLocale(servletRequest));
servletRequest.setAttribute(GrailsApplicationAttributes.PROPERTY_REGISTRY, registry);
}
return registry;
}
Aggregations