一、知識點
屬性編輯器是JavaBeans API的一項功能,用於屬性值與文本值相互轉換。每個屬性編輯器僅用於某一類屬性。
Spring IoC容器支援使用屬性編輯器來簡化Bean配置。例如,使用java.net.URL類型的屬性編輯器,可以指定用於URL類型屬性的URL字串。Spring會自動地將這個URL字串轉換成一個URL對象,注入到你的屬性中。Spring內建多種用於轉換常見類型Bean屬性的屬性編輯器。
需要在Spring IoC容器中註冊屬性編輯器,然後才能使用。CustomEditorConfigurer是作為Bean工廠後處理器來實現的,用於在任何Bean執行個體化之前註冊你的自訂屬性編輯器。
二、程式碼範例
[java]
package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import java.util.Date;
/**
* 產品銷售排名
* @author jackie
*
*/
public class ProductRanking {
private Product bestSeller;
private Date fromDate;
private Date toDate;
/**
* @return the bestSeller
*/
public Product getBestSeller() {
return bestSeller;
}
/**
* @param bestSeller
* the bestSeller to set
*/
public void setBestSeller(Product bestSeller) {
this.bestSeller = bestSeller;
}
/**
* @return the fromDate
*/
public Date getFromDate() {
return fromDate;
}
/**
* @param fromDate
* the fromDate to set
*/
public void setFromDate(Date fromDate) {
this.fromDate = fromDate;
}
/**
* @return the toDate
*/
public Date getToDate() {
return toDate;
}
/**
* @param toDate
* the toDate to set
*/
public void setToDate(Date toDate) {
this.toDate = toDate;
}
}
package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import java.util.Date;
/**
* 產品銷售排名
* @author jackie
*
*/
public class ProductRanking {
private Product bestSeller;
private Date fromDate;
private Date toDate;
/**
* @return the bestSeller
*/
public Product getBestSeller() {
return bestSeller;
}
/**
* @param bestSeller
* the bestSeller to set
*/
public void setBestSeller(Product bestSeller) {
this.bestSeller = bestSeller;
}
/**
* @return the fromDate
*/
public Date getFromDate() {
return fromDate;
}
/**
* @param fromDate
* the fromDate to set
*/
public void setFromDate(Date fromDate) {
this.fromDate = fromDate;
}
/**
* @return the toDate
*/
public Date getToDate() {
return toDate;
}
/**
* @param toDate
* the toDate to set
*/
public void setToDate(Date toDate) {
this.toDate = toDate;
}
} 以下是Java程式轉換特定模式的日期文字。
[java]
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
productRanking.setFromDate(dateFormat.parse("2007-09-01"));
productRanking.setToDate(dateFormat.parse("2007-09-30"));
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
productRanking.setFromDate(dateFormat.parse("2007-09-01"));
productRanking.setToDate(dateFormat.parse("2007-09-30")); Spring中等價Bean配置如下: [java] view plaincopyprint?<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.2.xsd">
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
<bean id="productRanking" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.ProductRanking">
<property name="bestSeller">
<bean class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc">
<property name="name" value="CD-RW" />
<property name="price" value="1.5" />
</bean>
</property>
<property name="fromDate">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2007-09-01"></constructor-arg>
</bean>
</property>
<property name="toDate">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2007-09-30"></constructor-arg>
</bean>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.2.xsd">
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
<bean id="productRanking" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.ProductRanking">
<property name="bestSeller">
<bean class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc">
<property name="name" value="CD-RW" />
<property name="price" value="1.5" />
</bean>
</property>
<property name="fromDate">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2007-09-01"></constructor-arg>
</bean>
</property>
<property name="toDate">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2007-09-30"></constructor-arg>
</bean>
</property>
</bean>
</beans> 前述配置對於設定日期屬性太複雜了。Spring IoC容器能夠使用屬性編輯器為你的屬性轉換文本值。Spring內建的CustomDateEditor類用於將日期文字轉換為java.util.Date屬性。首先在Bean設定檔中聲明執行個體。
[java]
<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>
<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> 然後在CustomEditorConfigurer執行個體中註冊這個屬性編輯器,這樣Spring可以轉換類型為java.util.Date屬性。
[html] view plaincopyprint?<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>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<ref local="dateEditor"/>
</entry>
</map>
</property>
</bean>
<bean id="productRanking" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.ProductRanking">
<property name="bestSeller">
<bean class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc">
<property name="name" value="CD-RW" />
<property name="price" value="1.5" />
</bean>
</property>
<property name="fromDate" value="2007-09-01" />
<property name="toDate" value="2007-09-30" />
</bean>
<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>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<ref local="dateEditor"/>
</entry>
</map>
</property>
</bean>
<bean id="productRanking" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.ProductRanking">
<property name="bestSeller">
<bean class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc">
<property name="name" value="CD-RW" />
<property name="price" value="1.5" />
</bean>
</property>
<property name="fromDate" value="2007-09-01" />
<property name="toDate" value="2007-09-30" />
</bean> 測試類別PropertyEditorTest
[java] view plaincopyprint?package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author jackie
*
*/
public class PropertyEditorTest {
@Test
public void testPropertyEditor(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ProductRanking productRanking = (ProductRanking) context.getBean("productRanking");
System.out.println("Product ranking from " + productRanking.getFromDate() + " to " + productRanking.getToDate());
}
}
package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author jackie
*
*/
public class PropertyEditorTest {
@Test
public void testPropertyEditor(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ProductRanking productRanking = (ProductRanking) context.getBean("productRanking");
System.out.println("Product ranking from " + productRanking.getFromDate() + " to " + productRanking.getToDate());
}
} 除了CustomDateEditor之外, Spring內建多個轉換常見資料類型的屬性編輯器,例如CustomNumberEditor, ClassEditor, FileEditor, LocaleEditor,StringArrayPropertyEditor以及URLEditor. 其中, ClassEditor, FileEditor, LocaleEditor以及URLEditor由Spring預先註冊,不需要再次註冊。關於這些編輯器使用的更多資訊,可以參考org.springframework.beans.propertyeditors包中這些類的Javadoc。