濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Spring注入Date類(lèi)型的三種方法總結(jié)

Spring注入Date類(lèi)型的三種方法總結(jié)

熱門(mén)標(biāo)簽:如何看懂地圖標(biāo)注點(diǎn) 自繪地圖標(biāo)注數(shù)據(jù) 海外圖書(shū)館地圖標(biāo)注點(diǎn) 給地圖標(biāo)注得傭金 電話(huà)機(jī)器人需要使用網(wǎng)絡(luò)嗎 電銷(xiāo)機(jī)器人免培訓(xùn) 南通通訊外呼系統(tǒng)產(chǎn)品介紹 潤(rùn)滑油銷(xiāo)售電銷(xiāo)機(jī)器人 外呼系統(tǒng)使用方法

Spring注入Date類(lèi)型的三種方法總結(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類(lèi)型注入.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)換類(lèi)型

?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è)類(lèi)重寫(xiě)PropertyEditorSupport的setAsText方法,再在配置文件中,配置轉(zhuǎn)換類(lèi)型就可以了,跟上面方法類(lèi)似

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注入的類(lèi)型與編輯器轉(zhuǎn)換的類(lèi)型匹配時(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è)類(lèi) 重寫(xiě)PropertyEditorSupport的setAsText方法 --> 
  !-- 自定義日期編輯器 --> 
  bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
    property name="customEditors"> !--需要編輯的屬性類(lèi)型,是一個(gè)map --> 
      map> 
        entry key="java.util.Date"> 
          bean class="com.springDemo1.Date類(lèi)型注入.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()); 
  } 
} 

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • 使用SpringMVC的@Validated注解驗(yàn)證的實(shí)現(xiàn)
  • 詳解Spring 參數(shù)驗(yàn)證@Validated和@Valid的區(qū)別
  • Spring Boot LocalDateTime格式化處理的示例詳解
  • springboot mybatis里localdatetime序列化問(wèn)題的解決
  • spring boot @ResponseBody轉(zhuǎn)換JSON 時(shí) Date 類(lèi)型處理方法【兩種方法】
  • 解決Spring Boot和Feign中使用Java 8時(shí)間日期A(yíng)PI(LocalDate等)的序列化問(wèn)題
  • Spring shiro + bootstrap + jquery.validate 實(shí)現(xiàn)登錄、注冊(cè)功能
  • 基于springboot處理date參數(shù)過(guò)程解析

標(biāo)簽:貸款邀約 黃石 南京 廣州 樂(lè)山 銅川 大連 內(nèi)江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Spring注入Date類(lèi)型的三種方法總結(jié)》,本文關(guān)鍵詞  Spring,注入,Date,類(lèi)型,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Spring注入Date類(lèi)型的三種方法總結(jié)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Spring注入Date類(lèi)型的三種方法總結(jié)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    仙游县| 汶川县| 石景山区| 博客| 盘山县| 达日县| 睢宁县| 南漳县| 平乐县| 临漳县| 安国市| 新竹县| 阳城县| 阿图什市| 凯里市| 宣武区| 南郑县| 宜兴市| 喜德县| 班戈县| 舒兰市| 张家界市| 弥勒县| 和田县| 米泉市| 安泽县| 额尔古纳市| 饶河县| 厦门市| 建平县| 漳平市| 奉新县| 方正县| 清丰县| 山西省| 兴安县| 如东县| 富川| 禹城市| 青河县| 绥棱县|