PropertyEditor
是spring提供的一种对属性自定义解析的一种方式,如:用户传入时间戳,直接将其转换为LocalDate
这里使用的PropertyEditorSupport
是spring提供的PropertyEditor
的实现,直接继承使用,主要重写setAsText
方法对属性的转换
package com.armin.self_editor;
import java.beans.PropertyEditorSupport;
import java.time.LocalDate;
public class LocalDatePropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
Long timeMillis = null;
try {
timeMillis = Long.valueOf(text);
} catch (NumberFormatException e) {
//todo 转换异常的处理
}
LocalDate localDate = Instant.ofEpochMilli(timeMillis).atZone(ZoneOffset.ofHours(8)).toLocalDate();
this.setValue(localDate);
}
}
这里需要实现PropertyEditorRegistrar
并重写registerCustomEditors
方法
package com.armin.self_editor;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import java.time.LocalDate;
public class LocalDatePropertyEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(LocalDate.class, new LocalDatePropertyEditor());
}
}
package com.armin.self_editor;
import java.time.LocalDate;
@Data
public class DateCustomer {
private LocalDate date;
}
注入解析的bean并将自定义属性编辑器注册到CustomEditorConfigurer
中
<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.armin.self_editor.DateCustomer">
<property name="date" value="1694534400000"/>
bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>-->
<bean class="com.armin.self_editor.LocalDatePropertyEditorRegistrar"/>
list>
property>
bean>
beans>
package com.armin.self_editor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SelfEditorTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("armin-self-editor.xml");
DateCustomer customer = ac.getBean(DateCustomer.class);
System.out.println(customer);
}
}
如果想要在Controller
层对web传入的参数进行自定义转换处理需要实现WebBindingInitializer
接口,在initBinder
方法中使用传入的WebDataBinder
参数调用registerCustomEditor
传入我们需要转换的类型与自定义转换处理的PropertyEditor
对象
package com.armin.self_editor;
import com.armin.self_editor.LocalDatePropertyEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import java.time.LocalDate;
public class MyWebBindingInitializer implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.registerCustomEditor(LocalDate.class, new LocalDatePropertyEditor());
}
}
然后将该对象设置到RequestMappingHandlerAdapter
中的webBindingInitializer
属性中
package com.armin.self_editor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.Resource;
@Configuration
public class WebConfiguration {
@Resource
public void setWebBindingInitializer (RequestMappingHanlderAdapter adapter) {
adapter.setWebBindingInitializer(new MyWebBindingInitializer());
}
}
至此已完成了Controller
层传参的全局处理(时间戳转换为LocalDate
的处理)
同时如果需要对单个Controller
中使用该转换可以在需要转换的Controller
加入如下代码
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(LocalDate.class, new LocalDatePropertyEditor());
}
WebDataBinder实际上就是实现了
PropertyEditorRegistry
SpringBoot中的一种封装方式