美文网首页
Spring中为bean注入Date对象

Spring中为bean注入Date对象

作者: f07bfe4a10fd | 来源:发表于2016-03-07 14:35 被阅读390次

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

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,但是如果我们使用下面的配置:

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

然后我们尝试着运行的话

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,然后引用。如下解决:

<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对象。

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

相关文章

  • Spring中为bean注入Date对象

    比如我们有下面的一个bean: 注意我们上面的bean中有一个Date,但是如果我们使用下面的配置: 然后我们尝试...

  • spring_IOC总结(二)--xml依赖注入

    spring的bean对象--依赖注入 spring 创建bean对象细节 配置spring核心容器xml配置文件...

  • Spring基础

    @Autowired是什么?依赖注入的地方,前提是被注入的对象和注入的对象都要在Spring中注册为bean(利用...

  • Spring注解s

    @Autowired是什么?依赖注入的地方,前提是被注入的对象和注入的对象都要在Spring中注册为bean(利用...

  • Spring 学习心得(二)

    Spring IOC again 参数值注入 注入基本值 注入Bean对象 注入Spring表达式值 注入nul...

  • service dao手动实例化(new)导致类中的spring

    手动注入会报空指针异常 new对象 =null,使用new创建对象时,Spring注入的bean为null 使用标...

  • Bean注入方式

    六星教育 - Spring源码分析1909 默认单例,注入为同一个对象 @Bean配置类注入:bean类,配置类(...

  • Spring的主心骨

    Spring的主心骨 解析配置 定位与注册对象 注入对象 Bean&BeanDefinition Bean是Spr...

  • Spring各类集合注入

    原文:Spring各类集合注入摘要: 我们常在Spring的Bean中注入各种基本类型的值和对象引用,如果需要注入...

  • 装配 Spring Bean

    Spring 框架基于控制反转和依赖注入得以实现的自动装配 Bean 对象的功能,关于装配 Bean 对象,掌握下...

网友评论

      本文标题:Spring中为bean注入Date对象

      本文链接:https://www.haomeiwen.com/subject/yvnakttx.html