标签:convert Converter Spring PropertyEditor public class Class
在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:
标签:convert,Converter,Spring,PropertyEditor,public,class,Class 来源: https://www.cnblogs.com/kmchen/p/16515320.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。