application.yml 中的 @Value 返回错误值
在我的 application.yml
文件中声明:
In my application.yml
file there is declared:
service:
a:
b: 011600
c: 011200
通过 @Value
注释从 .yml
中选择值:
selecting value from .yml
via @Value
annotation:
@Value("${service.a.c}")
private String VALUE;
我得到的不是预期的011200",而是 .yml 中未提及的4992".
Instead of expected '011200' I am getting '4992', which isn't mentioned in .yml.
您没有指定文档的 YAML 版本,看起来您的解析器默认为 1.1 版(或兼容 1.1 版).这导致以 0 开头的值后跟数字被解释为八进制,011200
(八进制)的值为 4736
(十进制),011600
(八进制)是 4992
.
You did not specify which version YAML your document has, and it looks like your parser defaults to version 1.1 (or version 1.1 compatibility). This causes values starting with 0 followed by digits to be interpreted as octal, and value of 011200
(octal) is 4736
(decimal), the value of 011600
(octal) is 4992
.
在 YAML 1.2 中,八进制整数值应该以 Oo
开头,因此 011200
将是整数 11200
In YAML 1.2, octal integer values should start with Oo
and therefore 011200
will be the integer 11200
尝试使用:
%YAML 1.2
---
service:
a:
b: 011600
c: 011200
如果这不能为您提供所需的价值,您应该考虑使用兼容的 YAML 解析器(YAML 1.2 规范是从 2009 年开始的,因此有足够的时间来解决问题).
and if that doesn't get you the value you want, you should consider using a compliant YAML parser (the YAML 1.2 specification is from 2009, so there has been ample time to get things right).
您当然可以引用您的整数标量,但它们将作为字符串加载,而不是作为整数加载.在这种情况下,您不需要指定版本标签,版本 1.2 和 1.1 的解释方式相同:
You can of course quote your integer scalars, but then they will be loaded as strings, not as integers. In that case you don't need to specify the version tag, both version 1.2 and 1.1 interpreted that the same way:
service:
a:
b: '011600'
c: '011200'
如果在获取文件中指定的十进制值后,仍然得到错误的 @Value("${service.ac}")
值,则应考虑使用不同的访问权限方法(如果键中有一个点(例如 serv.ice:
)或不同的解析器.
If after getting the decimal values you specified in the file, you still get the wrong value for @Value("${service.a.c}")
then, you should consider using a different access method (something that works if there is a dot in the key (e.g. serv.ice:
) or, again, a different parser.