Spring Boot 2.0 配置屬性自定義轉換

引言

當咱們經過@ConfigurationProperties註解實現配置 bean的時候,若是默認的配置屬性轉換沒法知足咱們的需求的時候,咱們能夠根據本身的需求經過如下擴展方式對配置屬性進行轉換java


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初始化的時候只獲取GenericConverterConverter接口
  • 官方文檔上還介紹了能夠使用實現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();
        }
}
相關文章
相關標籤/搜索