Spring 型別轉換
阿新 • • 發佈:2019-02-11
型別轉換
spring boot 配置檔案 application.properties 配置了一個日期,檔案裡的屬性值只能是字串,我們在配置類中定義的是Date型別,所以需要將字串轉換成Date型別。轉換方式有兩種,一種是使用PropertyEditor,另外一種是使用Converter
//application.properties
platform.version=1.0
platform.release=2018-05-25
//Platform.java
@ConfigurationProperties("platform")
@Component
public class Platform {
private String version;
private Date release;
//getter and setter
}
PropertyEditor
針對上述案例,我們可以提供一個自定義的PropertyEditor。可以直接實現PropertyEditor介面,或更為常見的做法是繼承PropertyEditorSupport,然後實現如下兩個方法:
- String getAsText()
- void setAsText(String text)
public class CustomDateEditor extends PropertyEditorSupport {
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Override
public String getAsText() {
Date value = (Date)getValue();
return dateFormat.format(value);
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
try {
setValue(dateFormat.parse(text));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
定義了CustomDateEditor 後,spring是如何感知並使用這個轉換器呢,可以通過CustomEditorConfigurer(繼承自BeanFactoryPostProcessor)類,將CustomDateEditor註冊到spring的BeanFactory中。CustomEditorConfigurer類說明中有兩個使用xml配置的樣例,我們這裡使用對應的java註解配置方式。
第一種方式:通過 PropertyEditorRegistrar 註冊,這也是推薦的方式。
@Configuration
public class Config {
@Bean
public CustomEditorConfigurer customEditorConfigurer() {
CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
customEditorConfigurer.setPropertyEditorRegistrars(new PropertyEditorRegistrar[]{new DatePropertyEditorRegistrar()});
return customEditorConfigurer;
}
}
public class DatePropertyEditorRegistrar implements PropertyEditorRegistrar {
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, null, new CustomDateEditor());
}
}
第二種方式:直接註冊自定義的PropertiyEditor
@Configuration
public class Config {
@Bean
public CustomEditorConfigurer customEditorConfigurer() {
CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
Map<Class<?>, Class<? extends PropertyEditor>> customEditors = new HashMap<>();
customEditors.put(Date.class, CustomDateEditor.class);
customEditorConfigurer.setCustomEditors(customEditors);
return customEditorConfigurer;
}
}
Converter
使用Conveter進行型別轉換,在spring boot中有兩種使用方式,首先都需要建立一個實現Converter了介面類。
public class CustomDateConverter implements Converter<String, Date> {
@Nullable
@Override
public Date convert(String source) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
第一種方式:使用ConfigurationPropertiesBinding註解。
@Component
@ConfigurationPropertiesBinding
public class CustomDateConverter implements Converter<String, Date> {
@Nullable
@Override
public Date convert(String source) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
第二種方式:配置名字為”conversionService”的bean。為什麼是這個名字,可以參考類:org.springframework.boot.context.properties.ConversionServiceDeducer
@Configuration
public class Config {
/**
* {@link ConversionServiceDeducer}
* @return
*/
@Bean
public ConversionService conversionService() {
DefaultConversionService service = new DefaultConversionService();
service.addConverter(new CustomDateConverter());
return service;
}
}