av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

瀏覽:183日期: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:創建一個類 重寫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()); 
  } 
} 

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

標簽: JSP
主站蜘蛛池模板: 国产中文字幕网 | 免费黄色录像视频 | 国产精品久久久久影院色老大 | 91视视频在线观看入口直接观看 | 亚欧洲精品在线视频免费观看 | 中文字幕日韩专区 | 91成人在线视频 | 欧美精品一区在线发布 | 国产精品美女久久久久久久久久久 | 91精品国产麻豆 | 亚洲福利视频一区二区 | 久久久成人一区二区免费影院 | 欧美a区 | 亚洲视频免费在线播放 | 欧美精品一区二区三区四区 在线 | 国产福利在线视频 | 日韩影院在线 | www国产亚洲精品久久网站 | 欧美a∨ | 秋霞国产 | 国产一区二区在线免费观看 | 色综合视频 | 男人天堂999 | h视频网站在线观看 | 亚洲91视频 | 精品国产久 | 国产精品a一区二区三区网址 | 国产精品一区二区在线观看 | 99久久国产综合精品麻豆 | 久久久久久久av | 一区二区三区在线 | 欧 | 91视频在线| 午夜精品一区二区三区在线观看 | 久久久精品一区二区三区 | 在线免费国产视频 | 岛国精品| 亚洲v日韩v综合v精品v | 中文字幕在线免费视频 | 亚洲一区二区综合 | 伊人精品在线视频 | 日本一区二区三区四区 |