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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  

目录
相关文章
|
22天前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
30 1
|
1月前
|
XML Java 开发者
Spring Boot中的bean注入方式和原理
Spring Boot中的bean注入方式和原理
51 0
|
1月前
|
XML 缓存 Java
Spring源码之 Bean 的循环依赖
循环依赖是 Spring 中经典问题之一,那么到底什么是循环依赖?简单说就是对象之间相互引用, 如下图所示: 代码层面上很好理解,在 bean 创建过程中 class A 和 class B 又经历了怎样的过程呢? 可以看出形成了一个闭环,如果想解决这个问题,那么在属性填充时要保证不二次创建 A对象 的步骤,也就是必须保证从容器中能够直接获取到 B。 一、复现循环依赖问题 Spring 中默认允许循环依赖的存在,但在 Spring Boot 2.6.x 版本开始默认禁用了循环依赖 1. 基于xml复现循环依赖 定义实体 Bean java复制代码public class A {
|
1月前
|
存储 NoSQL Java
Spring Boot统计一个Bean中方法的调用次数
Spring Boot统计一个Bean中方法的调用次数
33 1
|
2月前
|
Java 索引 Spring
spring创建bean的过程
spring创建bean的过程
|
3月前
|
缓存 Java Spring
Spring循环依赖原理和Bean创建过程
Spring循环依赖原理和Bean创建过程
69 0
|
7天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
17 1
|
11天前
|
XML Java 数据格式
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
19 0
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
|
22天前
|
XML Java 程序员
作为Java程序员还不知道Spring中Bean创建过程和作用?
作为Java程序员还不知道Spring中Bean创建过程和作用?
14 0
|
26天前
|
XML 缓存 Java
天天用 Spring,bean 实例化原理你懂吗
天天用 Spring,bean 实例化原理你懂吗
17 0