這篇文章主要介紹了Spring Boot 2.0 配置屬性自定義轉換的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
引言
當我們通過@ConfigurationProperties註解實現配置 bean的時候,如果默認的配置屬性轉換無法滿足我們的需求的時候,我們可以根據自己的需求通過以下擴展方式對配置屬性進行轉換
PropertyEditorSupport實現
下面的例子是把屬性中定義的字符串轉換成Movie,並且把name的值大寫
繼承PropertyEditorSupport並且實現PropertyEditorRegistrar接口
package com.paderlol.spring.practice.properties.editor; import com.paderlol.spring.practice.properties.pojo.Movie; import java.beans.PropertyEditorSupport; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; /** * @author pader PropertyEditor 在不同的包下面 */ @Slf4j public class CustomMovieEditor extends PropertyEditorSupport implements PropertyEditorRegistrar { @Override public String getAsText() { Movie movie = (Movie) getValue(); return movie == null ? "" : movie.getName(); } @Override public void setAsText(String text) throws IllegalArgumentException { log.info("繼承[PropertyEditorSupport]類,轉換數據={}", text); String[] data = text.split("-"); Movie movie = Movie.builder().name(data[0] .toUpperCase()).seat(Integer.parseInt(data[1])) .build(); setValue(movie); } @Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Movie.class,this); } }
註冊自定義的PropertyEditor
@Bean public CustomEditorConfigurer customEditorConfigurer() { CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer(); // 有兩種註冊方式 這是第一種 customEditorConfigurer.setPropertyEditorRegistrars( new PropertyEditorRegistrar[]{ new CustomMovieEditor() }); // 第二 種 Map<Class<?>,Class<? extends PropertyEditor>> maps = new HashMap<>(); maps.put(Movie.class,CustomMovieEditor.class); return customEditorConfigurer; }
Converter接口+@ConfigurationPropertiesBinding註解
//注意 @Component @ConfigurationPropertiesBinding public class StringToPersonConverter implements Converter<String, Person> { @Override public Person convert(String from) { log.info("使用[Converter]接口,轉換數據={}", from); String[] data = from.split(","); return Person.builder().name(data[0]).age(Integer.parseInt(data[1])).build(); } }
總結
- 以上兩種實現方式結果,但是Converter接口相比PropertyEditor接口更加靈活一些,PropertyEditor接口僅限於String轉換,Converter可以自定義別的,並且PropertyEditor接口通常用於Controller中的接收參數的轉換。
- @ConfigurationPropertiesBinding是限定符註解@Qualifier的派生類而已,參考org.springframework.boot.context.properties.ConversionServiceDeducer,以下是源代碼片段
@Autowired(required = false) @ConfigurationPropertiesBinding public void setConverters(List<Converter<?, ?>> converters) { this.converters = converters; } /** * A list of custom converters (in addition to the defaults) to use when * converting properties for binding. * @param converters the converters to set */ @Autowired(required = false) @ConfigurationPropertiesBinding public void setGenericConverters(List<GenericConverter> converters) { this.genericConverters = converters; }
- Formatter接口是不能對屬性完成轉換的,因爲ConversionServiceDeducer初始化的時候只獲取GenericConverter和Converter接口
- 官方文檔上還介紹了可以使用實現org.springframework.core.convert.ConversionService並且Bean名稱也必須叫conversionService,不過大部分情況不推薦自己通過這種方式去實現這個接口,因爲自己實現的ConversionService會替代默認的。具體參考ConversionServiceDeducer源碼:
public ConversionService getConversionService() { try { //默認首先尋找Bean名稱叫conversionService的ConversionService的Bean類 return this.applicationContext.getBean( ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME, ConversionService.class); } catch (NoSuchBeanDefinitionException ex) { //找不到就默認生成ApplicationConversionService類 return this.applicationContext.getAutowireCapableBeanFactory() .createBean(Factory.class).create(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。