使用来自application.properties的Spring表达式语言解析LocalTime

时间:2019-10-23 15:35:54

标签: java spring spring-boot java-time application.properties

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

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

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

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

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

调试模式下的错误:

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

4 个答案:

答案 0 :(得分:1)

要将日期注入@Value,请使用Spring表达式语言(SpEL), 例如:

@Value(“#{new java.text.SimpleDateFormat(‘${aTimeFormat}’).parse(‘${aTimeStr}’)}”)
Date myDate;

在您的情况下,您直接注入Date值而未提供格式化程序,因此它不知道要解析为哪种格式,不定义具有该格式的新属性,然后使用该格式化程序来解析该值,如上例所示,它会被注入。 您的application.properties属性应类似于:

aTimeStr=21:16:46
aTimeFormat=HH:mm:ss

答案 1 :(得分:1)

默认情况下,Spring uses用于时间格式的本地化非iso FormatStyle.SHORT除非被某种方式覆盖。因此,我怀疑您可以使用09:45 PM作为属性值,但这不是您想要的。

但是正如我已经提到的,您可以使用DateTimeFormatterRegistrar机制来全局覆盖Spring期望的模式。

或者您也可以在LocalTime myDateTime字段上使用@DateTimeFormat批注。使用此注释,可以通过三种方式指定所需的格式,即styleisopattern。在您的情况下,iso = DateTimeFormat.ISO.TIME可以工作。因此您的代码将如下所示:

@Value("${app.myDateTime}")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalTime myDateTime;

请注意,这三个属性是互斥的(请参见提供的链接上的javadoc):

每个属性都是互斥的,因此每个注释实例只能设置一个属性

答案 2 :(得分:0)

选项1-使用@ConfigurationPropertiesBinding

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

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

enter image description here

选项2-使用@Value

如果您宁愿坚持使用@Value,那么您就很接近了:

@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选项列表。

来源:

答案 3 :(得分:0)

我已经在jdk-11的Spring Boot 2.1.5上对此进行了测试

@Value("#{ T(java.time.LocalTime).parse('${app.myDateTime}',T(java.time.format.DateTimeFormatter).ISO_LOCAL_TIME)}")
private LocalTime timeValue;   //21:45