在Spring中使用PropertyEditor或Converter 实现Object 和 String 之间的转换
在Spring中使用PropertyEditor或Converter 实现Object 和 String 之间的转换
PropertyEditor
使用范围:
-
在 BeanWrapper 上注册自定义编辑器:
void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor);
-
特定的 IoC 容器中注册自定义编辑器:
-
把PropertyEditor 类与它们处理的类放在同一个包中,并且名称与该类相同,并且添加了 Editor:
com chank pop Something SomethingEditor // the PropertyEditor for the Something class 或者: com chank pop Something SomethingBeanInfo // the BeanInfo for the Something class
-
使用BeanFactory 引用,具体可以使用 ConfigurableBeanFactory 接口的 registerCustomEditor ()方法,但不被Spring推荐
-
创建和使用 PropertyEditorRegistry,使用CustomEditorConfigurer这个bean 工厂后处理器propertyEditorRegistrars属性进行注册或者配置customEditors属性
-
示例
定义了一个名为 ExoticType 的用户类和另一个名为 DependsOnExoticType 的类,后者需要将 ExoticType 设置为属性:
public class ExoticType {
private String name;
public ExoticType(String name) {
this.name = name;
}
}
public class DependsOnExoticType {
private ExoticType type;
public void setType(ExoticType type) {
this.type = type;
}
}
将 type 属性分配为字符串,PropertyEditor 将其转换为实际的 ExoticType 实例。下面的 bean 定义显示了如何建立这种关系:
<bean id="sample" class="example.DependsOnExoticType">
<property name="type" value="aNameForExoticType"/>
</bean>
PropertyEditor 的实现可能类似于以下内容:
package example;
public class ExoticTypeEditor extends PropertyEditorSupport {
public void setAsText(String text) {
setValue(new ExoticType(text.toUpperCase()));
}
}
路径配置:
也可以通过:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="example.ExoticType" value="example.ExoticTypeEditor"/>
</map>
</property>
</bean>
Converter
Converter 可以作为 PropertyEditor 实现的替代方法,要创建自己的转换器,需要实现 Converter 接口并将 S 参数化为要转换的类型,将 T 参数化为要转换的类型。如果需要将 S 的集合或数组转换为数组或 T 的集合,也可以应用这样的转换器,前提是委托数组或集合转换器也已注册不过默认情况下 DefaultConversionService 已经注册。
Spring 默认实现了一些转换器:
package org.springframework.core.convert.support;
final class StringToInteger implements Converter<String, Integer> {
public Integer convert(String source) {
return Integer.valueOf(source);
}
}
如果需要定义更大范围内的转换逻辑可以实现 ConverterFactory:
package org.springframework.core.convert.converter;
public interface ConverterFactory<S, R> {
<T extends R> Converter<S, T> getConverter(Class<T> targetType);
}
例如,Spring实现的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());
}
}
}
类型转换逻辑定义了统一的 API:ConversonService
package org.springframework.core.convert;
public interface ConversionService {
boolean canConvert(Class<?> sourceType, Class<?> targetType);
<T> T convert(Object source, Class<T> targetType);
boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType);
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
}
在 Spring 应用程序中,通常为每个 Spring 容器(或 ApplicationContext)配置一个 ConversonService 实例。每当框架需要执行类型转换时,Spring 就会获取 ConversonService 并使用它。还可以将此 ConversonService 注入到任何 bean 中并直接调用它。
若要使用自定义转换器补充或重写默认转换器,可以设置转换器属性converters:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix