spring 中的类型转换工具类

在看 org.springframework.data.redis.support.atomic.RedisAtomicInteger 的源码时,发现了 Spring 的一个类型转换的工具类:DefaultConversionService

能够轻松的将 String 类型和其他类型进行相互转换。

 

用法:

private Converter converter = new Converter(new DefaultConversionService());

// org.springframework.data.redis.serializer.GenericToStringSerializer#deserialize
public T deserialize(@Nullable byte[] bytes) {

    if (bytes == null) {
        return null;
    }

    String string = new String(bytes, charset);
    //  进行类型转换
    return converter.convert(string, type);
}

 

posted on 2023-12-14 20:58  快鸟  阅读(21)  评论(0编辑  收藏  举报