首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

Spring中为bean注入Date对象

2024-12-13 来源:要发发知识网

比如我们有下面的一个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 
    
    
    
  
    <bean id="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 
    
    
    
  
    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd" />
    </bean>
  
    <bean id="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 
    
    
    
  
    <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" 
        <property name="date" value="2010-02-31" />
    </bean>
  
</beans>
显示全文