injecting ConversionService into a custom Converter

Using Spring mvc-3. I am writing a custom Converter which needs access to other Converters registered to a ConversionService.

How can I accomplish this? I tried writing my custom converter as:

  class CustomConverter<X, Y>{
     @Autowired ConversionService service;
     //+getter & setters of service

     public Y convert(X input){
          // I need access to service to lookup simple conversions such as
          // String array to Long array etc..

     }

  }

And I registered my custom converter via applicationContext.xml

  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
     <property name = "converters">
         <list>
            <bean class="CustomConverter"/>
         </list>
     </property>
  </bean>

However, spring refuses to inject service into my CustomConverter(its always null). How can I accomplish this?

Thank you!

Answers


I have used something like this recently to solve this problem. Use a custom factory :

public class MyConversionServiceFactoryBean extends ConversionServiceFactoryBean {

    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();
        ConversionService conversionService = getObject();
        ConverterRegistry registry = (ConverterRegistry) conversionService;
        // register converters that need a nested conversion service
        registry.addConverter(new MyCustomConverter(conversionService));
    }
}

which is declared like this :

<bean id="conversionService"
    class="com.company.MyConversionServiceFactoryBean">
    <property name="converters">
        <list>
            ... declare standard converters here ...
        </list>
    </property>
</bean>

Need Your Help

Using arrays with strings with a while loop

php arrays string

I am writing some code to create fields automatically, which will save me a load of time. I have got most of my code working, but I have came across one error with the code, which is preventing me ...

String cut, trim at first space character

php string

i'm trying to select values from the database and place each and every one on a separate text input field. my code works properly, however, when i tried to display a string on a text field, the str...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.