Spring 自定义属性编辑(CustomEditorConfigurer)和类型转换器(ConversionServiceFactoryBean)一起配置问题
2017-02-13 00:44
411 查看
问题:
现在这样一种需求, 有一个bean它的属性是java.util.Date类型,我们要在spring的xml配置初始化它,怎么做呢
解决方案:
可以说spring的属性编辑器和类型转换器都是做类型转换的,但属性编辑器多为string转其它类型,
方法1:
添加属性编辑器:<!-- 日期属性编辑 --> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean class="com.UtilDatePropertyEditor"> <property name="formate" value="yyyy-MM-dd HH:mm:ss"></property> </bean> </entry> </map> </property> </bean>
java:
import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class UtilDatePropertyEditor extends PropertyEditorSupport { private String formate; @Override public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formate); try { Date date = simpleDateFormat.parse(text); this.setValue(date); } catch (ParseException e) { e.printStackTrace(); } } public void setFormate(String formate) { this.formate = formate; } }
方法二:
注册类型转换器
Configuring a ConversionService
A ConversionService is a stateless object designed to be instantiated at application startup, then shared between multiple threads. In a Spring application, you typically configure a ConversionService instance per Spring container (or ApplicationContext). ThatConversionService will be picked up by Spring and then used whenever a type conversion needs to be performed by the framework. You may also inject this ConversionService into any of your beans and invoke it directly.
Note | |
---|---|
If no ConversionService is registered with Spring, the original PropertyEditor-based system is used. |
conversionService:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
A default ConversionService can convert between strings, numbers, enums, collections, maps, and other common types. To suppliment or override the default converters with your own custom converter(s), set the
convertersproperty.
Property values may implement either of the Converter, ConverterFactory, or GenericConverter interfaces.
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="example.MyCustomConverter"/> </list> </property> </bean>
以上为spring 官方reference关于注册类型转换器的介绍,其中说到"T o
suppliment or override the default converters with your own custom converter(s), set the
convertersproperty.
Property values may implement either of the Converter, ConverterFactory, or GenericConverter interfaces. "
告诉我们可能实以 Converter, ConverterFactory, or GenericConverter这三个接口中的一个和相应配置来补充类型转换
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="String2DateConverter" /> </list> </property> </bean>
import java.text.ParseException; import java.util.Date; import org.apache.commons.lang.time.DateUtils; import org.springframework.core.convert.converter.Converter; public class String2DateConverter implements Converter<String, Date> { @Override public Date convert(String arg0) { try { return DateUtils.parseDate(arg0, new String[] { "yyyy-MM-dd HH:mm:ss" }); } catch (ParseException e) { return null; } } }
java.lang.IllegalArgumentException: Left-hand side type must not be null 异常
以上两种方法都可满足需求,但是不能一起使用,一起使用就会出现这个问题,这是spring的一个bug,所以我们必须选择其中一种,我个人更倾向于第二种bug描述 : https://jira.springsource.org/browse/SPR-6807
其中说到: ConversionService fails with CustomEditorConfigurer. While it is not recommended to mix the two in the same context it is unavoidable
spring 相关api: http://static.springsource.org/spring/docs/3.0.x/reference/validation.html
相关文章推荐
- Spring 自定义属性编辑(CustomEditorConfigurer)和类型转换器(ConversionServiceFactoryBean)一起配置问题
- Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
- spring-mvc配置异常问题FormattingConversionServiceFactoryBean
- Spring MVC 參數映射轉換(CustomEditorConfigurer)和类型转换器(ConversionServiceFactoryBean)
- Spring MVC 参数转换(CustomEditorConfigurer)和类型转换器(ConversionServiceFactoryBean)
- 学习springmvc的第十三天(自定义类型转换器ConversionServiceFactoryBean)
- Info模式下的隐形杀手(SpringMVC同时使用<mvc:resources.../>和FormattingConversionServiceFactoryBean时出现的问题)
- Hibernate与Spring整合的一些配置问题(一)——LocalSessionFactoryBean
- spring配置文件bean标签 name属性取值语法问题,(新手问题多)--小问题总结都在这里
- spring利用PropertiesFactoryBean管理属性配置文件properties
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- mybatis配置通用dao和service时出现org.springframework.beans.factory.BeanCreationException异常
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- spring管理属性配置文件properties——PropertiesFactoryBean和PropertyPlaceholderConfigurer的区别
- springmvc之自定义类型转换器ConversionServiceFactoryBean
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- 使用Spring的JndiObjectFactoryBean配置数据源时需要注意的问题