温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot 2.0 配置属性自定义转换的方法

发布时间:2020-09-30 18:42:34 来源:脚本之家 阅读:158 作者:paderlol 栏目:编程语言

引言

当我们通过@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();
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI