added EmbeddedValueResolver support to FormattingConversionServiceFactoryBean (SPR-7087)
This commit is contained in:
parent
eb3a3a6de2
commit
2a140addfd
|
|
@ -24,6 +24,7 @@ import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.context.EmbeddedValueResolverAware;
|
||||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||||
import org.springframework.format.AnnotationFormatterFactory;
|
import org.springframework.format.AnnotationFormatterFactory;
|
||||||
import org.springframework.format.FormatterRegistry;
|
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.datetime.joda.JodaTimeFormattingConfigurer;
|
||||||
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
|
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
import org.springframework.util.StringValueResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A factory for a {@link FormattingConversionService} that installs default
|
* A factory for a {@link FormattingConversionService} that installs default
|
||||||
|
|
@ -46,13 +48,15 @@ import org.springframework.util.ClassUtils;
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class FormattingConversionServiceFactoryBean
|
public class FormattingConversionServiceFactoryBean
|
||||||
implements FactoryBean<FormattingConversionService>, InitializingBean {
|
implements FactoryBean<FormattingConversionService>, EmbeddedValueResolverAware, InitializingBean {
|
||||||
|
|
||||||
private static final boolean jodaTimePresent = ClassUtils.isPresent(
|
private static final boolean jodaTimePresent = ClassUtils.isPresent(
|
||||||
"org.joda.time.LocalDate", FormattingConversionService.class.getClassLoader());
|
"org.joda.time.LocalDate", FormattingConversionService.class.getClassLoader());
|
||||||
|
|
||||||
private Set<?> converters;
|
private Set<?> converters;
|
||||||
|
|
||||||
|
private StringValueResolver embeddedValueResolver;
|
||||||
|
|
||||||
private FormattingConversionService conversionService;
|
private FormattingConversionService conversionService;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -66,8 +70,13 @@ public class FormattingConversionServiceFactoryBean
|
||||||
this.converters = converters;
|
this.converters = converters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) {
|
||||||
|
this.embeddedValueResolver = embeddedValueResolver;
|
||||||
|
}
|
||||||
|
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
this.conversionService = new FormattingConversionService();
|
this.conversionService = new FormattingConversionService();
|
||||||
|
this.conversionService.setEmbeddedValueResolver(this.embeddedValueResolver);
|
||||||
ConversionServiceFactory.addDefaultConverters(this.conversionService);
|
ConversionServiceFactory.addDefaultConverters(this.conversionService);
|
||||||
ConversionServiceFactory.registerConverters(this.converters, this.conversionService);
|
ConversionServiceFactory.registerConverters(this.converters, this.conversionService);
|
||||||
installFormatters(this.conversionService);
|
installFormatters(this.conversionService);
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
import org.springframework.context.support.GenericApplicationContext;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
@ -88,6 +89,7 @@ public class FormattingConversionServiceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFormatFieldForAnnotation() throws Exception {
|
public void testFormatFieldForAnnotation() throws Exception {
|
||||||
|
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
|
||||||
doTestFormatFieldForAnnotation(Model.class);
|
doTestFormatFieldForAnnotation(Model.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,6 +104,22 @@ public class FormattingConversionServiceTests {
|
||||||
context.getBeanFactory().registerSingleton("ppc", ppc);
|
context.getBeanFactory().registerSingleton("ppc", ppc);
|
||||||
context.refresh();
|
context.refresh();
|
||||||
context.getBeanFactory().initializeBean(formattingService, "formattingService");
|
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);
|
doTestFormatFieldForAnnotation(ModelWithPlaceholders.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,7 +134,6 @@ public class FormattingConversionServiceTests {
|
||||||
return source.toDate();
|
return source.toDate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
|
|
||||||
|
|
||||||
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
|
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
|
||||||
.toDate(), new TypeDescriptor(modelClass.getField("date")), TypeDescriptor.valueOf(String.class));
|
.toDate(), new TypeDescriptor(modelClass.getField("date")), TypeDescriptor.valueOf(String.class));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue