久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

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

瀏覽:164日期: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
主站蜘蛛池模板: 精品日韩欧美一区二区在线播放 | 欧美第8页 | 亚洲精品在线免费观看视频 | 中文字幕一区二区在线观看 | 欧美日韩一级电影 | av久久 | 在线色网站 | 中文字幕亚洲欧美 | 天天拍天天草 | 国产精品国产三级国产aⅴ原创 | 色综合久 | 日韩欧美国产精品综合嫩v 久久久久久国产精品高清 国产目拍亚洲精品99久久精品 | 欧美综合一区二区 | 国产黄色免费网站 | 欧美一级片在线 | 亚洲视频在线观看 | 久久激情网站 | 久久久久久久国产精品 | 亚洲国产欧美一区二区三区久久 | 国产探花| 亚洲国产精品久久 | 精品一区二区三区在线观看 | 33eee在线视频免费观看 | 欧美国产日韩一区 | 亚洲一区二区三区日韩 | 欧美一级免费 | 国产69精品久久久久观看黑料 | 成av在线| 国产精品久久久久一区二区三区 | 欧美一区二区三区在线观看视频 | 国产精品视频一区二区三区 | 亚洲狠狠爱一区二区三区 | 国产精品资源在线 | 国产午夜久久 | 国产精品久久久久久亚洲调教 | 亚洲精品在线免费看 | 91在线视频| 中文字幕一区二区三区四区 | 国产一区二区三区在线看 | 国产男女做爰免费网站 | 国产一区二区影院 |