、知識點
通過實現java.beans.PropertyEditor接口或者繼承便利的支持類 java.beans.PropertyEditorSupport,可以編寫自定義的屬性編輯器。
二、代碼示例
Product類的屬性編輯器
[java] package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import java.beans.PropertyEditorSupport;
/**
* Product類的屬性編輯器
* @author jackie
*
*/
public class ProductEditor extends PropertyEditorSupport {
/**
* 將屬性轉換成字符串值
*/
public String getAsText() {
Product product = (Product) getValue();
return product.getClass().getName() + "," + product.getName() + "," + product.getPrice();
}
/**
* 將字符串轉換成屬性
*/
public void setAsText(String text) throws IllegalArgumentException {
String[] parts = text.split(",");
try {
Product product = (Product) Class.forName(parts[0]).newInstance();
product.setName(parts[1]);
product.setPrice(Double.parseDouble(parts[2]));
setValue(product);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
}
package com.codeproject.jackie.springrecipesnote.springadvancedioc;
import java.beans.PropertyEditorSupport;
/**
* Product類的屬性編輯器
* @author jackie
*
*/
public class ProductEditor extends PropertyEditorSupport {
/**
* 將屬性轉換成字符串值
*/
public String getAsText() {
Product product = (Product) getValue();
return product.getClass().getName() + "," + product.getName() + "," + product.getPrice();
}
/**
* 將字符串轉換成屬性
*/
public void setAsText(String text) throws IllegalArgumentException {
String[] parts = text.split(",");
try {
Product product = (Product) Class.forName(parts[0]).newInstance();
product.setName(parts[1]);
product.setPrice(Double.parseDouble(parts[2]));
setValue(product);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
}
實際上,JavaBeans API將自動搜索類的屬性編輯器。為了能搜索到正確的屬性編輯器,它必須位於目標類的相同包裡,名稱必須是目標類名加上Editor後綴。如果你自定義的屬性編輯器遵照這種約定,就像ProductEditor,那麼就沒有必要再Spring IoC容器中再次注冊。否則的話,必須要在CustomEditorConfigurer實例中注冊自定義編輯器,才能使用它。注冊方法與內置編輯器相同。
[java] <?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="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">
<value>com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc,CD-RW,1.5</value>
</property>
<property name="fromDate" value="2007-09-01" />
<property name="toDate" value="2007-09-30" />
</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="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">
<value>com.codeproject.jackie.springrecipesnote.springadvancedioc.Disc,CD-RW,1.5</value>
</property>
<property name="fromDate" value="2007-09-01" />
<property name="toDate" value="2007-09-30" />
</bean>
</beans>