Wednesday, 29 June 2011

Using the Spring 3 ConverterFactory Interface

Yesterday, I mentioned that the Guys at Spring have come up with a whole new way of doing type conversion that supersedes the property editor support mechanism. This is based on the org.springframework.core.convert package and also I demonstrated how to implement the Converter interface and wire it in with your Spring beans. I also said that it offers additional functionality to the property editor support method and this comes in the form of the ConverterFactory, GenericConverter, and ConditionalGenericConverter interfaces. The Spring documentation briefly details these extra interfaces and how to implement them.

This blog takes the idea of the ConverterFactory together with a contrived wine service scenario and adds to Spring's documentation to demonstrate how it can be used. In this contrived scenario, I have a WineService that has two setters, each requiring an Enum argument:

public class WineService {

private WineCountry wineCountry;

private WineColour wineColour;

public final void setWineCountry(WineCountry wineCountry) {

this.wineCountry = wineCountry;

public final void setWineColour(WineColour wineColour) {

this.wineColour = wineColour;

/** Some arbitrary business method that checks wine availability */
public boolean isWineAvailable(WineCountry wineCountry, WineColour wineColour) {

if (this.wineCountry == wineCountry && this.wineColour == wineColour) {
return true;

return false;

In order to convert these Enum arguments from strings specified in the XML config, we’ll need a couple of Converters. In this case, we can centralise the conversion logic from String to Enum as the conversion requirements for each Enum are similar and for this we can use a ConverterFactory.

final class StringToEnumConverterFactory implements ConverterFactory<String, Enum<?>> {

public <T extends Enum<?>> Converter<String, T> getConverter(Class<T> targetType) {

return new StringToEnumConverter<T>(targetType);

private final class StringToEnumConverter<T extends Enum> implements Converter<String, T> {

private final Class<T> enumType;

public StringToEnumConverter(Class<T> enumType) {

this.enumType = enumType;

public T convert(String source) {

return (T) Enum.valueOf(enumType, source.trim());

private void checkArg(String source) {

// In the spec, null input is not allowed
if (source == null) {
throw new IllegalArgumentException("null source is in allowed");

This code has been shamelessly plagiarised from the Spring documentation although I have added a couple of changes to get rid of the warnings in Eclipse together with a quick null check.

The XML config is also very similar to yesterday’s in that we’re specifying a business object and a ConversionServiceFactoryBean as a conversion service. It only differs in that the converters property of the ConversionServiceFactoryBean takes our ConverterFactory implementation: StringToEnumConverterFactory. That’s because the converters property can be any implementations of the Converter, ConverterFactory, GenericConverter, and ConditionalGenericConverter interfaces.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

 <bean id="wineService" class="miscillaneous.conversionservice.converterfactory.WineService">
  <property name="wineColour">
  <property name="wineCountry">

 <bean id="conversionService"
     <property name="converters">
          <!-- Converters can be a list of converter or a ConverterFactory -->
        <bean class="miscillaneous.conversionservice.converterfactory.StringToEnumConverterFactory"/>

The code to test this is:

public class ConverterFactoryMain {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext(
    WineService wineService = ctx.getBean

(wineService, WineCountry.FRANCE, WineColour.RED);
(wineService, WineCountry.GERMANY, WineColour.WHITE);

private static void checkWine(WineService wineService, WineCountry wineCountry,
      WineColour wineColour
) {

+ " "
+ wineColour.toString()
+ " is "
+ (wineService.isWineAvailable(wineCountry, wineColour) == true ? "available"
: "unavailable"));

No comments: