In this XML config example a user defined PropertyEditorRegistrar implementation is registered with CustomEditorConfigurer. This is the right approach to register a custom PropertyEditor.
package com.logicbig.example;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class XmlCustomEditorExample {
public static void main (String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("spring-config2.xml");
MyBean bean = context.getBean(MyBean.class);
System.out.println(bean);
}
public static class MyBean {
private Double price;
public Double getPrice () {
return price;
}
public void setPrice (Double price) {
this.price = price;
}
@Override
public String toString () {
return "MyBean{" + "price=" + price + '}';
}
}
public static class MyCustomEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors (PropertyEditorRegistry registry) {
NumberFormat numberFormat = new DecimalFormat("##,###.00");
registry.registerCustomEditor(java.lang.Double.class,
new CustomNumberEditor(java.lang.Double.class,
numberFormat, true));
}
}
}
Output
MyBean{price=45678.567}
src/main/resources/spring-config2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="customer" class="com.logicbig.example.XmlCustomEditorExample.MyBean">
<property name="price" value="45,678.567"/>
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean class="com.logicbig.example.XmlCustomEditorExample.MyCustomEditorRegistrar" />
</list>
</property>
</bean>
</beans>
Original PostThis JavaConfig example shows how to use PropertyEditorRegistrar implementation to register a custom editor. Here we use PropertyEditor to convert text from a property file to a Java object.
package com.logicbig.example;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ValueAnnotationExample2 {
public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}
@Configuration
@PropertySource("classpath:app.properties")
public static class Config {
@Bean
public ClientBean clientBean () {
return new ClientBean();
}
@Bean
public static CustomEditorConfigurer customEditorConfigurer () {
CustomEditorConfigurer cec = new CustomEditorConfigurer();
cec.setPropertyEditorRegistrars(
new PropertyEditorRegistrar[]{
new MyCustomBeanRegistrar()});
return cec;
}
}
public static class ClientBean {
@Value("${theTradeDate}")
private Date tradeDate;
public void doSomething () {
System.out.printf("The trade date from prop file is %s%n", tradeDate);
}
}
public static class MyCustomBeanRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors (PropertyEditorRegistry registry) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
dateFormatter.setLenient(false);
registry.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormatter, true));
}
}
}
src/main/resources/app.properties:
theCurrency=PLN
thePrice=12,323.7654
theTradeDate=2016-9-14
Original Post