无法在Spring Boot中将java.util.Duration用作@Value配置 [英] Not able to use java.util.Duration as @Value configuration in Spring Boot

查看:612
本文介绍了无法在Spring Boot中将java.util.Duration用作@Value配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们无法在Spring Boot中将java.util.Duration用作@Value配置.

We are not able to use java.util.Duration as @Value configuration in Spring Boot.

在此处出现此问题的示例项目: https://github.com/costimuraru/spring -boot-sample Spring Boot版本:1.5.9.RELEASE

Sample project that exhibits this problem here: https://github.com/costimuraru/spring-boot-sample Spring Boot version: 1.5.9.RELEASE

@RestController
public class HelloController {

    @Value("${mykey}")
    Duration value;

    @RequestMapping("/")
    public String index() {
        return "Greetings at " + value;
    }

}

...

public class MyConfigPropertySource extends PropertySource {

    MyConfigPropertySource(String name) {
        super(name);
    }

    @Override
    public Object getProperty(String path) {
        return "mykey".equals(path) ? Duration.ofMinutes(1) : null;
    }
}

org.springframework.beans.factory.BeanCreationException:错误 创建名称为"helloController"的bean:自动接线的注入 依赖项失败;嵌套的异常是 org.springframework.core.convert.ConverterNotFoundException:否 发现可以从[java.time.Duration]类型进行转换的转换器 在以下位置键入[java.lang.String] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) 〜[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) 〜[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) 〜[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.Duration] to type [java.lang.String] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

此错误完全是意外的:No converter found capable of converting from type [java.time.Duration] to type [java.lang.String].我们既没有在@Value字段中也没有在PropertySource的return语句中使用String.那么为什么Spring Boot试图将其转换为String?任何帮助都将不胜感激.

This error is totally unexpected: No converter found capable of converting from type [java.time.Duration] to type [java.lang.String]. We are not using String in neither the @Value field nor in the return statement from the PropertySource. So why is Spring Boot trying to convert it to String? Any help is gladly appreciated.

推荐答案

只需使用Spring表达式语言

Just use Spring Expression Language

@Value("#{T(java.time.Duration).parse('${mykey}')}")
private Duration value;

这篇关于无法在Spring Boot中将java.util.Duration用作@Value配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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