added EmbeddedValueResolver support to FormattingConversionServiceFactoryBean (SPR-7087)

This commit is contained in:
Juergen Hoeller 2010-06-08 20:40:54 +00:00
parent eb3a3a6de2
commit 2a140addfd
2 changed files with 28 additions and 2 deletions

View File

@ -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<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
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);

View File

@ -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));