Only register Date converters with global format

Change JodaTimeFormatterRegistrar and DateFormatterRegistrar to only
register converters for the Date and Calendar types when a global format
has been defined. This means that the ObjectToObject converter will
handle String->Date conversion using the deprecated Date(String)
constructor (as was the case with Spring 3.1).

Issue: SPR-10105
This commit is contained in:
Phillip Webb
2013-02-12 11:02:30 -08:00
parent dbe3c234d6
commit 66ae626f91
4 changed files with 97 additions and 14 deletions

View File

@@ -40,20 +40,24 @@ import org.springframework.util.Assert;
public class DateFormatterRegistrar implements FormatterRegistrar {
private DateFormatter dateFormatter = new DateFormatter();
private DateFormatter dateFormatter;
public void registerFormatters(FormatterRegistry registry) {
addDateConverters(registry);
registry.addFormatter(this.dateFormatter);
registry.addFormatterForFieldType(Calendar.class, this.dateFormatter);
registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
// In order to retain back compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if(this.dateFormatter != null) {
registry.addFormatter(this.dateFormatter);
registry.addFormatterForFieldType(Calendar.class, this.dateFormatter);
}
}
/**
* Set the date formatter to register. If not specified the default {@link DateFormatter}
* will be used. This method can be used if additional formatter configuration is
* required.
* Set the date formatter to register. If not specified no formatter is registered.
* This method can be used if global formatter configuration is required.
* @param dateFormatter the date formatter
*/
public void setFormatter(DateFormatter dateFormatter) {

View File

@@ -174,7 +174,16 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
ReadableInstant.class, Date.class, Calendar.class);
ReadableInstant.class);
// In order to retain back compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if(this.formatters.containsKey(Type.DATE_TIME)) {
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
Date.class, Calendar.class);
}
registry.addFormatterForFieldAnnotation(
new JodaDateTimeFormatAnnotationFormatterFactory());