Spring 3 Type Conversion - Java-Springs

Spring 3 introduces a core.convert package that provides a general type conversion system.The system defines an SPI to implement type conversion logic, as well as an API to execute type conversions at runtime.Within a Spring container, this system can be used as an alternative to Property Editors to convert externalized bean property value strings to required property types.The public API may also be used anywhere in your application where type conversion is needed.

Converter SPI

The SPI to implement type conversion logic is simple and strongly typed:

package org.springframework.core.convert.converter;
public interface Converter<S, T> {
T convert(S source);
}

To create your own Converter, simply implement the interface above.Parameterize S as the type you are converting from, and T as the type you are converting to. For each call to convert(S), the source argument is guaranteed to be NOT null.Your Converter may throw any Exception if conversion fails. An IllegalArgumentException should be thrown to report an invalid source value. Take care to ensure your Converter implementation is thread-safe.

Several converter implementations are provided in the core.convert.support package as a convenience. These include converters from Strings to Numbers and other common types.Consider StringToInteger as an example Converter implementation:

package org.springframework.core.convert.support;
final class StringToInteger implements Converter<String, Integer> {
public Integer convert(String source) {
return Integer.valueOf(source);
}
}

ConverterFactory

When you need to centralize the conversion logic for an entire class hierarchy, for example, when converting from String to java.lang.Enum objects, implement ConverterFactory:

package org.springframework.core.convert.converter;public interface ConverterFactory<S, R> <T extends R> Converter<S, T> getConverter(Class<T> targetType); }

Parameterize S to be the type you are converting from and R to be the base type defining the range of classes you can convert to.Then implement get Converter(Class<T>), where T is a subclass of R.Consider the StringToEnum Converter Factory as an example:

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

When you require a sophisticated Converter implementation, consider the Generic Converter interface.With a more flexible but less strongly typed signature, a Generic Converter supports converting between multiple source and target types.In addition, a GenericConverter makes available source and target field context you can use when implementing your conversion logic. Such context allows a type conversion to be driven by a field annotation, or generic information declared on a field signature.

package org.springframework.core.convert.converter;
public interface GenericConverter {
public Set<ConvertiblePair> getConvertibleTypes();
Object convert(Object source,TypeDescriptor sourceType,
TypeDescriptor targetType);
}

To implement a Generic Converter, have get Convertible Types() return the supported source->target type pairs. Then implement convert(Object, Type Descriptor, Type Descriptor) to implement your conversion logic.The source Type Descriptor provides access to the source field holding the value being converted.

The target Type Descriptor provides access to the target field where the converted value will be set.A good example of a GenericConverter is a converter that converts between a Java Array and a Collection.Such an ArrayToCollectionConverter introspects the field that declares the target Collection type to resolve the Collection's element type. This allows each element in the source array to be converted to the Collection element type before the Collection is set on the target field.

ConditionalGenericConverter

Sometimes you only want a Converter to execute if a specific condition holds true. For example, you might only want to execute a Converter if a specific annotation is present on the target field. Or you might only want to execute a Converter if a specific method, such as static valueOf method, is defined on the target class. Conditional Generic Converter is an sub interface of Generic Converter that allows you to define such custom matching criteria:

public interface ConditionalGenericConverter extends GenericConverter {
boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);
}

A good example of a Conditional Generic Converter is an Entity Converter that converts between an persistent entity identifier and an entity reference. Such a Entity Converter might only match if the target entity type declares a static finder method e.g. find Account (Long). You would perform such a finder method check in the implementation of matches(TypeDescriptor, TypeDescriptor).

ConversionService API

The Conversion Service defines a unified API for executing type conversion logic at runtime. Converters are often executed behind this facade interface:

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);
}

Most Conversion Service implementations also implement Converter Registry, which provides an SPI for registering converters. Internally, a Conversion Service implementation delegates to its registered converters to carry out type conversion logic.

A robust Conversion Service implementation is provided in the core.convert.support package.Generic Conversion Service is the general-purpose implementation suitable for use in most environments. Conversion Service Factory provides a convenient factory for creating common Conversion Service configurations.

Configuring a ConversionService

A Conversion Service is a stateless object designed to be instantiated at application startup, then shared between multiple threads. In a Spring application, you typically configure a Conversion Service instance per Spring container (or Application Context).That Conversion Service will be picked up by Spring and then used whenever a type conversion needs to be performed by the framework.You may also inject this ConversionService into any of your beans and invoke it directly.

To register a default Conversion Service with Spring, add the following bean definition with id conversion Service:

<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean"/>

A default Conversion Service can convert between strings, numbers, enums, collections, maps, and other common types. To suppliment or override the default converters with your own custom converter(s), set the converters property.Property values may implement either of the Converter, Converter Factory, or Generic Converter interfaces.

<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="example.MyCustomConverter"/>
</list>
</property>
</bean>

It is also common to use a ConversionService within a Spring MVC application.

Using a Conversion Service programmatically

To work with a Conversion Service instance programatically, simply inject a reference to it like you would for any other bean:

@Service
public class MyService {
@Autowired
public MyService(ConversionService conversionService) {
this.conversionService = conversionService;
}
public void doIt() {
this.conversionService.convert(...)
}
}

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics