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

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

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

瀏覽:167日期: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
主站蜘蛛池模板: 亚洲日本在线观看 | 国产高清一区二区三区 | 欧美一级特黄特黄毛片 | 亚洲第一区视频在线观看 | 日日摸日日碰夜夜爽久久 | 国产成人一级片 | 久久九九热视频 | 免费高清国产 | 韩国精品一区二区三区在线观看 | 米奇精品一区二区三区在线观看 | a级精品九九九大片免费看 a级毛片免费观看网站 | 男女午夜性爽快免费视频不卡 | 午夜两性试爱视频免费 | 伊人色综合久久成人 | 国产激情久久久久久影院 | 欧美国产一区二区三区 | 成年女人毛片免费观看中文w | 欧美一级看片免费观看视频在线 | 99re热这里只有精品视频 | 色咪味成人网 | 美国一级毛片片aa成人 | 午夜欧美成人香蕉剧场 | 欧美人牲囗毛片 | 成人国产精品免费网站 | 欧美精品亚洲精品日韩专区 | 国产成人精品亚洲日本在线观看 | 欧美a级完整在线观看 | 亚洲欧美久久精品一区 | 欧美片网站免费 | 欧美成人午夜不卡在线视频 | 大视频在线爱爱爱爱 | 久久视频免费 | 久久精品综合国产二区 | 国产精品露脸脏话对白 | 韩国视频一区 | 国产91久久精品一区二区 | 欧美在线高清视频播放免费 | 午夜欧美精品久久久久久久久 | 精品日韩一区二区三区 | 美女毛片大全 | 成人亚洲欧美综合 |