Spring ConversionService 添加转换器

2022-01-13 00:00:00 type-conversion spring java

我搜索了以下问题,但找不到答案.

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

我想通过编写实现org.springframework.core.convert.converter.Converter的自定义转换器来使用spring的转换服务.

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>

执行上述操作时,我的应用程序初始化失败,因为我正在覆盖 bean conversionService 并仅注册我的自定义转换器.

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

我怎样才能不覆盖conversionService,只将我的自定义转换器添加到转换器列表中,同时保留现有的?

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?

提前致谢.

推荐答案

在尝试不同的方法,甚至在一些中遵循 Spring 源代码时,我遇到了一件有趣的事情.

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

我发现使用conversionService 而不用我的自定义转换器覆盖现有转换器的唯一方法是扩展或重新实现conversionService,调用超类的afterPropertiesSet() 方法来注册默认转换器,然后添加自定义转换器.

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.

但即使我使用这种方式,在运行时我也会遇到一个异常,即没有为我的特定类型找到转换器(例如,从 String 到 Logger).

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

这引起了我的兴趣,我按照 spring 源代码找出原因,我意识到 spring 试图找到一个使用 PropertyEditor 注册的自定义转换器.我不确定为什么会这样.我必须在这里补充一点,我的应用程序没有使用 spring mvc,并且 conversionService 可能需要以某种方式注册,而我没有这样做.

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

我很想知道为什么 Spring 没有在转换服务的注册表中找到我注册的自定义转换器(或者至少为什么 Spring 没有查看该注册表来查找自定义转换器).我是否缺少任何配置?

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?

相关文章