found hotspot; added ConverisonServiceFactoryBean
This commit is contained in:
@@ -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 {
|
||||
|
||||
private ConversionService conversionService = ConversionServiceFactory.createDefault();
|
||||
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||
|
||||
// implementing FormattingRegistry
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.format.support;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer;
|
||||
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
|
||||
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.
|
||||
* Subclasses may override {@link #installFormatters(FormatterRegistry)} to register custom formatters.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
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
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
installNumberFormatting();
|
||||
installJodaTimeFormattingIfPresent();
|
||||
this.conversionService = new FormattingConversionService();
|
||||
installFormatters(this.conversionService);
|
||||
}
|
||||
|
||||
// implementing FactoryBean
|
||||
@@ -45,7 +50,7 @@ public class FormattingConversionServiceFactoryBean implements FactoryBean<Conve
|
||||
return this.conversionService;
|
||||
}
|
||||
|
||||
public Class<? extends ConversionService> getObjectType() {
|
||||
public Class<ConversionService> getObjectType() {
|
||||
return ConversionService.class;
|
||||
}
|
||||
|
||||
@@ -53,17 +58,14 @@ public class FormattingConversionServiceFactoryBean implements FactoryBean<Conve
|
||||
return true;
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
// subclassing hooks
|
||||
|
||||
private void installNumberFormatting() {
|
||||
this.conversionService.addFormatterForFieldType(Number.class, new NumberFormatter());
|
||||
this.conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
|
||||
protected void installFormatters(FormatterRegistry registry) {
|
||||
registry.addFormatterForFieldType(Number.class, new NumberFormatter());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user