Spring中为bean注入Date对象

简介:

比如我们有下面的一个bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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中有一个Date,但是如果我们使用下面的配置:

1
2
3
4
5
6
7
8
9
10
< 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.mkyong.common.Customer">
         < property  name="date" value="2010-01-31" />
     </ bean >
  
</ beans >

  然后我们尝试着运行的话

1
2
3
4
5
6
7
8
9
10
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 foun
在这里提供两种解决办法:

1. Factory bean

声明一个dateFormat的bean,然后引用。如下解决:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< 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.mkyong.common.Customer">
         < property  name="date">
             < bean  factory-bean="dateFormat" factory-method="parse">
                 < constructor-arg  value="2010-01-31" />
             </ bean >
         </ property >
     </ bean >
  
</ beans >

  

2. CustomDateEditor

我们声明一个CustomDateEditor,将String转换为Date对象。

1
2
3
4
5
6
7
8
9
10
< 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 >

  

1
2
3
4
5
6
7
8
9
< bean  class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        < property  name="customEditors">
            < map >
                < entry  key="java.util.Date">
                    < ref  local="dateEditor" />
                </ entry >
            </ map >
        </ property >
    </ bean >

  完整的配置为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
< 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 >

  

 


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835191.html,如需转载请自行联系原作者
相关文章
|
2天前
|
XML Java 数据格式
Spring 属性注入方式
Spring 属性注入方式
12 2
|
6天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
27 4
|
8天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
18 1
|
10天前
|
JSON Java 数据处理
Spring Boot与Jsonson对象:灵活的JSON操作实战
【4月更文挑战第28天】在现代Web应用开发中,JSON数据格式的处理至关重要。假设 "Jsonson" 代表一个类似于Jackson的库,这样的工具在Spring Boot中用于处理JSON。本篇博客将介绍Spring Boot中处理JSON数据的基本概念,并通过实际例子展示如何使用类似Jackson的工具进行数据处理。
22 0
|
11天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
31 2
|
13天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
14天前
|
Java Spring
Spring⼯⼚创建复杂对象
Spring⼯⼚创建复杂对象
33 11
|
14天前
|
Java Spring 容器
Spring注入
Spring注入
30 13
|
21天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
21 1
|
26天前
|
XML Java 数据格式
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
22 0
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道