Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • SPRING TYPE CONVERSION(SPI Converter)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 365
    Comment on it

    SPI Converter

    The Spring 3.0 provide the mechanism of type conversion and general Type Conversion. We have SPI to implement type conversion logic and the API that execute Type Conversion at runtime. This API can be used anywhere in Application where you want to do the type conversion.

    For Type Conversion Spring 3 Provides following API

    Converter:- Converter interface is allow you to convert from one type to another type.

    package org.springframework.core.convert.converter;
    
            public interface Converter<S, T> 
            {
                    T convert(S source);    
            }
    

    Here, S is the source type and T is the target type. The Converter interface requires implementing classes to have one method that is convert( ) . The convert method must take a parameter of the source type and return a reference of the target type.

    Example:-

    package org.springframework.core.convert.support;
    
            final class IntegerToString implements Converter<Integer, String> 
            {
    
                   public String convert(Integer source) 
                {
                        return source.toString();
                   }
    
            }
    

    ConverterFactory:- This is used when you want to Centralized your conversion Logic for entire class hierarchy

    package org.springframework.core.convert.converter;
    
            public interface ConverterFactory<S, R> 
            {
                <T extends R> Converter<S, T> getConverter(Class<T> targetType);
            }
    

    The S is the source type which you want to convert,T is the target type and state the parent base class of classes to be converted in 'R'. To implement getConverter() method. In this case, 'T' will be the subclass type of 'R

    Example:-

    final class StringToNumberConverterFactory implements ConverterFactory<String, Number> {
    
        public <T extends Number> Converter<String, T> getConverter(Class<T> targetType) {
            return new StringToNumber<T>(targetType);
        }
    
        private static final class StringToNumber<T extends Number> implements Converter<String, T> {
    
            private final Class<T> targetType;
    
            public StringToNumber(Class<T> targetType) {
                this.targetType = targetType;
            }
    
            public T convert(String source) {
                if (source.length() == 0) {
                    return null;
                }
                return NumberUtils.parseNumber(source, this.targetType);
            }
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: