gpt4 book ai didi

spring - Spring 4.0 中的字符串到日期转换

转载 作者:行者123 更新时间:2023-12-04 09:18:35 25 4
gpt4 key购买 nike

我在 Spring 4.0.0 M3 上自学
以下是代码,

bean

package org.chebus.springs;

import java.util.Date;

public class Traingle {
private String name;
private int height;
private Date date;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public void drawShape() {
System.out.println(getName() + " Traingle height " + getHeight()
+ " Date = " + getDate());
}

}


ApplicationContext ctx = new ClassPathXmlApplicationContext("org/chebus/springs/Spring.xml");
Traingle traingle = ctx.getBean("traingle",Traingle.class);
traingle.drawShape();

XML 配置
<?xml version="1.0" encoding="UTF-8"?>
<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 = "traingle" class="org.chebus.springs.Traingle">
<property name="name" value = "RightAngled"/>
<property name="height" value = "20"/>
<property name="date" value = "2013-09-10"/>
</bean>

<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>


</beans>

异常(exception):

java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.beans.propertyeditors.CustomDateEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.springframework.beans.propertyeditors.CustomDateEditor] at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:260) at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:620) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:205) at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459) ... 17 more



不知道我哪里出错了。感谢你的帮助。谢谢!

最佳答案

很好,这似乎是 Spring 4.0+ 的新行为,您的代码可以在 3.2.x 版本的 Spring 中正常工作。

原因似乎是因为 customEditors 的类型在CustomEditorConfigurer在 Spring 4.0+ 中发生了变化。而它的类型是 Map<String, ?>对于 Spring 3.2.x,它是 Map<Class<?>, Class<? extends PropertyEditor>>使用 Spring 4.0+。

解决方法是改为创建自定义 PropertyEditorRegistrar,如下所示:

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;

public class CustomDateEditorRegistrar implements PropertyEditorRegistrar {

@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
}
}

并在配置中使用它:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean class="dateeditor.CustomDateEditorRegistrar"/>
</list>
</property>
</bean>

关于spring - Spring 4.0 中的字符串到日期转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18706869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com