国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

總結(jié)Spring注入Date類型的步驟

2024-09-05 00:18:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Spring可以為屬性或bean注入基本類型的值,當(dāng)要插入非基本類型的值時(shí),使用屬性編輯器即可,今天這篇文章是錯(cuò)新技術(shù)頻道小編總結(jié)Spring注入Date類型的步驟吧!

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

測(cè)試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" class="java.text.SimpleDateFormat">   <constructor-arg value="yyyy-MM-dd" />   </bean>      <bean id="datebean" class="com.springDemo1.Date類型注入.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"     class="org.springframework.beans.propertyeditors.CustomDateEditor">     <constructor-arg>       <bean class="java.text.SimpleDateFormat">         <constructor-arg value="yyyy-MM-dd"></constructor-arg>       </bean>     </constructor-arg>     <constructor-arg value="true" />   </bean>   <!-- 使 Spring轉(zhuǎn)換為java.util.Date -->   <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">     <property name="customEditors">       <map>         <entry key="java.util.Date">           <ref bean="dateEditor" />         </entry>       </map>     </property>   </bean> </beans> 

方式3:先用一個(gè)類重寫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)換的類型匹配時(shí)就會(huì)交給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)建一個(gè)類 重寫PropertyEditorSupport的setAsText方法 -->   <!-- 自定義日期編輯器 -->   <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">     <property name="customEditors"> <!--需要編輯的屬性類型,是一個(gè)map -->       <map>         <entry key="java.util.Date">           <bean class="com.springDemo1.Date類型注入.MyDatePropertyEditor">             <property name="format" value="yyyy-MM-dd" /> <!--注入需要轉(zhuǎn)換的格式 -->           </bean>         </entry>       </map>     </property>   </bean> </beans> 

測(cè)試:

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());   } } 本文是錯(cuò)新技術(shù)頻道為大家總結(jié)Spring注入Date類型的步驟,大家都了解了嗎?如果大家對(duì)本文有任何疑問(wèn),可以隨時(shí)給錯(cuò)新技術(shù)頻道小編留言哦!
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青铜峡市| 阿克苏市| 桑日县| 贵溪市| 隆回县| 高青县| 湖南省| 西吉县| 贺兰县| 江西省| 江油市| 鄂托克前旗| 盘锦市| 双流县| 广元市| 旺苍县| 新蔡县| 茌平县| 临安市| 东辽县| 酒泉市| 长治县| 兴安盟| 金溪县| 宣威市| 陈巴尔虎旗| 视频| 钟祥市| 梨树县| 乌什县| 陵水| 滦南县| 武山县| 囊谦县| 静宁县| 贞丰县| 响水县| 类乌齐县| 昭觉县| 凉城县| 怀远县|