found hotspot; added ConverisonServiceFactoryBean
This commit is contained in:
parent
4024b67926
commit
692b1ef636
|
|
@ -841,7 +841,7 @@ public final class DefaultListableBeanFactoryTests {
|
||||||
@Test
|
@Test
|
||||||
public void testCustomConverter() {
|
public void testCustomConverter() {
|
||||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
GenericConversionService conversionService = (GenericConversionService) ConversionServiceFactory.createDefault();
|
GenericConversionService conversionService = (GenericConversionService) ConversionServiceFactory.createDefaultConversionService();
|
||||||
conversionService.addConverter(new Converter<String, Float>() {
|
conversionService.addConverter(new Converter<String, Float>() {
|
||||||
public Float convert(String source) {
|
public Float convert(String source) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,97 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.context.support;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
import org.springframework.core.convert.converter.ConverterFactory;
|
||||||
|
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||||
|
import org.springframework.core.convert.converter.GenericConverter;
|
||||||
|
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A factory for a ConversionService that installs default converters appropriate for most environments.
|
||||||
|
* Set the <code>converters</code> property to supplement or override the default converters.
|
||||||
|
* @author Keith Donald
|
||||||
|
* @since 3.0
|
||||||
|
* @see ConversionServiceFactory#createDefaultConversionService()
|
||||||
|
*/
|
||||||
|
public class ConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean {
|
||||||
|
|
||||||
|
private Set<Object> converters;
|
||||||
|
|
||||||
|
private ConversionService conversionService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the set of custom Converters that should be added.
|
||||||
|
*/
|
||||||
|
public void setConverters(Set<Object> converters) {
|
||||||
|
this.converters = converters;
|
||||||
|
}
|
||||||
|
|
||||||
|
// implementing InitializingBean
|
||||||
|
|
||||||
|
public void afterPropertiesSet() {
|
||||||
|
this.conversionService = createConversionService();
|
||||||
|
registerConverters(this.converters, (ConverterRegistry) this.conversionService);
|
||||||
|
}
|
||||||
|
|
||||||
|
// implementing FactoryBean
|
||||||
|
|
||||||
|
public ConversionService getObject() {
|
||||||
|
return this.conversionService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<? extends ConversionService> getObjectType() {
|
||||||
|
return ConversionService.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// subclassing hooks
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the ConversionService instance returned by this factory bean.
|
||||||
|
*/
|
||||||
|
protected ConversionService createConversionService() {
|
||||||
|
return ConversionServiceFactory.createDefaultConversionService();
|
||||||
|
}
|
||||||
|
|
||||||
|
// internal helpers
|
||||||
|
|
||||||
|
private void registerConverters(Set<Object> converters, ConverterRegistry registry) {
|
||||||
|
if (converters != null) {
|
||||||
|
for (Object converter : converters) {
|
||||||
|
if (converter instanceof Converter<?, ?>) {
|
||||||
|
registry.addConverter((Converter<?, ?>) converter);
|
||||||
|
} else if (converter instanceof ConverterFactory<?, ?>) {
|
||||||
|
registry.addConverterFactory((ConverterFactory<?, ?>) converter);
|
||||||
|
} else if (converter instanceof GenericConverter) {
|
||||||
|
registry.addGenericConverter((GenericConverter) converter);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Each converter must implement one of the Converter, ConverterFactory, or GenericConverter interfaces");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -41,7 +41,7 @@ import org.springframework.format.Printer;
|
||||||
*/
|
*/
|
||||||
public class FormattingConversionService implements FormatterRegistry, ConversionService {
|
public class FormattingConversionService implements FormatterRegistry, ConversionService {
|
||||||
|
|
||||||
private ConversionService conversionService = ConversionServiceFactory.createDefault();
|
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||||
|
|
||||||
// implementing FormattingRegistry
|
// implementing FormattingRegistry
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.format.support;
|
||||||
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.core.convert.ConversionService;
|
import org.springframework.core.convert.ConversionService;
|
||||||
|
import org.springframework.format.FormatterRegistry;
|
||||||
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.format.number.NumberFormatter;
|
import org.springframework.format.number.NumberFormatter;
|
||||||
|
|
@ -25,18 +26,22 @@ import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A factory for a FormattingConversionService that installs default formatters for common types such as numbers and datetimes.
|
* A factory for a FormattingConversionService that installs default formatters for common types such as numbers and datetimes.
|
||||||
|
* Subclasses may override {@link #installFormatters(FormatterRegistry)} to register custom formatters.
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class FormattingConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean {
|
public class FormattingConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean {
|
||||||
|
|
||||||
private FormattingConversionService conversionService = new FormattingConversionService();
|
private static final boolean jodaTimePresent = ClassUtils.isPresent(
|
||||||
|
"org.joda.time.DateTime", FormattingConversionService.class.getClassLoader());
|
||||||
|
|
||||||
|
private FormattingConversionService conversionService;
|
||||||
|
|
||||||
// implementing InitializingBean
|
// implementing InitializingBean
|
||||||
|
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
installNumberFormatting();
|
this.conversionService = new FormattingConversionService();
|
||||||
installJodaTimeFormattingIfPresent();
|
installFormatters(this.conversionService);
|
||||||
}
|
}
|
||||||
|
|
||||||
// implementing FactoryBean
|
// implementing FactoryBean
|
||||||
|
|
@ -45,7 +50,7 @@ public class FormattingConversionServiceFactoryBean implements FactoryBean<Conve
|
||||||
return this.conversionService;
|
return this.conversionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<? extends ConversionService> getObjectType() {
|
public Class<ConversionService> getObjectType() {
|
||||||
return ConversionService.class;
|
return ConversionService.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,17 +58,14 @@ public class FormattingConversionServiceFactoryBean implements FactoryBean<Conve
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal helpers
|
// subclassing hooks
|
||||||
|
|
||||||
private void installNumberFormatting() {
|
protected void installFormatters(FormatterRegistry registry) {
|
||||||
this.conversionService.addFormatterForFieldType(Number.class, new NumberFormatter());
|
registry.addFormatterForFieldType(Number.class, new NumberFormatter());
|
||||||
this.conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
|
registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
|
||||||
|
if (jodaTimePresent) {
|
||||||
|
new JodaTimeFormattingConfigurer().installJodaTimeFormatting(registry);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void installJodaTimeFormattingIfPresent() {
|
|
||||||
if (ClassUtils.isPresent("org.joda.time.DateTime", FormattingConversionService.class.getClassLoader())) {
|
|
||||||
new JodaTimeFormattingConfigurer().installJodaTimeFormatting(this.conversionService);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -25,26 +25,14 @@ import org.springframework.core.convert.ConversionService;
|
||||||
*/
|
*/
|
||||||
public final class ConversionServiceFactory {
|
public final class ConversionServiceFactory {
|
||||||
|
|
||||||
private static ConversionService DEFAULT_INSTANCE;
|
|
||||||
|
|
||||||
private ConversionServiceFactory() {
|
private ConversionServiceFactory() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the shared default ConversionService.
|
|
||||||
*/
|
|
||||||
public static synchronized ConversionService getDefault() {
|
|
||||||
if (DEFAULT_INSTANCE == null) {
|
|
||||||
DEFAULT_INSTANCE = createDefault();
|
|
||||||
}
|
|
||||||
return DEFAULT_INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new default ConversionService prototype that can be safely modified.
|
* Create a new default ConversionService prototype that can be safely modified.
|
||||||
*/
|
*/
|
||||||
public static ConversionService createDefault() {
|
public static ConversionService createDefaultConversionService() {
|
||||||
GenericConversionService conversionService = new GenericConversionService();
|
GenericConversionService conversionService = new GenericConversionService();
|
||||||
conversionService.addGenericConverter(new ArrayToArrayConverter(conversionService));
|
conversionService.addGenericConverter(new ArrayToArrayConverter(conversionService));
|
||||||
conversionService.addGenericConverter(new ArrayToCollectionConverter(conversionService));
|
conversionService.addGenericConverter(new ArrayToCollectionConverter(conversionService));
|
||||||
|
|
|
||||||
|
|
@ -48,25 +48,7 @@ import org.springframework.core.convert.converter.Converter;
|
||||||
|
|
||||||
public class DefaultConversionTests {
|
public class DefaultConversionTests {
|
||||||
|
|
||||||
private ConversionService conversionService = ConversionServiceFactory.getDefault();
|
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||||
|
|
||||||
@Test
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public void testUnmodifiableListConversion() {
|
|
||||||
List<String> stringList = new ArrayList<String>();
|
|
||||||
stringList.add("foo");
|
|
||||||
stringList.add("bar");
|
|
||||||
|
|
||||||
List<String> frozenList = Collections.unmodifiableList(stringList);
|
|
||||||
|
|
||||||
List<String> converted = conversionService.convert(frozenList, List.class);
|
|
||||||
|
|
||||||
// The converted list should contain all the elements in the original list
|
|
||||||
Assert.assertEquals(frozenList, converted);
|
|
||||||
// Looks like it was supposed to be a copy (but CollectionToCollectionConverter
|
|
||||||
// doesn't work that way right now). Commented out (DS).
|
|
||||||
// Assert.assertNotSame(frozenList, converted);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStringToCharacter() {
|
public void testStringToCharacter() {
|
||||||
|
|
@ -740,4 +722,22 @@ public class DefaultConversionTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public void testUnmodifiableListConversion() {
|
||||||
|
List<String> stringList = new ArrayList<String>();
|
||||||
|
stringList.add("foo");
|
||||||
|
stringList.add("bar");
|
||||||
|
|
||||||
|
List<String> frozenList = Collections.unmodifiableList(stringList);
|
||||||
|
|
||||||
|
List<String> converted = conversionService.convert(frozenList, List.class);
|
||||||
|
|
||||||
|
// The converted list should contain all the elements in the original list
|
||||||
|
Assert.assertEquals(frozenList, converted);
|
||||||
|
// TODO Looks like it was supposed to be a copy (but CollectionToCollectionConverter
|
||||||
|
// doesn't work that way right now). Commented out (DS).
|
||||||
|
// Assert.assertNotSame(frozenList, converted);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,10 +37,12 @@ import org.springframework.util.Assert;
|
||||||
*/
|
*/
|
||||||
public class StandardTypeConverter implements TypeConverter {
|
public class StandardTypeConverter implements TypeConverter {
|
||||||
|
|
||||||
|
private static ConversionService DEFAULT_INSTANCE;
|
||||||
|
|
||||||
private final ConversionService conversionService;
|
private final ConversionService conversionService;
|
||||||
|
|
||||||
public StandardTypeConverter() {
|
public StandardTypeConverter() {
|
||||||
this.conversionService = ConversionServiceFactory.getDefault();
|
this.conversionService = getDefaultConversionService();
|
||||||
}
|
}
|
||||||
|
|
||||||
public StandardTypeConverter(ConversionService conversionService) {
|
public StandardTypeConverter(ConversionService conversionService) {
|
||||||
|
|
@ -64,4 +66,12 @@ public class StandardTypeConverter implements TypeConverter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ConversionService getDefaultConversionService() {
|
||||||
|
synchronized(this) {
|
||||||
|
if (DEFAULT_INSTANCE == null) {
|
||||||
|
DEFAULT_INSTANCE = ConversionServiceFactory.createDefaultConversionService();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return DEFAULT_INSTANCE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||||
*/
|
*/
|
||||||
private static class TypeConvertorUsingConversionService implements TypeConverter {
|
private static class TypeConvertorUsingConversionService implements TypeConverter {
|
||||||
|
|
||||||
private final ConversionService service = ConversionServiceFactory.createDefault();
|
private final ConversionService service = ConversionServiceFactory.createDefaultConversionService();
|
||||||
|
|
||||||
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
|
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
|
||||||
return this.service.canConvert(sourceType, targetType);
|
return this.service.canConvert(sourceType, targetType);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue