关于Spring框架中自动转换日期类型的问题

Sniper2016 2016-09-14 02:35:47

首先一个实体类,封装date,设置getter setter方法

package entity;

import java.io.Serializable;
import java.util.Date;

@SuppressWarnings("serial")
public class Day implements Serializable{
private Date date;

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


然后是applicationContext.xml文件的配置,我用setter方法给date注入值,写一个工具让其自动转换成date格式


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<bean id="day" class="entity.Day">
<!--这里的value我输入字符串类型的日期格式-->
<property name="date" value="2008-08-08"></property>
</bean>
<!--配置转换器-->
<bean id="datePropertyConvert" class="util.DatePropertyConvert"></bean>

<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<!--指定转化器要转化的目标类型-->
<entry key="java.util.Date" value-ref="datePropertyConvert"></entry>
</map>
</property>
</bean>

</beans>


写一个工具类来转换日期格式

package util;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DatePropertyConvert extends PropertyEditorSupport {
private String formart = "yyyy-MM-dd";

@Override
public void setAsText(String text) throws IllegalArgumentException {
//获取日期格式化对象
SimpleDateFormat sdf=new SimpleDateFormat(formart);

try {
//将字符串类型的日期转换成Date类型
Date date = sdf.parse(text);
setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}


}

@Override

public void setValue(Object value) {
super.setValue(value);
}

@Override
public String getAsText() {
return super.getAsText();
}
}

之后是测试类

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import entity.Day;

public class Test {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Day d=(Day)ctx.getBean("day");
System.out.println(d.getDate());

}
}



然后运行测试类报出如下异常
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customEditorConfigurer' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'datePropertyConvert' while setting bean property 'customEditors' with key [TypedStringValue: value [java.util.Date], target type [null]]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [util.DatePropertyConvert] for bean with name 'datePropertyConvert' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: util.DatePropertyConvert

求解
...全文
211 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sniper2016 2016-09-14
  • 打赏
  • 举报
回复
找到了!原来是我util包名写成了utli 改过之后运行通过了 谢谢各位
soton_dolphin 2016-09-14
  • 打赏
  • 举报
回复
<bean id="datePropertyConvert" class="util.DatePropertyConvert"></bean> 这个tag是不是关早了??

<bean id="datePropertyConvert" class="util.DatePropertyConvert">
 
      <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
           <map>
<!--指定转化器要转化的目标类型-->
             <entry key="java.util.Date" value-ref="datePropertyConvert"></entry>
           </map>
        </property>
      </bean>
</bean>
hylrf0 2016-09-14
  • 打赏
  • 举报
回复
检查下applicationContext.xml是否能被读取到,还是说需要在前面加classpath:,从报错来看是找不到DatePropertyConvert这个类

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧