logger.info("Parsing date string: " + dateTimeString);
return dateFormat.parseDateTime(dateTimeString);
}
public String print(DateTime dateTime, Locale locale) {
logger.info("Formatting datetime: " + dateTime);
return dateFormat.print(dateTime);
}
};
}
}
In Listing 14-13, the custom formatter is in bold. It implements the Formatter<DateTime> interface
and implements two methods defined by the interface. The parse() method parses the String format
into the DateTime type (the locale was also passed for localization support), while the print() method is
to format a DateTime instance into a String. The date pattern can be injected into the bean (or the
default will be yyyy-MM-dd). Also, in the init() method, the custom formatter is registered by calling the
setFormatters() method. You can add as many formatters as required.
Configuring ConversionServiceFactoryBean
To configure the ApplicationConversionServiceFactoryBean in Spring's ApplicationContext, we just
need to declare a bean with that class as the provider. Listing 14-14 shows the configuration (conv-
format-service-app-context.xml).
Listing 14-14. The conv-format-service-app-context.xml File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config/>
<bean id="conversionService"
class="com.apress.prospring3.ch14.convserv.factory.ApplicationConversionServiceFactoryBean"/>
<bean id="clarence" class="com.apress.prospring3.ch14.domain.Contact"
p:firstName="Clarence"
p:lastName="Ho"
p:birthDate="1978-08-09"
p:personalSite="http://www.clarence.com"
/>
</beans>
Listing 14-15 shows the testing program.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home