Spring ConversionService添加转换器 [英] Spring ConversionService adding Converters

查看:516
本文介绍了Spring ConversionService添加转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想通过编写实现的自定义转换器来使用spring的转换服务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正试图找到一个用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没有在conversionService的注册表中找到我注册的自定义转换器(或者至少为什么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?

这篇关于Spring ConversionService添加转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆