examples of converting from a String (delimited by a comma character) to an Array and from a List to a
Set were also added for demonstration purposes.
Running the program produces the following output:
Contact info: First name: Clarence - Last name: Ho - Birth date: 1978-08-09T00:00:00.000+08:00
­ Personal site: http://www.clarence.com
Another contact info: First name: Ho - Last name: Clarence - Birth date: 1978-08-
09T00:00:00.000+08:00 ­ Personal site: http://www.clarence.com
String array: abc
Set: b
Set: c
Set: a
As shown in the output, you will see that Contact and AnotherContact are converted correctly, as
well as the String to Array and the List to Set. With Spring's type conversion service, you can create
custom converters easily and perform conversion at any layer within your application. One possible use
case is that you have two different systems with the same contact information that you need to update.
However, the database structure is different (for example, the last name in system A means the first
name in system B, and so on). You can use the type conversion system to convert the objects before
persisting to each individual system.
Starting with Spring 3.0, Spring MVC makes heavy use of the conversion service (as well as the
formatter SPI that we will discuss in the next section). In the web application context configuration, the
declaration of the tag <mvc:annotation-driven/> will automatically register all default converters (for
example, StringToArrayConverter, StringToBooleanConverter, and StringToLocaleConverter, all
residing under the org.springframework.core.convert.support package) and formatters (for example,
CurrencyFormatter, DateFormatter, and NumberFormatter, all residing under various subpackages within
the org.springframework.format package). More will be covered in Chapters 17 and 18, when we discuss
web application development in Spring.
Field Formatting in Spring 3
Besides the type conversion system, another great feature that Spring brings to developers is the
Formatter SPI. As you might expect, this SPI can help configure the field-formatting aspects.
In the Formatter SPI, the main interface for implementing a formatter is the
org.springframework.format.Formatter<T> interface. Spring provides a few implementations of
commonly used types, including CurrencyFormatter, DateFormatter, NumberFormatter, and
PercentFormatter.
Implementing a Custom Formatter
Implementing a custom formatter is easy too. We will use the same Contact class and implement a
custom formatter for converting the DateTime type of the birthDate attribute to and from a String.
However, this time we will take a different approach; we will extend Spring's
org.springframework.format.support.FormattingConversionServiceFactoryBean class and provide our
custom formatter. The FormattingConversionServiceFactoryBean class is a factory class that provides
convenient access to the underlying FormattingConversionService class, which supports the type
conversion system, as well as field formatting according to the formatting rules defined for each field type.
Listing 14-13 shows a custom class that extends the FormattingConversionServiceFactoryBean class,
with a custom formatter defined for formatting JodaTime's DateTime type.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home