• <bdo id='KTxUp'></bdo><ul id='KTxUp'></ul>

      1. <tfoot id='KTxUp'></tfoot>
        <i id='KTxUp'><tr id='KTxUp'><dt id='KTxUp'><q id='KTxUp'><span id='KTxUp'><b id='KTxUp'><form id='KTxUp'><ins id='KTxUp'></ins><ul id='KTxUp'></ul><sub id='KTxUp'></sub></form><legend id='KTxUp'></legend><bdo id='KTxUp'><pre id='KTxUp'><center id='KTxUp'></center></pre></bdo></b><th id='KTxUp'></th></span></q></dt></tr></i><div id='KTxUp'><tfoot id='KTxUp'></tfoot><dl id='KTxUp'><fieldset id='KTxUp'></fieldset></dl></div>

        <small id='KTxUp'></small><noframes id='KTxUp'>

        <legend id='KTxUp'><style id='KTxUp'><dir id='KTxUp'><q id='KTxUp'></q></dir></style></legend>

        Spring ConversionService 添加转换器

        时间:2023-07-27
        <tfoot id='DUIAi'></tfoot>

          • <bdo id='DUIAi'></bdo><ul id='DUIAi'></ul>
            <legend id='DUIAi'><style id='DUIAi'><dir id='DUIAi'><q id='DUIAi'></q></dir></style></legend>
          • <i id='DUIAi'><tr id='DUIAi'><dt id='DUIAi'><q id='DUIAi'><span id='DUIAi'><b id='DUIAi'><form id='DUIAi'><ins id='DUIAi'></ins><ul id='DUIAi'></ul><sub id='DUIAi'></sub></form><legend id='DUIAi'></legend><bdo id='DUIAi'><pre id='DUIAi'><center id='DUIAi'></center></pre></bdo></b><th id='DUIAi'></th></span></q></dt></tr></i><div id='DUIAi'><tfoot id='DUIAi'></tfoot><dl id='DUIAi'><fieldset id='DUIAi'></fieldset></dl></div>
              <tbody id='DUIAi'></tbody>
            • <small id='DUIAi'></small><noframes id='DUIAi'>

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

                  问题描述

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

                  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?

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

                  上一篇:在 Java 中,如何将 InputStream 转换为字节数组 (b 下一篇:从 2 或 4 字节转换为有符号/无符号 short/int

                  相关文章

                  最新文章

                    <legend id='y5b5y'><style id='y5b5y'><dir id='y5b5y'><q id='y5b5y'></q></dir></style></legend>
                      <bdo id='y5b5y'></bdo><ul id='y5b5y'></ul>
                    <i id='y5b5y'><tr id='y5b5y'><dt id='y5b5y'><q id='y5b5y'><span id='y5b5y'><b id='y5b5y'><form id='y5b5y'><ins id='y5b5y'></ins><ul id='y5b5y'></ul><sub id='y5b5y'></sub></form><legend id='y5b5y'></legend><bdo id='y5b5y'><pre id='y5b5y'><center id='y5b5y'></center></pre></bdo></b><th id='y5b5y'></th></span></q></dt></tr></i><div id='y5b5y'><tfoot id='y5b5y'></tfoot><dl id='y5b5y'><fieldset id='y5b5y'></fieldset></dl></div>

                    <tfoot id='y5b5y'></tfoot>
                    1. <small id='y5b5y'></small><noframes id='y5b5y'>