Skip to content

Commit 2a140ad

Browse files
committed
added EmbeddedValueResolver support to FormattingConversionServiceFactoryBean (SPR-7087)
1 parent eb3a3a6 commit 2a140ad

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.beans.factory.FactoryBean;
2626
import org.springframework.beans.factory.InitializingBean;
27+
import org.springframework.context.EmbeddedValueResolverAware;
2728
import org.springframework.core.convert.support.ConversionServiceFactory;
2829
import org.springframework.format.AnnotationFormatterFactory;
2930
import org.springframework.format.FormatterRegistry;
@@ -33,6 +34,7 @@
3334
import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer;
3435
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
3536
import org.springframework.util.ClassUtils;
37+
import org.springframework.util.StringValueResolver;
3638

3739
/**
3840
* A factory for a {@link FormattingConversionService} that installs default
@@ -46,13 +48,15 @@
4648
* @since 3.0
4749
*/
4850
public class FormattingConversionServiceFactoryBean
49-
implements FactoryBean<FormattingConversionService>, InitializingBean {
51+
implements FactoryBean<FormattingConversionService>, EmbeddedValueResolverAware, InitializingBean {
5052

5153
private static final boolean jodaTimePresent = ClassUtils.isPresent(
5254
"org.joda.time.LocalDate", FormattingConversionService.class.getClassLoader());
5355

5456
private Set<?> converters;
5557

58+
private StringValueResolver embeddedValueResolver;
59+
5660
private FormattingConversionService conversionService;
5761

5862

@@ -66,8 +70,13 @@ public void setConverters(Set<?> converters) {
6670
this.converters = converters;
6771
}
6872

73+
public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) {
74+
this.embeddedValueResolver = embeddedValueResolver;
75+
}
76+
6977
public void afterPropertiesSet() {
7078
this.conversionService = new FormattingConversionService();
79+
this.conversionService.setEmbeddedValueResolver(this.embeddedValueResolver);
7180
ConversionServiceFactory.addDefaultConverters(this.conversionService);
7281
ConversionServiceFactory.registerConverters(this.converters, this.conversionService);
7382
installFormatters(this.conversionService);

org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.Test;
3333

3434
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
35+
import org.springframework.beans.factory.support.RootBeanDefinition;
3536
import org.springframework.context.i18n.LocaleContextHolder;
3637
import org.springframework.context.support.GenericApplicationContext;
3738
import org.springframework.core.convert.TypeDescriptor;
@@ -88,6 +89,7 @@ public LocalDate convert(DateTime source) {
8889

8990
@Test
9091
public void testFormatFieldForAnnotation() throws Exception {
92+
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
9193
doTestFormatFieldForAnnotation(Model.class);
9294
}
9395

@@ -102,6 +104,22 @@ public void testFormatFieldForAnnotationWithPlaceholders() throws Exception {
102104
context.getBeanFactory().registerSingleton("ppc", ppc);
103105
context.refresh();
104106
context.getBeanFactory().initializeBean(formattingService, "formattingService");
107+
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
108+
doTestFormatFieldForAnnotation(ModelWithPlaceholders.class);
109+
}
110+
111+
@Test
112+
public void testFormatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception {
113+
GenericApplicationContext context = new GenericApplicationContext();
114+
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
115+
Properties props = new Properties();
116+
props.setProperty("dateStyle", "S-");
117+
props.setProperty("datePattern", "M/d/yy");
118+
ppc.setProperties(props);
119+
context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
120+
context.getBeanFactory().registerSingleton("ppc", ppc);
121+
context.refresh();
122+
formattingService = context.getBean("formattingService", FormattingConversionService.class);
105123
doTestFormatFieldForAnnotation(ModelWithPlaceholders.class);
106124
}
107125

@@ -116,7 +134,6 @@ public Date convert(DateTime source) {
116134
return source.toDate();
117135
}
118136
});
119-
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
120137

121138
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
122139
.toDate(), new TypeDescriptor(modelClass.getField("date")), TypeDescriptor.valueOf(String.class));

0 commit comments

Comments
 (0)