6.5 Spring3 類型轉換,6.5spring3
6.5 Spring3 類型轉換
Spring3引入了core.convert包,提供了一個通用的類型轉換系統。這個系統定義了一個SPI來實作類別型轉換邏輯。SPI就像API一樣,用來在運行時執行類型轉換。在Spring容器中,這個系統可以用來替代PropertyEditors,因為它也可以將bean的string屬性值轉換成所需的屬性類型。在你的應用程式中,任何需要類型轉換的地方都可以使用這些公用API。(下文中的Converter將被翻譯成轉換器)
Converter SPI
實作類別型轉換邏輯的SPI簡單且是強型別的:
package org.springframework.core.convert.converter;public interface Converter<S, T> {T convert(S source);}
想要建立你自己的Converter,只需要實現上面的介面。參數S是轉換前的類型,而T則是轉換後的類型。每次調用convert(S)時,參數(原值)不能為null。你的轉換器可能會在轉換失敗時拋出異常。若是一個無效的原值,那麼應該拋出一個IllegalArgumentException。並且確保你的轉換器實現是安全執行緒的。
為了方便起見,在core.convert.support包中提供了一些converter的實作類別。其中包括了String和Number以及其他常見類型之間的轉換。可以將下面這個Converter的實作類別——StringToInteger當成一個小例子來學習:
package org.springframework.core.convert.support;final class StringToInteger implements Converter<String, Integer> {public Integer convert(String source) {return Integer.valueOf(source);}}
轉換器工廠
當你需要集中一個完整的類結構的轉換邏輯時,比如,從String到java.lang.Enum的轉換,那麼,實現ConverterFactory。
package org.springframework.core.convert.converter;public interface ConverterFactory<S, R> {<T extends R> Converter<S, T> getConverter(Class<T> targetType);}
參數S是轉換前的參數類型,而參數R是你要轉換成的類的基底類型,它定義了你能夠轉換的類的範圍。然後需要實現getConverter(Class<T>),其中T是R的子類。
考慮下面的ConverterFactory ——StringToEnum:
package org.springframework.core.convert.support;final class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {public<T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {return new StringToEnumConverter(targetType);}private final class StringToEnumConverter<T extends Enum> implements Converter<String, T> {private Class<T> enumType;public StringToEnumConverter(Class<T> enumType) {this.enumType = enumType;}public T convert(String source) {return(T) Enum.valueOf(this.enumType, source.trim());}}}
GenericConverter(泛型轉換器)
當你需要一個複雜的轉換器實現時,可以考慮GenericConverter介面。它擁有一個相對弱類型但卻更加靈活的方法頭,GenericConverter支援多個原類型和目標類型的轉換。另外,當你在實現你的轉換邏輯時,GenericConverter還能夠讓你使用原欄位和目標欄位的上下文。這個上下文允許被屬性(field)註解驅動的類型轉換,或者是被泛型資訊定義的類型轉換:
package org.springframework.core.convert.converter;public interface GenericConverter {public Set<ConvertiblePair> getConvertibleTypes();Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);}
實現一個GenericConverter介面時,getConvertibleTypes()需要返回所支援的源類型和目標類型的配對(譯者註:這個方法在ConversionServiceFactory的registerConverters調用,之後會被緩衝),convert(Object,TypeDescriptor, TypeDescriptor)方法用來定義你的轉換邏輯。源TypeDescriptor提供了對源欄位的訪問且包含了需要轉換的原值。而目標TypeDescriptor則提供了對目標欄位的訪問且用於放置轉換後的值。
關於GenericConverter一個比較典型的例子就是Java數群組類型和集合類型之間的轉換。這個ArrayToCollectionConverter通過內省目的地組合類型的欄位來決定集合的元素類型。這允許源數組中的每一個元素在集合被設定到目標欄位之前被轉換成集合元素類型。
注意:由於GenericConverter是一個更為複雜的SPI介面,只有當你需要的時候再去使用它。對於基本的類型轉換,優先使用Converter或者是ConverterFactory。
帶條件的GenericConverter
有時你只想在滿足某個特定條件的時候才執行一個Converter。比如,只有當目標屬性上有一個特定的註解時才去執行一個Converter,或者當目標類中定義了一個特定的方法,比如一個靜態valueOf方法時,才去執行一個Converter。那麼你可以使用ConditionalGenericConverter,它是GenericConverter的子介面,允許你定義剛才提到的那些匹配條件:
public interface ConditionalGenericConverter extends GenericConverter {boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);}
關於ConditionalGenericConverter的一個典型的例子是EntityConverter。它提供了一個持久的實體標識符和一個實體引用之間的轉換。這樣的EntityConverter可能只會匹配那些定義了一個靜態finder方法(比如findAccount(Long))的目標實體。你需要在實現matches(TypeDescriptor,TypeDescriptor)方法時來檢查是否存在這樣的finder方法。
(譯者註:關於matches方法,是在第一次需要類型轉換的時候會被調用,之後就會被緩衝下來)
ConversionService API
ConversionService為運行時執行類型轉換邏輯定義了一個統一的API。Converters通常在這層門面介面之後運行:
package org.springframework.core.convert;public interface ConversionService {boolean canConvert(Class<?> sourceType, Class<?> targetType);<T> T convert(Object source, Class<T> targetType);boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType);Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);}
大多數ConversionService實作類別也實現了ConverterRegistry介面。ConverterRegistry為註冊converters提供了一個SPI。實際上,在ConversionService實現裡會將類型轉換的邏輯委託給註冊了的converters。
core.convert.support包裡提供了一個健壯的ConversionService實現。GenericConversionService作為一個通用的實作類別,可以滿足大多數情況。ConversionServiceFactory提供了一個方便的建立常用的ConversionService配置的工廠。
配置一個ConversionService
ConversionService是一個無狀態的對象,且被設計成在應用啟動時就被執行個體化,並在多個線程間共用。在Spring應用中一個典型的情況,為每個Spring容器(或是ApplicationContext)配置一個ConversionService執行個體。這個ConversionService將會生存在Spring中,並且在需要類型轉換的時候被使用。你也可以將它注入到你的beans中並直接地調用它。
注意:如果Spring中沒有註冊ConversionService,那麼它將會使用原始的基於PropertyEditor的系統。
為了在Spring中註冊一個預設的ConversionService,添加下面的bean定義並且將它的id設定為conversionService:
<bean id="conversionService"class="org.springframework.context.support.ConversionServiceFactoryBean"/>
預設的ConversionServcice能夠轉換strings、number、enums、collections、maps和其他常見類型。通過設定converters屬性,可以用你自訂的converters來補充或覆蓋預設的converters。這個converters屬性的值必須要實現Converter、ConverterFactory或是GenericConverter介面。
<bean id="conversionService"class="org.springframework.context.support.ConversionServiceFactoryBean"><property name="converters"><list><bean class="example.MyCustomConverter" /></list></property></bean>
在Spring MVC應用中使用ConversionService也是很常見的。閱讀“ConfiguringFormatting in Spring MVC”這一節獲得