生活随笔
收集整理的這篇文章主要介紹了
spring中自定义属性编辑器CustomEditorConfigurer
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://gundumw100.iteye.com/blog/574440
什么是屬性編輯器,作用?
* 自定義屬性編輯器,spring配置文件中的字符串轉換成相應的對象進行注入
spring已經有內置的屬性編輯器,我們可以根據需求自己定義屬性編輯器
* 如何定義屬性編輯器?
* 繼承PropertyEditorSupport類,覆寫setAsText()方法,參見:UtilDatePropertyEditor.java
* 將屬性編輯器注冊到spring中,參見:applicationContext.xml
比如:
有一個類里面有一個Date屬性
Java代碼
public?class?Bean1?{ ???????private?Date?dateValue; ???????public?void?setDateValue(Date?dateValue)?{ ??????????this.dateValue?=?dateValue; ??????} ??}??
public class Bean1 {private Date dateValue;public void setDateValue(Date dateValue) {this.dateValue = dateValue;}
}
applicationContext.xml配置文件如下:
Java代碼 ?
<!--將bean1中的Date賦值2008-08-15,spring會認為2008-08-15是String,無法轉換成Date,會報錯!--> ??<bean?id="bean1"?class="com.bjsxt.spring.Bean1"> ????????<property?name="dateValue"> ???????????<value>2008-08-15</value> ??????</property> ??</bean> ??<!--?于是定義屬性編輯器?-->?????? ??<bean?id="customEditorConfigurer"?class="org.springframework.beans.factory.config.CustomEditorConfigurer"> ??????<property?name="customEditors"> ??????????<map> ??????????????<entry?key="java.util.Date"> ??????????????????<bean?class="com.bjsxt.spring.UtilDatePropertyEditor"> ??????????????????????????????????????????<!--干脆把format也注入,靈活處理格式--> ??????????????????????<property?name="format"?value="yyyy-MM-dd"/> ??????????????????</bean> ??????????????</entry> ??????????</map> ??????</property> ??</bean>?????
<!--將bean1中的Date賦值2008-08-15,spring會認為2008-08-15是String,無法轉換成Date,會報錯!-->
<bean id="bean1" class="com.bjsxt.spring.Bean1"><property name="dateValue"><value>2008-08-15</value></property>
</bean>
<!-- 于是定義屬性編輯器 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="customEditors"><map><entry key="java.util.Date"><bean class="com.bjsxt.spring.UtilDatePropertyEditor"><!--干脆把format也注入,靈活處理格式--><property name="format" value="yyyy-MM-dd"/></bean></entry></map></property>
</bean>
UtilDatePropertyEditor.java 如下,必須繼承java.beans.PropertyEditorSupport類,覆寫setAsText()方法
Java代碼 ?
import?java.beans.PropertyEditorSupport; ??import?java.text.ParseException; ??import?java.text.SimpleDateFormat; ??import?java.util.Date; ??????????public?class?UtilDatePropertyEditor?extends?PropertyEditorSupport?{ ????????private?String?format="yyyy-MM-dd"; ?????? ??????@Override??????public?void?setAsText(String?text)?throws?IllegalArgumentException?{ ??????????System.out.println("UtilDatePropertyEditor.saveAsText()?--?text="?+?text); ?????????? ??????????SimpleDateFormat?sdf?=?new?SimpleDateFormat(format); ??????????try?{ ??????????????Date?d?=?sdf.parse(text); ??????????????this.setValue(d); ??????????}?catch?(ParseException?e)?{ ??????????????e.printStackTrace(); ??????????} ??????} ????????public?void?setFormat(String?format)?{ ??????????this.format?=?format; ??????} ????}??
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/*** java.util.Date屬性編輯器 * @author Administrator**/
public class UtilDatePropertyEditor extends PropertyEditorSupport {private String format="yyyy-MM-dd";@Overridepublic void setAsText(String text) throws IllegalArgumentException {System.out.println("UtilDatePropertyEditor.saveAsText() -- text=" + text);SimpleDateFormat sdf = new SimpleDateFormat(format);try {Date d = sdf.parse(text);this.setValue(d);} catch (ParseException e) {e.printStackTrace();}}public void setFormat(String format) {this.format = format;}}
這樣就可以完成正確解析了,注意customEditors是Spring的類CustomEditorConfigurer提供的屬性,是一個Map,里面存放的都是自定義的編輯器(customEditors),比如這里存放的是UtilDatePropertyEditor日期編輯器,看CustomEditorConfigurer源碼就知道了。
測試一下:
Java代碼 ?
import?org.springframework.beans.factory.BeanFactory; ??import?org.springframework.context.support.ClassPathXmlApplicationContext; ????import?junit.framework.TestCase; ????public?class?InjectionTest?extends?TestCase?{ ?????? ??????private?BeanFactory?factory; ?????? ??????@Override??????protected?void?setUp()?throws?Exception?{ ??????????factory?=?new?ClassPathXmlApplicationContext("applicationContext.xml");? ??????} ????????public?void?testInjection1()?{ ??????????Bean1?bean1?=?(Bean1)factory.getBean("bean1"); ??????????System.out.println("bean1.dateValue="?+?bean1.getDateValue()); ??????} ?????? ??}??
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;import junit.framework.TestCase;public class InjectionTest extends TestCase {private BeanFactory factory;@Overrideprotected void setUp() throws Exception {factory = new ClassPathXmlApplicationContext("applicationContext.xml"); }public void testInjection1() {Bean1 bean1 = (Bean1)factory.getBean("bean1");System.out.println("bean1.dateValue=" + bean1.getDateValue());}}
能打印出日期就OK了。
總結
以上是生活随笔為你收集整理的spring中自定义属性编辑器CustomEditorConfigurer的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。