Spring系列之类型转换-12
目录
类型转换
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
当您需要集中整个类层次结构的转换逻辑时(例如,从转换String为Enum对象时),您可以实现 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.support 。GenericConversionService是适用于大多数环境的通用实现。为创建通用配置ConversionServiceFactory提供了一个方便的工厂。
配置一个ConversionService
ConversionService是一个无状态对象,旨在在应用程序启动时实例化,然后在多个线程之间共享。在 Spring 应用程序中,您通常ConversionService为每个 Spring 容器(或ApplicationContext)配置一个实例。ConversionService每当框架需要执行类型转换时,Spring 就会选择并使用它。您还可以将其 ConversionService注入任何 bean 并直接调用它。
要使用 Spring 注册默认值。
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean"/>
默认值ConversionService可以在字符串、数字、枚举、集合、映射和其他常见类型之间进行转换。要使用您自己的自定义转换器补充或覆盖默认转换器,请设置该converters属性。属性值可以实现任何Converter、ConverterFactory或GenericConverter接口。
<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的更多相关文章
- Spring 系列教程之容器的功能
Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...
- Spring系列之Spring常用注解总结 转载
Spring系列之Spring常用注解总结 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...
- Spring 系列目录
Spring(https://spring.io/) 系列目录 第一篇:Spring 系列 第一章 Spring Core (1) Convert 1.1.1 Spring ConversionSer ...
- Spring 系列教程之 bean 的加载
Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...
- Spring系列(三):Spring IoC源码解析
一.Spring容器类继承图 二.容器前期准备 IoC源码解析入口: /** * @desc: ioc原理解析 启动 * @author: toby * @date: 2019/7/22 22:20 ...
- Spring 系列之jdbcTemplate的使用
Spring系列之 jdbcTemplate 啥是jdncTemplate? t他是spring框架中提供的一个对象,是对原始的jdbcAPI对象的简单封装,spring框架为我们提供了很多操作,模板 ...
- Spring系列 SpringMVC的请求与数据响应
Spring系列 SpringMVC的请求与数据响应 SpringMVC的数据响应 数据响应的方式 y以下案例均部署在Tomcat上,使用浏览器来访问一个简单的success.jsp页面来实现 Suc ...
- Spring系列14:IoC容器的扩展点
Spring系列14:IoC容器的扩展点 回顾 知识需要成体系地学习,本系列文章前后有关联,建议按照顺序阅读.上一篇我们详细介绍了Spring Bean的生命周期和丰富的扩展点,没有阅读的强烈建议先阅 ...
- Spring 系列: Spring 框架简介 -7个部分
Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...
- Spring 系列: Spring 框架简介
Spring AOP 和 IOC 容器入门(转载) 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级的.强壮的 J2EE 应用程序.dev ...
随机推荐
- 深度剖析CPython解释器》Python内存管理深度剖析Python内存管理架构、内存池的实现原理
目录 1.楔子 第1层:基于第0层的"通用目的内存分配器"包装而成. 第2层:在第1层提供的通用 *PyMem_* 接口基础上,实现统一的对象内存分配(object.tp_allo ...
- DAC双通道模板
#define DAC_C #include "dac.h" float DAC_DispenseA; float DAC_DispenseB; void MyDAC_Init(v ...
- 【踩坑】lua加载模块但找不到模块最蠢的原因
这个问题比较蠢,我用MinGW编译的lua去加载了MSVC编译的lua模块导致找不到符号,然后花了几个小时找为什么我VS项目使用函数导出接口了但是函数依然没有导出(使用dumpbin和nm都能看到导出 ...
- zookeeper(1)-集群的搭建
集群搭建 1. 下载二进制文件 $ wget --no-check-certificate https://mirrors.ustc.edu.cn/apache/zookeeper/zookeeper ...
- Jenkins拉取GitHub上代码
1.github 生成 Personal Access Token 2.github 设置 GitHub webhooks (具体需要持续集成的项目),新建或者设置现有项目的 webhooks 选项, ...
- Oracle虚拟机与主机共享设置
VM中linux与主机的文件共享 1.打开 Oracle VM VirtualBox 点击 [控制] [设置] [数据空间] 添加你所希望共享的文件夹
- MySQL时区的问题
我这里是在application.properties文件中配置的MySQL连接信息. 开始时间显示不征程是因为没有配置时区,后来加上了setTimeZone=Asia/Shanghai,时间显示正常 ...
- 什么是axios
原文: https://blog.csdn.net/qq_40837310/article/details/123028044 1.使用格式和jquery的ajax很相似,和最初的相比可以链式调用,1 ...
- nginx: the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
Nginx如果未开启SSL模块,配置Https时将提示如题错误 原因:nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了,但是现 ...
- Docker的资源限制
CPU CGROUP MEM CGROUP Storage 默认的overlay fs不支持配额,需要底层文件系统如xfs,ext4的支持. 在docker中启用示例如下,限制单个容器最大使用空间为2 ...