偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

詳解Spring自定義屬性編輯器

開發(fā) 后端
Spring 自定義屬性編輯器,Spring DI注入的時候可以把普通屬性注入進來,但是像Date類型的就無法被識別。這時可以通過Spring的屬性編輯器把配置文件中的字符串轉(zhuǎn)化成相應(yīng)的對象進行注入。

Spring 自定義屬性編輯器

Spring DI注入的時候可以把普通屬性注入進來,但是像Date類型的就無法被識別。這時可以通過Spring的屬性編輯器把配置文件中的字符串轉(zhuǎn)化成相應(yīng)的對象進行注入。

Spring有自帶的屬性編輯器,我們也可以寫自定義的屬性編輯器

自定義屬性編輯器:

繼承java.beans.PropertyEditorSupport類,重寫其中的setAsText(String text)方法。

再把自定義的屬性編輯器注入到Spring中。

例子:

JavaBean類

Java代碼

 

 

  1. package com.cos.entity;     
  2.     
  3. import java.util.Date;     
  4. import java.util.List;     
  5. import java.util.Map;     
  6. import java.util.Set;     
  7.     
  8. public class UserBean {     
  9.     
  10.     private Date birthday;     
  11.     
  12.     public Date getBirthday() {     
  13.         return birthday;     
  14.     }     
  15.     
  16.     public void setBirthday(Date birthday) {     
  17.         this.birthday = birthday;     
  18.     }     
  19. }    

 

自定義屬性編輯器

Java代碼

 

 

  1. package com.cos.entity;     
  2.     
  3. import java.beans.PropertyEditorSupport;     
  4. import java.text.ParseException;     
  5. import java.text.SimpleDateFormat;     
  6.     
  7. //自己寫一個自定義屬性編輯器來繼承屬性編輯器PropertyEditorSupport     
  8. public class DatePropertyEditor extends PropertyEditorSupport {     
  9.     
  10.     //時間的格式     
  11.     String format;     
  12.     
  13.     public String getFormat() {     
  14.         return format;     
  15.     }     
  16.     
  17.     public void setFormat(String format) {     
  18.         this.format = format;     
  19.     }     
  20.     
  21.     //需要重寫屬性編輯器的setAsText()方法     
  22.     @Override    
  23.     public void setAsText(String text) {     
  24.         try {     
  25.             SimpleDateFormat f = new SimpleDateFormat(format);     
  26.             //把轉(zhuǎn)換后的值傳進去     
  27.             this.setValue(f.parse(text));     
  28.         } catch (ParseException ex) {     
  29.             ex.printStackTrace();     
  30.         }     
  31.     }     
  32. }    

 

spring配置文件 applicationContext.xml :

Xml代碼

 

  1. <beans xmlns="http://www.springframework.org/schema/beans"    
  2.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd     
  5.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">    
  6.     
  7.     <!-- 普通屬性注入 -->    
  8.     <bean id="userBean" class="com.cos.entity.UserBean">    
  9.         <!-- 時間屬性,需要屬性編輯器 -->    
  10.         <property name="birthday" value="2011-03-16"/>    
  11.     </bean>    
  12.     
  13.     <!-- 特殊屬性的注入.把特殊屬性注入到CustomEditorConfigurer Bean 里 -->    
  14.     <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">    
  15.         <property name="customEditors">    
  16.             <map>    
  17.                 <entry key="java.util.Date">    
  18.                     <bean class="com.cos.entity.DatePropertyEditor">    
  19.                         <property name="format" value="yyyy-MM-dd"/>    
  20.                     </bean>    
  21.                 </entry>    
  22.             </map>    
  23.         </property>    
  24.     </bean>    
  25. </beans>    

 

 

 

 

 

 

 

 

 

org.springframework.beans.factory.config.CustomEditorConfigurer類可以讀取PropertyEditorSupport類及子類,將字符串轉(zhuǎn)化為指定的類型。

PropertyEditorSupport類把要轉(zhuǎn)化的Date類型注入到customEditors Map中。

測試類:

Java代碼

 

 

  1. package com.cos.entity;     
  2.     
  3. import org.springframework.beans.factory.BeanFactory;     
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;     
  5.     
  6. public class Main {     
  7.     
  8.     public static void main(String[] args) {     
  9.         //通過spring配置文件返回Bean的工廠對象     
  10.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");     
  11.         //Bean工廠通過Bean的id得到JavaBean     
  12.         UserBean ub = (UserBean) factory.getBean("userBean");     
  13.         System.out.println(""+ub.getBirthday());     
  14.     }     
  15. }    

 

責(zé)任編輯:金賀 來源: JavaEye博客
相關(guān)推薦

2024-01-08 08:30:05

光標(biāo)圖形編輯器開發(fā)游標(biāo)

2010-11-16 13:21:08

Oracle命令行

2024-10-14 17:18:27

2023-10-20 08:02:25

圖形編輯器前端

2011-08-09 17:16:56

CoreAnimati動畫

2009-08-04 13:35:16

ASP.NET自定義樣

2025-02-05 12:01:35

屬性編輯器Web

2013-04-01 14:35:10

Android開發(fā)Android自定義x

2011-09-07 09:30:46

Lua編輯器Scite

2013-09-10 16:02:59

Elipse編輯器

2009-02-26 08:54:50

FCKeditorHTML編輯器

2009-11-26 17:37:37

Linux編輯器

2022-06-20 08:26:39

Spring容器類型轉(zhuǎn)換

2023-08-26 19:04:40

配置write轉(zhuǎn)換器

2022-09-21 14:42:03

JSProps屬性

2009-12-23 10:27:22

vi編輯器

2011-08-02 11:17:13

iOS開發(fā) View

2023-10-10 16:04:30

圖形編輯器格式轉(zhuǎn)換

2010-03-24 09:20:07

CentOS vi編輯

2009-08-06 17:13:56

ASP.NET自定義控
點贊
收藏

51CTO技術(shù)棧公眾號