Alex Rivera | Logout

Spring ConversionService adding Converters

Asked 2012-10-08T09:56:24.533
19

I searched for the below problem, but couldn't find an answer.

I want to use spring's conversion service by writing my custom converter that implements org.springframework.core.convert.converter.Converter.

Then i add my custom converter as below:

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean id="StringToLoggerConverter" class="com.citi.spring.converter.LoggerConverter" />
       </list>
   </property>
</bean>

When doing the above my application initialization fails, because i am overriding bean conversionService and registering only my custom converter.

How can i not override the conversionService and only add my custom converter to the list of converters, at the same time keeping the existing ones?

Thanks in advance.

Edit
Report

1 Answer

0

write a custom conversionService

public class CustomerConversionServiceFactoryBean extends ConversionServiceFactoryBean {

    List<Converter<Object, Object>> converters = new ArrayList<>();

    public CustomerConversionServiceFactoryBean() {
        super();

    DefaultConversionService conversionservice = (DefaultConversionService)  super.getObject();

        for(int i=0;i<converters.size();i++){
            conversionservice.addConverter(converters.get(i));
        }
    }
}

then change your xml

<bean id="conversionService"
    class="CustomerConversionServiceFactoryBean" >
    <property name="converters" >
        <list>
            <bean class="CustomConverter"  />
        </list>
    </property>
</bean>

I think this will help you...

answered 2012-10-08T12:24:35.530

Your Answer