spring4.0之后,自定义属性编辑器的配置变化
来源:互联网 发布:淘宝上的qq号能买吗 编辑:程序博客网 时间:2024/06/05 00:46
- 问题:spring中注入时间日期(java.util.Date)类型的属性的时候需要进行类型转换,因为spring不能直接注入Date类型。之前学习spring的时候是学的spring 2.5的版本,但是今天把spring的包都换成了spring 4.2 的,发现之前的出现错误。
具体的错误信息如下:
- log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
- log4j:WARN Please initialize the log4j system properly.
- Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customEditorConfigurer' defined in file [E:\Workspaces\MyEclipse 10\spring_utildate\bin\applicationContext-beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'customEditors'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.zm.bean.UtilDatePropertyEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [com.zm.bean.UtilDatePropertyEditor]
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
- at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
- at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
- at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
- at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:171)
- at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
- at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
- at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
- at com.zm.test.Test.main(Test.java:11)
- Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'customEditors'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [com.zm.bean.UtilDatePropertyEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [com.zm.bean.UtilDatePropertyEditor]
- at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:596)
- at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
- at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:204)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1527)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1486)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
- ... 11 more
- Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [com.zm.bean.UtilDatePropertyEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [com.zm.bean.UtilDatePropertyEditor]
- at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:298)
- at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:655)
- at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:222)
- at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
- ... 17 more
Spring 2.0的配置
bean类:
- package com.zm.bean;
- import java.util.Date;
- public class Bean1 {
- private Date dateValue;
- public Date getDateValue() {
- return dateValue;
- }
- public void setDateValue(Date dateValue) {
- this.dateValue = dateValue;
- }
- }
- package com.zm.bean;
- import java.beans.PropertyEditorSupport;
- import java.text.SimpleDateFormat;
- import java.text.ParseException;
- import java.util.Date;
- public class UtilDatePropertyEditor extends PropertyEditorSupport {
- private String format="yyyy-MM-dd";
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- // TODO Auto-generated method stub
- System.out.println("UtilDatePropertyEditor.setAsText() -- text=" + text);
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- try{
- Date d = sdf.parse(text);
- this.setValue(d);
- }catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void setFormat(String format) {
- this.format = format;
- }
- }
相关配置:
- <span style="white-space:pre"> </span><!-- 配置bean1 -->
- <bean id="bean1" class="com.zm.bean.Bean1">
- <property name="dateValue" value="2016-08-20"/>
- </bean>
- <!-- 配置自定义属性编辑器 -->
- <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
- <property name="customEditors">
- <map>
- <entry key="java.util.Date" >
- <bean class="com.zm.bean.UtilDatePropertyEditor">
- <property name="format" value="yyyy-MM-dd"/>
- </bean>
- </entry>
- </map>
- </property>
- </bean>
Spring 4.0的配置
相关配置:
- <span style="white-space:pre"> </span><!-- 配置bean1 -->
- <bean id="bean1" class="com.zm.bean.Bean1">
- <property name="dateValue" value="2016-08-20"/>
- </bean>
- <!-- 配置自定义属性编辑器 -->
- <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
- <property name="customEditors">
- <map>
- <entry key="java.util.Date" value="com.zm.bean.UtilDatePropertyEditor"/>
- </map>
- </property>
- </bean>
对比上面的配置可以发现,唯一的区别在于,在配置customEditors属性的时候,spring2.0配置的<entry>的value是一个<bean>,而spring4.0配置的<entry>的value是类名。
查看源码可以发现customEditors的类型,在spring2.0 中是Map类型,key和value的类型并不确定,但是底层处理的时候,key的类型必须是Class类型,而value是一个注入的bean;在spring4.0中,customEditors的类型变为了Map<Class<?>, Class<? extends PropertyEditor>>, 也就是说,key和value的类型已经确定为Class类型。
阅读全文
0 0
- spring4.0之后,自定义属性编辑器的配置变化
- spring4.0之后,自定义属性编辑器的配置变化
- Spring4.0后自定义属性编辑器配置的变化
- spring自定义属性编辑器的配置
- Spring4.0之后的新特性--泛型依赖注入
- android studio升级到3.0之后的一些配置变化
- Spring4----使用注解方式配置bean和bean的属性
- 怎样修改自定义活动的属性编辑器
- spring中自定义属性编辑器的实现
- spring的自定义属性编辑器CustomEditorConfigurer
- 一段spring4的配置
- 自定义属性编辑器
- 自定义属性编辑器
- Spring自定义属性编辑器
- Spring自定义属性编辑器
- springMVC自定义属性编辑器
- springMVC自定义属性编辑器
- springMVC自定义属性编辑器
- hdu 多校联赛 Is Derek lying?
- 主席树(Kth number,HDU 2665)
- hdfs查看目录大小文件大小
- 【ARM】控制寄存器的几种方式
- EasyHook 中部分函数的实现分析—-申请钩子
- spring4.0之后,自定义属性编辑器的配置变化
- 获取历史和实时股票数据接口
- redis过期时间的过期策略
- 第三章maven的DOS命令创建java、web项目
- HDU1235 统计同成绩学生人数
- linux下rabbitmq SimpleAmqpClient客户端的使用的简单例子
- uva 120
- AndroidStudio基础教程(一)
- redis的数据安全性和性能保障