使用来自application.properties的Spring表达式语言解析LocalTime [英] Parse LocalTime using spring expression language from application.properties

查看:214
本文介绍了使用来自application.properties的Spring表达式语言解析LocalTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从spring的application.properties中解析LocalTime:

I am trying to parse LocalTime from spring's application.properties with following code:

@Value("#{ T(java.time.LocalTime).parse('${app.myDateTime}')}")
private LocalTime myDateTime;

在application.properties中,我已经定义了如下属性:

in application.properties I have defined property like this:

app.myDateTime=21:45:00

错误消息:

Failed to bind properties under 'app.my-date-time' to java.time.LocalTime:

Property: app.my-date-time
Value: 21:45:00
Origin: class path resource [application.properties]:44:15
Reason: failed to convert java.lang.String to @org.springframework.beans.factory.annotation.Value java.time.LocalTime

知道我做错了什么吗?谢谢.

Any idea what I did wrong? Thank you.

调试模式下的错误:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalTime] for value '21:45:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [21:45:00]
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
    at org.springframework.boot.context.properties.bind.BindConverter$CompositeConversionService.convert(BindConverter.java:170)
    at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:96)
    at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:88)
    at org.springframework.boot.context.properties.bind.Binder.bindProperty(Binder.java:313)
    at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:258)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:214)
    ... 210 common frames omitted
Caused by: java.lang.IllegalArgumentException: Parse attempt failed for value [21:45:00]
    at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:206)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
    ... 217 common frames omitted
Caused by: java.time.format.DateTimeParseException: Text '21:45:00' could not be parsed at index 5
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalTime.parse(LocalTime.java:441)
    at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:72)
    at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:46)
    at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:200)
    ... 218 common frames omitted

推荐答案

选项1-使用@ConfigurationPropertiesBinding

如果使用@ConfigurationProperties加载属性,则可以使用@ConfigurationPropertiesBinding将自定义转换器绑定到Spring:

Option 1 - use @ConfigurationPropertiesBinding

If you use @ConfigurationProperties to load your properties you can use @ConfigurationPropertiesBinding to bind custom converters to Spring:

@Component
@ConfigurationPropertiesBinding
public class LocalTimeConverter implements Converter<String, LocalTime> {
  @Override
  public LocalTime convert(String source) {
      if(source==null){
          return null;
      }
      return LocalTime.parse(source, DateTimeFormatter.ofPattern("HH:mm:ss"));
  }
}

如果您更喜欢@Value,那么您就很接近了:

If you rather stick to @Value then you were pretty close:

@Value("#{T(java.time.LocalTime).parse('${app.myDateTime}', T(java.time.format.DateTimeFormatter).ofPattern('HH:mm:ss'))}")

请参见 https://docs. oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html 中的DateTimeFormatter选项列表.

See https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html for a list of DateTimeFormatter options.

来源:

https: //www.logicbig.com/tutorials/spring-framework/spring-boot/custom-configuration-properties-binding.html

这篇关于使用来自application.properties的Spring表达式语言解析LocalTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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