Browse Source

added EmbeddedValueResolver support to FormattingConversionServiceFactoryBean (SPR-7087)

pull/1234/head
Juergen Hoeller 15 years ago
parent
commit
2a140addfd
  1. 11
      org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java
  2. 19
      org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java

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

@ -24,6 +24,7 @@ import java.util.Set; @@ -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; @@ -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; @@ -46,13 +48,15 @@ import org.springframework.util.ClassUtils;
* @since 3.0
*/
public class FormattingConversionServiceFactoryBean
implements FactoryBean<FormattingConversionService>, InitializingBean {
implements FactoryBean<FormattingConversionService>, 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 @@ -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);

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

@ -32,6 +32,7 @@ import org.junit.Before; @@ -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 { @@ -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 { @@ -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 { @@ -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));

Loading…
Cancel
Save