BeanWrapperImpl internally registers most of the Spring's built-in PropertyEditors (see the complete list in last tutorial) That means that type conversion can happen not only in Spring IoC container, by even outside of Spring container, by the simple use of BeanWrapper.
Examples
Following example shows, how BeanWrapper does auto type conversion from string to Date.
package com.logicbig.example;
import java.util.Date;
public class Person {
private String name;
private Date dateOfBirth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", dateOfBirth=" + dateOfBirth +
'}';
}
}
package com.logicbig.example;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
public class PersonBeanWrapperExample {
public static void main(String[] args) {
Person person = new Person();
BeanWrapper bw = new BeanWrapperImpl(person);
bw.setPropertyValue("name", "Tim");
bw.setPropertyValue("dateOfBirth", "Thu Jul 01 16:18:19 CDT 2021");
System.out.println(person);
System.out.println(person.getDateOfBirth().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate());
}
}
OutputPerson{name='Tim', dateOfBirth=Thu Jul 01 16:18:19 CDT 2021} 2021-07-01
Registering Custom editor with BeanWrapperImpl
package com.logicbig.example;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import java.text.SimpleDateFormat;
import java.util.Date;
public class PersonBeanWrapperExample2 {
public static void main(String[] args) {
Person person = new Person();
BeanWrapperImpl beanWrapper= new BeanWrapperImpl(person);
CustomDateEditor dateEditor = new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true);
beanWrapper.registerCustomEditor(Date.class, dateEditor);
beanWrapper.setPropertyValue("name", "Tim");
beanWrapper.setPropertyValue("dateOfBirth", "1984-07-10");
System.out.println(person);
}
}
OutputPerson{name='Tim', dateOfBirth=Tue Jul 10 00:00:00 CDT 1984}
Creating new property editor
package com.logicbig.example;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyEditorSupport;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateEditorExample {
public static void main(String[] args) {
Person person = new Person();
BeanWrapperImpl bw = new BeanWrapperImpl(person);
bw.registerCustomEditor(LocalDate.class,
new LocalDateEditor(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
bw.setPropertyValue("name", "Tim");
bw.setPropertyValue("dateOfBirth", "1984/07/06");
System.out.println(person);
}
private static class Person{
private String name;
private LocalDate dateOfBirth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", dateOfBirth=" + dateOfBirth +
'}';
}
}
public static class LocalDateEditor extends PropertyEditorSupport {
private final DateTimeFormatter formatter;
public LocalDateEditor(DateTimeFormatter formatter) {
this.formatter = formatter;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(LocalDate.parse(text, formatter));
}
@Override
public String getAsText() {
return getValue().toString();
}
}
}
OutputPerson{name='Tim', dateOfBirth=1984-07-06}
Accessing default PropertyEditor
Following example shows that we can access a default PropertyEditor via BeanWrapperImpl, even without wrapping an object.
package com.logicbig.example;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyEditor;
import java.util.Currency;
public class PropertyEditorAccessExample {
public static void main (String[] args) {
BeanWrapperImpl wrapper = new BeanWrapperImpl();
PropertyEditor editor = wrapper.getDefaultEditor(Currency.class);
editor.setAsText("MYR");
Currency value = (Currency) editor.getValue();
System.out.println(value.getDisplayName());
}
}
OutputMalaysian Ringgit
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|