Spring注入Date類型的三種方法總結(jié)
Spring注入Date類型的三種方法總結(jié)
測(cè)試Bean:
public class DateBean { private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
方式1:利用SimpleDateFormat的構(gòu)造方法注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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.xsd"> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="datebean" class="com.springDemo1.Date類型注入.DateBean"> <property name="birthday"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="2015-12-31" /> </bean> </property> </bean> </beans>
方式2:純配置,先自定義CustomDateEditor,再轉(zhuǎn)換類型
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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.xsd"> <!-- 自定義日期編輯器 --> <bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor"> <constructor-arg> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd"></constructor-arg> </bean> </constructor-arg> <constructor-arg value="true" /> </bean> <!-- 使 Spring轉(zhuǎn)換為java.util.Date --> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <ref bean="dateEditor" /> </entry> </map> </property> </bean> </beans>
方式3:先用一個(gè)類重寫PropertyEditorSupport的setAsText方法,再在配置文件中,配置轉(zhuǎn)換類型就可以了,跟上面方法類似
public class MyDatePropertyEditor extends PropertyEditorSupport { private String format; public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } // text為需要轉(zhuǎn)換的值,當(dāng)為bean注入的類型與編輯器轉(zhuǎn)換的類型匹配時(shí)就會(huì)交給setAsText方法處理 public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat sdf = new SimpleDateFormat(format); try { this.setValue(sdf.parse(text)); } catch (ParseException e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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.xsd"> <!--方式3:創(chuàng)建一個(gè)類 重寫PropertyEditorSupport的setAsText方法 --> <!-- 自定義日期編輯器 --> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <!--需要編輯的屬性類型,是一個(gè)map --> <map> <entry key="java.util.Date"> <bean class="com.springDemo1.Date類型注入.MyDatePropertyEditor"> <property name="format" value="yyyy-MM-dd" /> <!--注入需要轉(zhuǎn)換的格式 --> </bean> </entry> </map> </property> </bean> </beans>
測(cè)試:
public class DateTest { @Test public void testName() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); DateBean bean = (DateBean) context.getBean("datebean"); System.out.println(bean.getBirthday()); } }
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 使用SpringMVC的@Validated注解驗(yàn)證的實(shí)現(xiàn)
- 詳解Spring 參數(shù)驗(yàn)證@Validated和@Valid的區(qū)別
- Spring Boot LocalDateTime格式化處理的示例詳解
- springboot mybatis里localdatetime序列化問題的解決
- spring boot @ResponseBody轉(zhuǎn)換JSON 時(shí) Date 類型處理方法【兩種方法】
- 解決Spring Boot和Feign中使用Java 8時(shí)間日期API(LocalDate等)的序列化問題
- Spring shiro + bootstrap + jquery.validate 實(shí)現(xiàn)登錄、注冊(cè)功能
- 基于springboot處理date參數(shù)過程解析
相關(guān)文章
SpringMVC 數(shù)據(jù)綁定實(shí)例詳解
這篇文章主要介紹了 SpringMVC 數(shù)據(jù)綁定實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04詳解hibernate自動(dòng)創(chuàng)建表的配置
這篇文章主要介紹了詳解hibernate自動(dòng)創(chuàng)建表的配置的相關(guān)資料,需要的朋友可以參考下2017-06-06JavaWeb實(shí)現(xiàn)圖形報(bào)表折線圖的方法
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)圖形報(bào)表折線圖的方法,涉及JSP包的引用、圖形操作、配置文件設(shè)置及字符串操作技巧,需要的朋友可以參考下2016-06-06請(qǐng)求轉(zhuǎn)發(fā)jsp頁(yè)面亂碼問題的快速解決方法
下面小編就為大家?guī)?lái)一篇請(qǐng)求轉(zhuǎn)發(fā)jsp頁(yè)面亂碼問題的快速解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-08-08JSP實(shí)現(xiàn)百萬(wàn)富翁猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了JSP實(shí)現(xiàn)百萬(wàn)富翁猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Spring 中 @Service 和 @Resource 注解的區(qū)別
這篇文章主要介紹了Spring @Service 和 @Resource 注解的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-03-03jsp使用ECharts動(dòng)態(tài)在地圖上標(biāo)識(shí)點(diǎn)
echarts地圖展示功能很強(qiáng)大,官網(wǎng)上靜態(tài)展示的例子很多了,動(dòng)態(tài)的資料少,需要參考本文的可以進(jìn)來(lái)了解一下。2016-10-10