@Value
是一个核心容器特性,它不提供与类型安全配置属性相同的特性。下表总结了 @ConfigurationProperties
和 @Value
支持的特性:
特性 | @ConfigurationProperties | @Value |
---|---|---|
Relaxed binding | YES | NO |
Meta-data support | YES | NO |
SpEL evaluation | No | Yes |
If you define a set of configuration keys for your own components, we recommend you to group them in a POJO annotated with @ConfigurationProperties
. Please also be aware that since @Value
does not support relaxed binding, it isn’t a great candidate if you need to provide the value using environment variables.
如果您为自己的组件定义了一组配置键,我们建议您将它们分组到一个带有 @ConfigurationProperties
注释的 POJO 中。还请注意,由于 @Value
不支持 relaxed binding,如果您需要使用环境变量提供相应的值,那么它不是一个很好的选择。
Finally, while you can write a SpEL
expression in @Value
, such expressions are not processed from Application property files.
最后,虽然可以在 @Value
中编写 SpEL
表达式,但是这些表达式并不从应用程序属性文件中处理。