这一个Spring例子向您展示如何为bean属性注入一个“日期”。
package com.yiibai.common;

import java.util.Date;

public class Customer {

	Date date;

	public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} @Override
public String toString() {
return "Customer [date=" + date + "]";
} }
bean配置文件
<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="customer" class="com.yiibai.common.Customer">
<property name="date" value="2015-12-31" />
</bean> </beans>

执行-运行程序输出

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"SpringBeans.xml"); Customer cust = (Customer) context.getBean("customer");
System.out.println(cust); }
}
可能会遇到如下错误信息:
Caused by: org.springframework.beans.TypeMismatchException:
Failed to convert property value of type [java.lang.String] to
required type [java.util.Date] for property 'date'; nested exception is java.lang.IllegalArgumentException:
Cannot convert value of type [java.lang.String] to
required type [java.util.Date] for property 'date':
no matching editors or conversion strategy found

解决办法

在Spring中,可以通过两种方式注入日期:

1. Factory bean

声明一个dateFormat bean,在“customer” Bean,引用 “dateFormat” bean作为一个工厂bean。该工厂方法将调用SimpleDateFormat.parse()自动转换成字符串Date对象。
<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="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean> <bean id="customer" class="com.yiibai.common.Customer">
<property name="date">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2015-12-31" />
</bean>
</property>
</bean> </beans>

2. CustomDateEditor

声明一个 CustomDateEditor 类将字符串转换成 java.util.Date。
<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>
并声明另一个“CustomEditorConfigurer”,使 Spring转换 bean 属性,其类型为java.util.Date。
<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配置文件的完整例子(applicationContext.xml)。
<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.yiibai.common.Customer">
<property name="date" value="2015-12-31" />
</bean> </beans>
 
http://www.yiibai.com/spring/spring-how-to-pass-a-date-into-bean-property-customdateeditor.html

最新文章

  1. iOS --------Crash 分析(一)
  2. DDD 领域驱动设计-三个问题思考实体和值对象(续)
  3. mongodb学习(一)
  4. wordpress(三)wordpress手动更新
  5. python之Excel操作
  6. 利用nginx泛域名解析配置二级域名和多域名
  7. VC++多线程编程
  8. svn迁移
  9. NativeExcel 读取文件
  10. 使用ViewPager实现三个fragment切换
  11. docker registry 搭建
  12. python开发_tkinter_小球完全弹性碰撞游戏
  13. 浅谈SSRF漏洞
  14. 51nod 1201 整数划分 dp
  15. Python入门-三级菜单
  16. Node 杂技
  17. 使用mpVue开发小程序实战总结
  18. P(查准率),R(查全率),F1 值
  19. 启动HDFS
  20. xml与dataset的数据交互

热门文章

  1. UNDO自我理解总结
  2. 在Ubuntu上安装Redis MySQL MongoDB memcached Nginx
  3. 设计模式--工厂模式 caffe_layer注册
  4. webservice使用
  5. Ubuntu下Hadoop的安装和配置
  6. IDEA添加其他项目为库文件的方法
  7. python【项目】:选课系统【简易版】
  8. Codeforces 918C The Monster(括号匹配+思维)
  9. hdu 5895(矩阵快速幂+欧拉函数)
  10. Hadoop案例(二)压缩解压缩