如何使用自定义 Spring Converter 转换 XML 配置值? [英] How to use custom Spring Converter to convert XML config values?

查看:27
本文介绍了如何使用自定义 Spring Converter 转换 XML 配置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有自定义 Converter 实现的 Spring ConversionService 来转换 Spring XML 配置中的值.

I want to use the Spring ConversionService with custom Converter implementations to convert values from Spring XML config.

我通过xml config这样配置一个bean:

I configure a bean through xml config like this:

<bean name="A1" class="com.example.MyClass">
    <constructor-arg name="time" value="10"/>
</bean>

关联的类:

import java.time.Duration;

public class MyClass {
    public MyClass(Duration time) {
        System.out.println("TIME: " + time);
    }
}

应该执行转换的转换器是:

The converter that should perform the conversion is:

public class StringToDurationInSecondsConverter implements Converter<String, Duration> {
    @Override
    public Duration convert(String source) {
        int seconds = Integer.valueOf(source);
        return Duration.ofSeconds(seconds);
    }
}

配置如下:

@SpringBootApplication
public class Application {

    @Bean
    public ConversionService conversionService(
            Set<Converter<?, ?>> converters,
            ConversionServiceFactoryBean factory) {
        factory.setConverters(converters);
        return factory.getObject();
    }

    @Bean
    public ConversionServiceFactoryBean conversionServiceFactoryBean() {
        return new ConversionServiceFactoryBean();
    }

    @Bean
    public StringToDurationInSecondsConverter stringToDurationInSecondsConverter() {
        return new StringToDurationInSecondsConverter();
    }
    // ...
}

转换器按预期注入到 conversionService 方法中,因此 conversionService 应该正确初始化.但是无法执行xml配置中参数time的转换.错误信息是:

The converters are injected as expected into conversionService method, so that the conversionService should be properly initialized. But the conversion of the parameter time in the xml config could not be performed. The error message is:

org.springframework.beans.factory.UnsatisfiedDependencyException:在通过 SAX InputSource 加载的资源中定义名称为A1"的 bean 创建错误:通过构造函数参数 0 表示的不满足的依赖关系:无法转换类型 [java.lang.String 的参数值] 到所需类型 [java.time.Duration]:无法将类型java.lang.String"的值转换为所需类型java.time.Duration";嵌套异常是 java.lang.IllegalStateException:无法将java.lang.String"类型的值转换为所需类型java.time.Duration":找不到匹配的编辑器或转换策略

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'A1' defined in resource loaded through SAX InputSource: Unsatisfied dependency expressed through constructor parameter 0: Could not convert argument value of type [java.lang.String] to required type [java.time.Duration]: Failed to convert value of type 'java.lang.String' to required type 'java.time.Duration'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.time.Duration': no matching editors or conversion strategy found

如果我在 convert 方法中设置断点,它永远不会到达.所以看起来转换器从未被调用过.

If I set a breakpoint into my convert method it is never reached. So it looks like the converter is never called.

即使我将 ConversionService 直接注入(修改后的)MyClass 并在构造函数中调用 convert 方法...

Even If I inject the ConversionService directly to the (modified) MyClass and call the convert method within the constructor ...

public class MyClass {
    public MyClass(String time, ConversionService conversionService) {
        final Duration duration = conversionService.convert(time, Duration.class);
        System.out.println(duration);
    }
}

...我得到了例外:

ConverterNotFoundException:找不到能够从类型 [java.lang.String] 转换为类型 [java.time.Duration] 的转换器

ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.time.Duration]

应用程序的核心是使用 Springs Java 配置配置的,但还有一些更易变"的配置,它由另一个 bean 从文件中读取(除了转换之外还有哪些工作).

The core of the application is configured with Springs Java config, but there is some more "volatile" config that is read from a file by another bean (what works apart from the conversion).

是否可以使用带有自定义 Converter 的 Spring 的 ConversionService 来转换 Spring xml 配置中的值?怎么样?

Is it possible to use Spring's ConversionService with custom Converters to convert values in a Spring xml config? How?

推荐答案

感谢 Sotirios Delimanolis 的 ">评论(在另一个问题上)我得出了以下解决方案:>

Thanks to the comment of Sotirios Delimanolis (on another question) I came to the following solution:

@Bean
public ConversionService conversionService(Set<Converter<?, ?>> converters) {
    final ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    factory.setConverters(converters);
    factory.afterPropertiesSet(); // necessary
    return factory.getObject();
}

从 XML 转换属性值现在工作正常.

Converting attribute values from XML works fine now.

这篇关于如何使用自定义 Spring Converter 转换 XML 配置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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