Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用application.properties中的spring表达式语言解析LocalTime_Java_Spring_Spring Boot_Java Time_Application.properties - Fatal编程技术网

Java 使用application.properties中的spring表达式语言解析LocalTime

Java 使用application.properties中的spring表达式语言解析LocalTime,java,spring,spring-boot,java-time,application.properties,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 @Value("${app.myDateTime}") @DateTimeFormat(

我试图用以下代码从spring的application.properties解析LocalTime:

@Value("#{ T(java.time.LocalTime).parse('${app.myDateTime}')}")
private LocalTime myDateTime;
在application.properties中,我定义了如下属性:

app.myDateTime=21:45:00
@Value("${app.myDateTime}")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalTime myDateTime;
错误消息:

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

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

@Value(“#{new java.text.SimpleDateFormat(‘${aTimeFormat}’).parse(‘${aTimeStr}’)}”)
Date myDate;
在您的例子中,您直接注入日期值,而不提供格式化程序,因此它不知道解析为什么格式,使用该格式定义一个新属性,并使用该格式化程序解析值,就像上面的示例一样,它将被注入。 您的application.properties属性应类似于:

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

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

@Value(“#{new java.text.SimpleDateFormat(‘${aTimeFormat}’).parse(‘${aTimeStr}’)}”)
Date myDate;
在您的例子中,您直接注入日期值,而不提供格式化程序,因此它不知道解析为什么格式,使用该格式定义一个新属性,并使用该格式化程序解析值,就像上面的示例一样,它将被注入。 您的application.properties属性应类似于:

aTimeStr=21:16:46
aTimeFormat=HH:mm:ss
选项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 LocalTime.parse(source, DateTimeFormatter.ofPattern("HH:mm:ss"));
  }
}
有关DateTimeFormatter选项的列表,请参阅

资料来源:

选项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 LocalTime.parse(source, DateTimeFormatter.ofPattern("HH:mm:ss"));
  }
}
有关DateTimeFormatter选项的列表,请参阅

资料来源:


我已经用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

我已经用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
默认情况下,本地化的非iso FormatStyle.SHORT表示时间格式,除非以某种方式覆盖它。所以我怀疑你可以用09:45 PM来计算财产价值,但这不是你想要的

但正如我已经提到的,您可以全局覆盖Spring使用机器所期望的模式

或者也可以在LocalTime myDateTime字段上使用注释。使用此注释,有三种方法可以指定所需的格式,即样式、iso、图案。在您的情况下,iso=DateTimeFormat.iso.TIME可以工作。因此,您的代码将如下所示:

app.myDateTime=21:45:00
@Value("${app.myDateTime}")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalTime myDateTime;
请注意,这三个属性是互斥的,请参见提供的链接中的javadoc:

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

默认情况下,本地化的非iso FormatStyle.SHORT表示时间格式,除非以某种方式覆盖它。所以我怀疑你可以用09:45 PM来计算财产价值,但这不是你想要的

但正如我已经提到的,您可以全局覆盖Spring使用机器所期望的模式

或者也可以在LocalTime myDateTime字段上使用注释。使用此注释,有三种方法可以指定所需的格式,即样式、iso、图案。在您的情况下,iso=DateTimeFormat.iso.TIME可以工作。因此,您的代码将如下所示:

app.myDateTime=21:45:00
@Value("${app.myDateTime}")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalTime myDateTime;
请注意,这三个属性是互斥的,请参见提供的链接中的javadoc:

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


这对本地时间有效吗?因为OP使用的是LocalTime。我试过使用DateTimeFormatter,比如java.time.format.DateTimeFormatter.of模式'HH:mm:ss',但不起作用。错误:无法将类型为“java.time.format.Parsed”的值转换为所需类型“java.time.LocalTime”:没有匹配的编辑器或转换策略。这是否适用于LocalTime?因为OP使用的是LocalTime。我试过使用DateTimeFormatter,比如java.time.format.DateTimeFormatter.of模式'HH:mm:ss',但不起作用。错误:无法将类型为“java.time.format.Parsed”的值转换为所需类型“java.time.LocalTime”:没有匹配的编辑器或转换策略。我正在查找第二个选项,但我遇到了相同的错误:未能将java.lang.String转换为@org.springframework.beans.factory.annotation.value java.time。LocalTime@DenisStephanov我听到这个消息我很惊讶,所以我自己尝试了第一种选择。它确实有效。答案中唯一的错误是,您需要HH:mm:ss,以便可以在[0-23]的时间间隔内解析小时数。我用小时修正更新了答案,并用屏幕截图显示了我的答案。也许第二个答案也因为同样的原因失败了。我链接了DateTimeFormatter文档,旨在使DateTimeFormatter适合您的需要。我用堆栈跟踪更新了问题,请检查一下,第二个选项是我正在寻找的,但我得到了相同的错误:未能将java.lang.String转换为@org.springframework.beans.factory.annotation.Value java.time。LocalTime@DenisStephanov听到这个我很惊讶,所以
我自己尝试了第一种选择。它确实有效。答案中唯一的错误是,您需要HH:mm:ss,以便可以在[0-23]的时间间隔内解析小时数。我用小时修正更新了答案,并用屏幕截图显示了我的答案。也许第二个答案也因为同样的原因失败了。我链接了DateTimeFormatter文档,旨在使DateTimeFormatter适合您的需要。我使用堆栈跟踪更新了问题,请检查它。您需要使用堆栈添加完整的错误消息trace@Deadpool不幸的是。应用程序启动失败,原因是我写的有问题:/启用调试级日志记录并重新运行应用程序,我也很想知道这个应用程序。my-date-timeproperty@Deadpool我用堆栈跟踪更新了问题,请检查它并尝试我的答案,它对我有效。你需要用堆栈添加完整的错误消息trace@Deadpool不幸的是。应用程序启动失败,原因是我写的有问题:/启用调试级日志记录并重新运行应用程序,我也很想知道这个应用程序。my-date-timeproperty@Deadpool我用stack trace更新了问题,请查看我的答案,我的答案对meI有用,请不要理解。。。与此相同的错误:/I我正在使用OpenJDK8,可能是这个问题吗?您可能会犯一些小错误,或者您可以尝试升级java版本吗@DenisStephanovI不要轻描淡写。。。与此相同的错误:/I我正在使用OpenJDK8,可能是这个问题吗?您可能会犯一些小错误,或者您可以尝试升级java版本吗@德尼斯泰普哈诺夫