spring中自定義屬性編輯器CustomEditorConfigurer


什么是屬性編輯器,作用? 
* 自定義屬性編輯器,spring配置文件中的字符串轉換成相應的對象進行注入 
spring已經有內置的屬性編輯器,我們可以根據需求自己定義屬性編輯器 

* 如何定義屬性編輯器? 
* 繼承PropertyEditorSupport類,覆寫setAsText()方法,參見:UtilDatePropertyEditor.java 
* 將屬性編輯器注冊到spring中,參見:applicationContext.xml 

比如: 
有一個類里面有一個Date屬性 

Java代碼    收藏代碼
  1. public class Bean1 {  
  2.      private Date dateValue;  
  3.      public void setDateValue(Date dateValue) {  
  4.         this.dateValue = dateValue;  
  5.     }  
  6. }  


applicationContext.xml配置文件如下: 

Java代碼    收藏代碼
  1. <!--將bean1中的Date賦值2008-08-15,spring會認為2008-08-15是String,無法轉換成Date,會報錯!-->  
  2. <bean id="bean1" class="com.bjsxt.spring.Bean1">  
  3.       <property name="dateValue">  
  4.          <value>2008-08-15</value>  
  5.     </property>  
  6. </bean>  
  7. <!-- 於是定義屬性編輯器 -->        
  8. <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  9.     <property name="customEditors">  
  10.         <map>  
  11.             <entry key="java.util.Date">  
  12.                 <bean class="com.bjsxt.spring.UtilDatePropertyEditor">  
  13.                                         <!--干脆把format也注入,靈活處理格式-->  
  14.                     <property name="format" value="yyyy-MM-dd"/>  
  15.                 </bean>  
  16.             </entry>  
  17.         </map>  
  18.     </property>  
  19. </bean>     


UtilDatePropertyEditor.java 如下,必須繼承java.beans.PropertyEditorSupport類,覆寫setAsText()方法 

Java代碼    收藏代碼
  1. import java.beans.PropertyEditorSupport;  
  2. import java.text.ParseException;  
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. /** 
  7.  * java.util.Date屬性編輯器  
  8.  * @author Administrator 
  9.  * 
  10.  */  
  11. public class UtilDatePropertyEditor extends PropertyEditorSupport {  
  12.   
  13.     private String format="yyyy-MM-dd";  
  14.       
  15.     @Override  
  16.     public void setAsText(String text) throws IllegalArgumentException {  
  17.         System.out.println("UtilDatePropertyEditor.saveAsText() -- text=" + text);  
  18.           
  19.         SimpleDateFormat sdf = new SimpleDateFormat(format);  
  20.         try {  
  21.             Date d = sdf.parse(text);  
  22.             this.setValue(d);  
  23.         } catch (ParseException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.     }  
  27.   
  28.     public void setFormat(String format) {  
  29.         this.format = format;  
  30.     }  
  31.   
  32. }  


這樣就可以完成正確解析了,注意customEditors是Spring的類CustomEditorConfigurer提供的屬性,是一個Map,里面存放的都是自定義的編輯器(customEditors),比如這里存放的是UtilDatePropertyEditor日期編輯器,看CustomEditorConfigurer源碼就知道了。 

測試一下: 

Java代碼    收藏代碼
  1. import org.springframework.beans.factory.BeanFactory;  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3.   
  4. import junit.framework.TestCase;  
  5.   
  6. public class InjectionTest extends TestCase {  
  7.       
  8.     private BeanFactory factory;  
  9.       
  10.     @Override  
  11.     protected void setUp() throws Exception {  
  12.         factory = new ClassPathXmlApplicationContext("applicationContext.xml");   
  13.     }  
  14.   
  15.     public void testInjection1() {  
  16.         Bean1 bean1 = (Bean1)factory.getBean("bean1");  
  17.         System.out.println("bean1.dateValue=" + bean1.getDateValue());  
  18.     }  
  19.       
  20. }  


能打印出日期就OK了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM