亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:191日期:2022-06-07 09:22:43

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"> 
  <constructor-arg value="yyyy-MM-dd" /> 
  </bean> 
   
  <bean id="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" 
   > 
    <constructor-arg> 
      <bean> 
<constructor-arg value="yyyy-MM-dd"></constructor-arg> 
      </bean> 
    </constructor-arg> 
    <constructor-arg value="true" /> 
  </bean> 
  <!-- 使 Spring轉(zhuǎn)換為java.util.Date --> 
  <bean> 
    <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> 
    <property name="customEditors"> <!--需要編輯的屬性類型,是一個(gè)map --> 
      <map> 
<entry key="java.util.Date"> 
  <bean> 
    <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ì)本站的支持!

標(biāo)簽: JSP
主站蜘蛛池模板: 国产丝袜福利 | 精品视频在线观看 | 99久久综合狠狠综合久久 | 拍拍拍拍拍拍拍无挡大全免费 | 国产a v高清一区二区三区 | 国产啪精品视频网给免丝袜 | 五月天婷婷在线视频国产在线 | 日日摸夜夜搂人人要 | 国产在线视频专区 | 皮皮在线精品亚洲 | 久久香蕉国产线看观看8青草 | 精品一区二区三区色花堂 | 高清国产一级毛片国语 | 久久久亚洲国产精品主播 | 在线观看91精品国产hd | 黄色免费一级播放片 | 国产日韩亚洲 | 国产精品麻豆久久99 | 国产一级免费在线观看 | 国产精品人成 | 国产极品嫩模大尺度福利视频 | 一级毛片在线直接观看 | 国产网红主播精品福利大秀专区 | 91免费高清视频 | 国产亚洲精品色一区 | 亚洲综合色就色手机在线观看 | 俄罗斯aa毛片极品 | 你懂的成人 | 日韩a无吗一区二区三区 | 91亚洲国产 | 色婷婷六月丁香在线观看 | www草逼| 免费看一级黄色 | 久久91精品国产91久久跳舞 | 国产黄色片在线观看 | 麻豆国产96在线 | 日韩 | 国产精品视频福利 | 午夜影院亚洲 | 欧美性视频一区二区三区 | 欧美成人免费全部观看天天性色 | 在线看黄色 |