在yaml中使用算术运算



我在Grails 5.2.5应用程序中使用spring作业。此应用程序用于同时运行X(由reports.total配置定义)报告。

为保证正常运行,spring.task.scheduling.pool.size配置必须大于等于作业总数,符合报告要求。总共需要3+reports.total(值为3,因为当前有3个作业需要并发运行)

reports:
total: 1
spring:
task:
scheduling:
pool:
size: 3+reports.total

我尝试了以下值,但没有成功:

  • ${reports.total} + 3
  • {${reports.total} + 3}
  • 3 + reports.total
  • ${reports.total + 3}
  • ${4+1}
  • 4+1
  • #{${reports.total} + 3}
  • $(( ${reports.total} + 3 ))

总是返回如下内容:

Failed to bind properties under 'spring.task.scheduling.pool.size' to int:
Property: spring.task.scheduling.pool.size
Value: ${4+1}
Origin: "spring.task.scheduling.pool.size" from property source "Config resource 'class path resource [application.yml]' via location 'optional:classpath:/'"
Reason: failed to convert java.lang.String to int (caused by java.lang.NumberFormatException: For input string: "${4+1}")

Grails 5.2.5 - JDK 11.0.16

一般不直接在YAML文件中创建计算字段。但是,如果需要在应用程序中包含经过计算的配置数据或引用类,则可以将其添加到运行时中。groovy文件。

运行时。Groovy文件应该与其他配置文件放在相同的位置。它是在你的应用程序类被加载后处理的,这意味着你可以在这个文件中包含更复杂的配置数据,包括'groovy'代码。

最新更新