日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring注入日期到bean属性-CustomDateEditor

發布時間:2024/9/20 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring注入日期到bean属性-CustomDateEditor 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這一個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;}@Overridepublic 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/beanshttp://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] torequired 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/beanshttp://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/beanshttp://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://pan.baidu.com/s/1bxZyfO http://www.yiibai.com/spring/spring-how-to-pass-a-date-into-bean-property-customdateeditor.html

總結

以上是生活随笔為你收集整理的Spring注入日期到bean属性-CustomDateEditor的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。