使用CustomEditorConfigurer时报错。

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> </constructor-arg> <constructor-arg value="true" /> </bean> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <ref local="dateEditor" /> </entry> </map> </property> </bean> <bean id="customer" class="com.mkyong.common.Customer"> <property name="date" value="2010-02-31" /> </bean> </beans>
在跟随mkyong的脚步使用customEditorConfigurer时报了错,无法进行下去
错误1:ref 的local属性在springframework3.0后就废除了,于是我用bean代替,bean是多个xml文件都能扫描的吧,这个应该没问题。
改完后紧接着还是报错:nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type
这个是由于3.0后customEditors改成一个
Map<Class<?>, Class<? extends PropertyEditor>> customEditors>的类型,也就是说这时候我们不能够再往里面key-object这样的结构了
这个就有点蛋疼了,于是我把第一个bean给删掉了,既然不能传对象那就把之前这个类型传进来吧。可是run的时候还是报错,提示没有默认构造器,由于,我们传进去的是两个类,
类不像对象一样可以在构造方法或是set方法进行依赖注入怎么办呢。。试了蛮多方法都不行,在springframework官网上找到答案
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
   <property name="customEditors">
     <map>
       <entry key="java.util.Date" value="mypackage.MyCustomDateEditor"/>
       <entry key="mypackage.MyObject" value="mypackage.MyObjectEditor"/>
     </map>
   </property>
 </bean>
可以这样搞,但是我没有定义过这种类型,于是打算定义一个类并继承CustomDateEditor这个类型,还是报错了(找不到构造器,看来我是挺蠢的。)没办法了,这招也用不了了,最后只能够抄了,既然CustomDateEditor能实现CustomEditorConfigurer要的东西,那么我就把刚才定义的类作为这个类的双胞胎。继承与CustomDateEditor的相同的超类,并且把它里面的属性方法拿出来设置到这个类里,并且初始化私有变量和CustomDateEditor一样,由于我的默认构造器和CustomDateEditor的不一样,我的默认构造器是不带参数的,并且对要设置的变量都设置了,于是当我把对应的xml改成
<!--<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">-->
<!--<property name="customEditors">-->
<!--<map>-->
<!--<entry key="java.util.Date" value="com.ric.demo.RicCustomerDateEditor" />-->
<!--</map>-->
<!--</property>-->
<!--</bean>-->
这种形式的时候就顺利输出了我要的结果

顺便说一下CustomEditorConfigurer最终实现Inject date的时候也是用的 bean-factory
可能其好处是这个customEditorConfigurer可是通过map的形式保存每个类(map的key)对应怎么初始化的(map的value)来共享bean-factory吧(猜的),不然我觉得使用bean-factory会简单的多。

自己记录一下,时间花多了。。
 



转载于:https://www.cnblogs.com/codetime/p/6291514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值