</property>
</bean>
The order of the beans within the converters property is not important.
To test the conversion, we use the same testing program as the previous sample, which is the
ConvServExample class. Listing 14-12 shows the revised main() method.
Listing 14-12. Testing Conversion Service
package com.apress.prospring3.ch14.convserv;
// Import statements omitted
public class ConvServExample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:conv-service-app-context.xml");
ctx.refresh();
Contact clarence = ctx.getBean("clarence", Contact.class);
System.out.println("Contact info: " + clarence);
ConversionService conversionService = ctx.getBean(ConversionService.class);
// Convert from Contact to AnotherContact
AnotherContact anotherContact =
conversionService.convert(clarence, AnotherContact.class);
System.out.println("Another contact info: " + anotherContact);
// Conversion from String to Array
String[] stringArray = conversionService.convert(
"a,b,c", String[].class);
System.out.println("String array: " + stringArray[0] +
stringArray[1] + stringArray[2]);
// Conversion from List to Set
List<String> listString = new ArrayList<String>();
listString.add("a");
listString.add("b");
listString.add("c");
Set<String> setString = conversionService.convert(
listString, HashSet.class);
for (String string: setString)
System.out.println("Set: " + string);
}
}
In Listing 14-12, look at the bold line, in which a handle to the ConversionService interface is
obtained from the ApplicationContext. Because we already registered the ConversionService in
ApplicationContext with our custom converters, we can use it to convert the Contact object, as well as
convert between other types that the conversion service already supports. As shown in the listing,
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home