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

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

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

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

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

測試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:先用一個類重寫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)換的類型匹配時就會交給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" /> <!--注入需要轉(zhuǎn)換的格式 --> 
  </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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

標(biāo)簽: JSP
主站蜘蛛池模板: 美国特级黄 色大片 | 看毛片网| 日韩一区二区三区在线视频 | 成人免费网站久久久 | 国产欧美日韩综合精品二区 | 国产日韩精品一区二区在线观看播放 | 九九精品视频在线免费观看 | 老司机51精品视频在线观看 | 国产精品午夜寂寞视频 | 在线看污片 | 日本一级毛片免费 | 欧美性大片免费 | 91精品国产爱久久久久 | 亚洲欧美日本国产综合在线 | 亚洲在线看片 | 成年男女男免费视频网站不卡 | 最新三级网站 | 一级黄色片一级黄色片 | 伊人精品视频一区二区三区 | 全免费午夜真人毛片视频 | 日本一级毛片免费 | 欧美日韩一区二区三区高清不卡 | 永久在线观看www免费视频 | 在线观看黄网视频免费播放 | 国产性tv国产精品 | 一级黄色免费大片 | 黄色激情在线观看 | 国产成人精品视频在放 | 麻豆视频传媒入口 | 国产乱在线观看视频 | 午夜久久 | 小明看看在线观看 | 爱爱视频在线免费观看 | a级黄毛片 | 欧美日韩亚洲一区二区三区在线观看 | 日本一区二区三区久久 | 日韩综合 | 亚洲精品不卡午夜精品 | 久久久久久在线 | 亚洲欧洲日韩国产aa色大片 | 国产91精品一区二区 |