【问题标题】:How to use arithmetical operations i YML file? [duplicate]如何在 YML 文件中使用算术运算? [复制]
【发布时间】:2021-12-12 23:21:15
【问题描述】:

我正在使用 SpringBoot。

在以前的项目中,我使用的是 application.property 文件,内容看起来像这样:

seconds.timeOut=10
interval.milliseconds.cleaner=#{${seconds.timeOut}*2*1000}
interval.seconds.cleanerOffset=#{${seconds.timeOut}*3}

结果是正确的cleaner=20000 和cleanerOffset=30

在新项目中,我切换到 application.yml 文件。具有相同的配置:

      seconds:
        timeOut: 10
      interval:
        milliseconds:
           cleaner: ${seconds.timeOut}*2*1000
      interval:
        seconds:
          cleanerOffset: ${seconds.timeOut}*3

但结果是字符串清洁器 = "10*2*1000" 当然我也有例外:

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'cleaningWorker': Invalid fixedDelayString value "10*2*1000" - cannot parse into long

我找不到任何解决方法,你能帮我吗?

【问题讨论】:

  • 您是否已经尝试将 #{${seconds.timeOut}*2*1000} 作为 YAML 文件中的值?
  • 这能回答你的问题吗? Use SpringBoot SpEL in YAML
  • 是的,我尝试 #{${seconds.timeOut}*2*1000} 但这不起作用!
  • @blurfus 谢谢这正是我正在寻找的。​​span>

标签: java spring yaml


【解决方案1】:

试试:

$(( ${seconds.timeOut} * 2000seconds ))

如果这不起作用,请参考this,这会有所帮助。

【讨论】:

  • 我认为您的解决方案存在语法错误。 2000seconds 似乎不正确。此外,OP 希望在 YAML 文件中进行这些计算
  • 2000 秒的语法是正确的,因为在我的 YAML 文件中类似的工作正常。您还可以使用天、年和月。
【解决方案2】:

谢谢大家。

这个解决方案适合我:

      seconds:
        timeOut: 10
      interval:
        milliseconds:
           cleaner: '#{${seconds.timeOut}*2*1000}'
      interval:
        seconds:
          cleanerOffset: '#{${seconds.timeOut}*3}'

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    相关资源
    最近更新 更多