类型转换

Spring 3 引入了一个core.convert提供通用类型转换系统的包。系统定义了一个 SPI 来实现类型转换逻辑和一个 API 来在运行时执行类型转换。在 Spring 容器中,您可以使用此系统作为实现的替代PropertyEditor方案,将外部化的 bean 属性值字符串转换为所需的属性类型。您还可以在应用程序中需要类型转换的任何地方使用公共 API。

转换器 SPI

实现类型转换逻辑的 SPI 很简单,而且是强类型的

它是一个典型的Converter实现:

package org.springframework.core.convert.support;

final class StringToInteger implements Converter<String, Integer> {

    public Integer convert(String source) {
return Integer.valueOf(source);
}
}

使用ConverterFactory

当您需要集中整个类层次结构的转换逻辑时(例如,从转换StringEnum对象时),您可以实现 ConverterFactory

考虑StringToEnumConverterFactory作为一个例子:

package org.springframework.core.convert.support;

final class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {

    public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnumConverter(targetType);
} private final class StringToEnumConverter<T extends Enum> implements Converter<String, T> { private Class<T> enumType; public StringToEnumConverter(Class<T> enumType) {
this.enumType = enumType;
} public T convert(String source) {
return (T) Enum.valueOf(this.enumType, source.trim());
}
}
}

使用GenericConverter

当您需要复杂的Converter实现时,请考虑使用 GenericConverter接口。

ConversionService

ConversionService定义了一个统一的 API,用于在运行时执行类型转换逻辑。转换器通常在以下外观接口后面运行

大多数ConversionService实现还实现ConverterRegistry了 ,它提供了一个用于注册转换器的 SPI。在内部,ConversionService 实现委托其注册的转换器执行类型转换逻辑。

ConversionService包中提供了一个健壮的实现core.convert.supportGenericConversionService是适用于大多数环境的通用实现。为创建通用配置ConversionServiceFactory提供了一个方便的工厂。

配置一个ConversionService

ConversionService是一个无状态对象,旨在在应用程序启动时实例化,然后在多个线程之间共享。在 Spring 应用程序中,您通常ConversionService为每个 Spring 容器(或ApplicationContext)配置一个实例。ConversionService每当框架需要执行类型转换时,Spring 就会选择并使用它。您还可以将其 ConversionService注入任何 bean 并直接调用它。

要使用 Spring 注册默认值。

<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean"/>

默认值ConversionService可以在字符串、数字、枚举、集合、映射和其他常见类型之间进行转换。要使用您自己的自定义转换器补充或覆盖默认转换器,请设置该converters属性。属性值可以实现任何ConverterConverterFactoryGenericConverter接口。

<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="example.MyCustomConverter"/>
</set>
</property>
</bean>

ConversionService编程方式使用

要以ConversionService编程方式使用实例,您可以像对任何其他 bean 一样注入对它的引用。以下示例显示了如何执行此操作:

@Service
public class MyService { public MyService(ConversionService conversionService) {
this.conversionService = conversionService;
} public void doIt() {
this.conversionService.convert(...)
}
}

对于大多数用例,您可以使用convert指定 的方法targetType,但它不适用于更复杂的类型,例如参数化元素的集合。

Spring系列之类型转换-12的更多相关文章

  1. Spring 系列教程之容器的功能

    Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...

  2. Spring系列之Spring常用注解总结 转载

    Spring系列之Spring常用注解总结   传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...

  3. Spring 系列目录

    Spring(https://spring.io/) 系列目录 第一篇:Spring 系列 第一章 Spring Core (1) Convert 1.1.1 Spring ConversionSer ...

  4. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  5. Spring系列(三):Spring IoC源码解析

    一.Spring容器类继承图 二.容器前期准备 IoC源码解析入口: /** * @desc: ioc原理解析 启动 * @author: toby * @date: 2019/7/22 22:20 ...

  6. Spring 系列之jdbcTemplate的使用

    Spring系列之 jdbcTemplate 啥是jdncTemplate? t他是spring框架中提供的一个对象,是对原始的jdbcAPI对象的简单封装,spring框架为我们提供了很多操作,模板 ...

  7. Spring系列 SpringMVC的请求与数据响应

    Spring系列 SpringMVC的请求与数据响应 SpringMVC的数据响应 数据响应的方式 y以下案例均部署在Tomcat上,使用浏览器来访问一个简单的success.jsp页面来实现 Suc ...

  8. Spring系列14:IoC容器的扩展点

    Spring系列14:IoC容器的扩展点 回顾 知识需要成体系地学习,本系列文章前后有关联,建议按照顺序阅读.上一篇我们详细介绍了Spring Bean的生命周期和丰富的扩展点,没有阅读的强烈建议先阅 ...

  9. Spring 系列: Spring 框架简介 -7个部分

    Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...

  10. Spring 系列: Spring 框架简介

    Spring AOP 和 IOC 容器入门(转载) 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级的.强壮的 J2EE 应用程序.dev ...

随机推荐

  1. python之目录结构01

    本文档主要是自己学习巩固以及复习之用,主要写些自己的学习体会! 以下为一个简要的目录构: Foo/ |-- bin/ | |-- foo | |-- foo/ | |-- tests/ | | |-- ...

  2. css3中-webkit是什么意思

    在CSS样式中很多样式名前缀都带有'-webkit-',但在CSS提供的API中查询不到这些样式名. 原因:CSS3中新增了一些属性,针对不同的浏览器,规定其内核名称让它们可以对这些新增属性进行解析. ...

  3. 《Rust权威指南》学习笔记——8.通用集合类型

    Rust通用集合类型 动态数组Vec 字符串String 和&str 哈希映射HashMap

  4. openwrt 配置 单网卡多IP

    config interface 'wan0' option ifname 'eth1' option proto 'static' option nat '1' option mtu '1500' ...

  5. 更改ubuntu分辨率

    显示器是1920*1080的,ubuntu20里没有,查了一通,修改成功,过程如下: 1.打开终端,输入xrandr, 我用的虚拟机,记下Virtual1 connected primary 1920 ...

  6. zookeeper 选举流程源码解析

    在开始之前,我们先了解一下zookeeper集群角色,zookeeper中存在leader,follower, observer这么几个角色, leader, follower, 就类似与mysql ...

  7. div垂直居中的4种方式方式

    一.使用单元格居中 <!DOCTYPE html> <html> <head> <title>测试</title> </head> ...

  8. 头条二面:宕机后,Redis如何实现快速恢复?

    Redis作为非常火热的内存数据库,其除了具有非常高的性能之外,还需要保证高可用,在故障发生时,尽可能地降低故障带来的影响,Redis也提供了完善的故障恢复机制:哨兵.下面就来具体来看看Redis的故 ...

  9. svn 报 is not a working copy 错误

    当时提交代码 svn  报 is not a working copy ,上网查找问题  要我重新拉代码下来 然后放进修改的代码重新提交,我觉得很不合理,我看了下我提交的代码文件有80多个,我在想是否 ...

  10. Collections.synchronizedList使用方法陷阱(1)

    无意发现了这个例子,拿来记住 @NotThreadSafeclass BadListHelper <E> {    public List<E> list = Collecti ...