Spring ConversionService adding Converters

后端 未结 9 796
别那么骄傲
别那么骄傲 2020-12-31 03:47

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.spri

9条回答
  •  误落风尘
    2020-12-31 04:38

    While experimenting with different ways and even following spring source code in some i came across with an interesting thing.

    The only way i found to use conversionService without overriding the existing converters with my custom ones was either to extend or re-implement the conversionService calling the super class's afterPropertiesSet() method to register the default converters and then add the custom ones.

    But even if i was using that way, at runtime i was getting an exception that no converter was found for my specific types (e.g. from String to Logger).

    That triggered my interest and i followed spring source code to find out why and i realized that spring was trying to find a custom converter registered with PropertyEditor. I am not sure why this is happening. I have to add here that my application is not using spring mvc and conversionService might somehow need to be registered and i didn't do it.

    Finally, i solved the issue with registering a custom converter using Property editor. This documentation can be viewed as reference:

    http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html

    I would be very interested in knowing why Spring was not finding my registered custom converters in the conversionService's registry (or at least why spring was not looking at that registry to find the custom converters). Was i missing any configuration?

提交回复
热议问题