spring@Value字符串类型转换为对象

欢迎关注我的微信公众号:好好学技术

背景

工作中经常会碰到各种奇奇怪怪的需求,比如从配置文件读取出来的值转换为对象。

万能的字符串

任何时候都可以使用字符串作为属性的值,从配置文件中读取出来,如下:
配置文件内容为

test:
  person: 1,张三,15

通过,分割,分别是id、姓名和年龄。
但是我们希望将它转换成一个Person对象。

@Value("${test.person}")
private Person person;

我们希望通过这种方式读取出配置文件,并转换为Person对象。

自定义转换类

PropertyEditor

这是JDK中提供的类型转化工具类

首先,定义一个实体类

package com.fandf.demo.propertyeditor;

import lombok.Data;

/**
 * 在Spring源码中,有可能需要把String转成其他类型,所以在Spring源码中提供了一些技术来更方便的做对象的类型转化
 *
 * @author fandongfeng
 */
@Data
public class Person {

    private Long id;
    private String name;
    private Integer age;

}

定义一个PropertyEditor

package com.fandf.demo.propertyeditor;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;

/**
 * 这是JDK中提供的类型转化工具类
 *
 * @author fandongfeng
 */
public class StringToPersonPropertyEditor extends PropertyEditorSupport implements PropertyEditor {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Person person = new Person();
        String[] split = text.split(",");
        person.setId(Long.valueOf(split[0]));
        person.setName(split[1]);
        person.setAge(Integer.valueOf(split[2]));
        this.setValue(person);
    }

    //打印结果
    // Person(id=1, name=张三, age=16)
    public static void main(String[] args) {
        StringToPersonPropertyEditor propertyEditor = new StringToPersonPropertyEditor();
        propertyEditor.setAsText("1,张三,15");
        Person value = (Person) propertyEditor.getValue();
        System.out.println(value);
    }
}

那么在spring中应该怎么用呢?
只需要加入下面代码即可

    @Bean
    public CustomEditorConfigurer customEditorConfigurer() {
        CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
        Map<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();
        // 表示StringToPersonPropertyEditor可以将String转化成Person类型,在Spring源码中,如果发现当前对象是String,而需要的类型是Person,就会使用该PropertyEditor来做类型转化
        propertyEditorMap.put(Person.class, StringToPersonPropertyEditor.class);
        customEditorConfigurer.setCustomEditors(propertyEditorMap);
        return customEditorConfigurer;
    }

ConversionService

Spring中提供的类型转化服务,它比PropertyEditor更强大

package com.fandf.demo.propertyeditor;

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.support.DefaultConversionService;

import java.util.Collections;
import java.util.Set;

/**
 * @author fandongfeng
 */
public class StringToPersonConverter implements ConditionalGenericConverter {
    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        return sourceType.getType().equals(String.class) && targetType.getType().equals(Person.class);
    }

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        return Collections.singleton(new ConvertiblePair(String.class, Person.class));
    }

    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        Person person = new Person();
        String[] split = String.valueOf(source).split(",");
        person.setId(Long.valueOf(split[0]));
        person.setName(split[1]);
        person.setAge(Integer.valueOf(split[2]));
        return person;
    }

    public static void main(String[] args) {
        DefaultConversionService conversionService = new DefaultConversionService();
        conversionService.addConverter(new StringToPersonConverter());
        Person value = conversionService.convert("1,张三,15", Person.class);
        System.out.println(value);
    }

}

在spring中可以这样使用

    @Bean
    public ConversionServiceFactoryBean conversionService() {
        ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
        conversionServiceFactoryBean.setConverters(Collections.singleton(new StringToPersonConverter()));
        return conversionServiceFactoryBean;
    }

TypeConverter

整合了PropertyEditor和ConversionService的功能,是Spring内部用的

        SimpleTypeConverter typeConverter = new SimpleTypeConverter();
        typeConverter.registerCustomEditor(Person.class, new StringToPersonPropertyEditor());
        Person value = typeConverter.convertIfNecessary("1,张三,16", Person.class);
        System.out.println(value);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值