diff --git a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java index 163b783f661..8c40588422f 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java +++ b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java @@ -24,6 +24,7 @@ import java.util.Set; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.FormatterRegistry; @@ -33,6 +34,7 @@ import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer; import org.springframework.format.number.NumberFormatAnnotationFormatterFactory; import org.springframework.util.ClassUtils; +import org.springframework.util.StringValueResolver; /** * A factory for a {@link FormattingConversionService} that installs default @@ -46,13 +48,15 @@ import org.springframework.util.ClassUtils; * @since 3.0 */ public class FormattingConversionServiceFactoryBean - implements FactoryBean, InitializingBean { + implements FactoryBean, EmbeddedValueResolverAware, InitializingBean { private static final boolean jodaTimePresent = ClassUtils.isPresent( "org.joda.time.LocalDate", FormattingConversionService.class.getClassLoader()); private Set converters; + private StringValueResolver embeddedValueResolver; + private FormattingConversionService conversionService; @@ -66,8 +70,13 @@ public class FormattingConversionServiceFactoryBean this.converters = converters; } + public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) { + this.embeddedValueResolver = embeddedValueResolver; + } + public void afterPropertiesSet() { this.conversionService = new FormattingConversionService(); + this.conversionService.setEmbeddedValueResolver(this.embeddedValueResolver); ConversionServiceFactory.addDefaultConverters(this.conversionService); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); installFormatters(this.conversionService); diff --git a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java index 144db168ff2..26ca6f3d62c 100644 --- a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java +++ b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java @@ -32,6 +32,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.convert.TypeDescriptor; @@ -88,6 +89,7 @@ public class FormattingConversionServiceTests { @Test public void testFormatFieldForAnnotation() throws Exception { + formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(Model.class); } @@ -102,6 +104,22 @@ public class FormattingConversionServiceTests { context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); context.getBeanFactory().initializeBean(formattingService, "formattingService"); + formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); + doTestFormatFieldForAnnotation(ModelWithPlaceholders.class); + } + + @Test + public void testFormatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception { + GenericApplicationContext context = new GenericApplicationContext(); + PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + Properties props = new Properties(); + props.setProperty("dateStyle", "S-"); + props.setProperty("datePattern", "M/d/yy"); + ppc.setProperties(props); + context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class)); + context.getBeanFactory().registerSingleton("ppc", ppc); + context.refresh(); + formattingService = context.getBean("formattingService", FormattingConversionService.class); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class); } @@ -116,7 +134,6 @@ public class FormattingConversionServiceTests { return source.toDate(); } }); - formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime() .toDate(), new TypeDescriptor(modelClass.getField("date")), TypeDescriptor.valueOf(String.class));