• 使用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会简单的多。

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



  • 相关阅读:
    C语言博客05--指针
    网络1911、1912 D&S第2次作业--批改总结
    JAVA课程设计——愤怒的小鸟(个人)
    JAVA课程设计——愤怒的小鸟(团队)
    网络1911、1912 C语言第1次作业批改总结
    Python--安装第三方库的方法
    Eclipse中文插件安装教程
    DS博客作业08--课程总结
    DS博客作业07--查找
    DS博客作业06--图
  • 原文地址:https://www.cnblogs.com/codetime/p/6291514.html
Copyright © 2020-2023  润新知