The interface ConverterFactory is used to tie a single converter to an entire class hierarchy type.
Definition of ConverterFactory(Version: spring-framework 6.1.2) package org.springframework.core.convert.converter;
........
public interface ConverterFactory<S, R> {
<T extends R> Converter<S, T> getConverter(Class<T> targetType); 1
}
There are number of built-in converter factories provided by Spring. For example let's see StringToNumberConverterFactory:
Source Code of StringToNumberConverterFactory.java(Version: spring-framework 6.1.2) package org.springframework.core.convert.support;
........
final class StringToNumberConverterFactory<T extends Number> implements ConverterFactory {
@Override
public <T extends Number> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToNumber<>(targetType);
}
private static final class StringToNumber implements Converter {
private final Class<T> targetType;
public StringToNumber(Class<T> targetType) {
this.targetType = targetType;
}
@Override
@Nullable
public T convert(String source) {
if (source.isEmpty()) {
return null;
}
return NumberUtils.parseNumber(source, this.targetType);
}
}
}
Where NumberUtils.parseNumber(String text, Class<T> targetClass) parses the given text into a Number instance of the given target class, using the corresponding decode / valueOf method (if interested check out the source code).
Example
package com.logicbig.example;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
public class ConverterFactoryExample {
public static void main(String[] args) {
ConversionService conversionService = new DefaultConversionService();
//StringToNumberConverterFactory is used
Double num = conversionService.convert("1.5", Double.class);
System.out.println(num);
}
}
Output1.5
Built-in converter factories
The built-in converter factories are not public, so we cannot find them in API docs. To know what types can be converted by default please use your IDE to view hierarchy of ConverterFactory interface. Following code list all converter factories defined in Spring core:
package com.logicbig.example;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.type.filter.AssignableTypeFilter;
import java.util.Set;
public class SpringBuiltInConverterFactoriesFinder {
public static void main(String[] args) {
ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AssignableTypeFilter(ConverterFactory.class));
Set<BeanDefinition> components = provider.findCandidateComponents("org/springframework");
components.stream()
.map(BeanDefinition::getBeanClassName)
.sorted()
.forEach(System.out::println);
}
}
Outputorg.springframework.core.convert.support.CharacterToNumberFactory org.springframework.core.convert.support.IntegerToEnumConverterFactory org.springframework.core.convert.support.NumberToNumberConverterFactory org.springframework.core.convert.support.StringToEnumConverterFactory org.springframework.core.convert.support.StringToNumberConverterFactory
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 4.0.7.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|