色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Spring注入Date類型的三種方法總結

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

Spring注入Date類型的三種方法總結

測試Bean:

public class DateBean { 
  private Date birthday; 
 
  public Date getBirthday() { 
    return birthday; 
  } 
 
  public void setBirthday(Date birthday) { 
    this.birthday = birthday; 
  } 
} 

方式1:利用SimpleDateFormat的構造方法注入

<?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,再轉換類型

<?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轉換為java.util.Date --> 
  <bean> 
    <property name="customEditors"> 
      <map> 
<entry key="java.util.Date"> 
  <ref bean="dateEditor" /> 
</entry> 
      </map> 
    </property> 
  </bean> 
</beans> 

方式3:先用一個類重寫PropertyEditorSupport的setAsText方法,再在配置文件中,配置轉換類型就可以了,跟上面方法類似

public class MyDatePropertyEditor extends PropertyEditorSupport { 
  private String format; 
 
  public String getFormat() { 
    return format; 
  } 
 
  public void setFormat(String format) { 
    this.format = format; 
  } 
 
  // text為需要轉換的值,當為bean注入的類型與編輯器轉換的類型匹配時就會交給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)建一個類 重寫PropertyEditorSupport的setAsText方法 --> 
  <!-- 自定義日期編輯器 --> 
  <bean> 
    <property name="customEditors"> <!--需要編輯的屬性類型,是一個map --> 
      <map> 
<entry key="java.util.Date"> 
  <bean> 
    <property name="format" value="yyyy-MM-dd" /> <!--注入需要轉換的格式 --> 
  </bean> 
</entry> 
      </map> 
    </property> 
  </bean> 
</beans> 

測試:

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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

標簽: JSP
主站蜘蛛池模板: 国产精品国产自线在线观看 | 久热草在线 | 在线观看精品国内福利视频 | 欧美亚洲日本国产综合网 | 国产在线高清不卡免费播放 | 国产日本韩国不卡在线视频 | 国产一区免费观看 | 二区久久国产乱子伦免费精品 | 日韩美女视频网站 | 午夜欧美精品久久久久久久久 | 亚洲理论片在线中文字幕 | 亚洲不卡一区二区三区在线 | 一级毛片视频在线 | 高跟丝袜美女一级毛片 | 美女张开腿让我桶 | www.av免费| 色多多最新地址福利地址 | 亚洲高清免费观看 | 久久久久久国产精品免费免费 | 成人a在线观看 | 国产精品免费一区二区三区 | 国产精品拍拍拍福利在线观看 | 国产精品99久久久久久www | 国产成人精品免费视频大全五级 | 日本特黄特色免费大片 | 亚洲精品tv久久久久 | 国产精品揄拍一区二区 | 超级碰碰碰视频视频在线视频 | 99re8免费视频精品全部 | 一级女性全黄久久生活片免费 | 国产精品情人露脸在线观看 | 日韩高清在线不卡 | 97成人精品视频在线播放 | 91精品国产色综合久久不 | aaa免费视频 | 国产成人18黄网站免费 | 亚洲成人免费在线 | 久久亚洲综合 | 欧美性色生活片天天看99 | 久草视频在线首页 | 亚洲成人影院在线观看 |