如何在没有位置配置 属性 的情况下配置 Spring java 环境?
How to configure the Spring java environment without the locations configuration property?
我正在创建一个名为 FileConfig
的 class,带有以下注释:
@ConfigurationProperties(locations = { "classpath:application.properties" , "${file.config.location}" })
'application.properties' 中存在以下行:
file.config.location = fileinfo.yaml
显然,我的编译器说,位置 属性 已弃用,有利于直接使用其他位置配置环境。有人可以帮助我如何配置环境,以便使用 'fileinfo.yaml' 文件
的内容初始化 FileConfig
中的变量
使用 @PropertySource("classpath:testapplication.properties")
而不是使用上面的代码行会产生错误。
如果我没有正确理解您的问题,您可以在 application.properties
文件中指定 fileInfo.yaml
文件的位置。并且此值应设置为 属性 class FileConfig
.
的位置
我认为那是不可能的,因为没有评估 SpEL 表达式。
这里是来自 @ConfigurationProperties
的 java 文档的一部分:
Annotation for externalized configuration. Add this to a class definition or a @Bean method in a @Configuration class if you want to bind and validate some external Properties (e.g. from a .properties file).
Note that contrary to @Value, SpEL expressions are not evaluated since
property values are externalized.
我正在创建一个名为 FileConfig
的 class,带有以下注释:
@ConfigurationProperties(locations = { "classpath:application.properties" , "${file.config.location}" })
'application.properties' 中存在以下行:
file.config.location = fileinfo.yaml
显然,我的编译器说,位置 属性 已弃用,有利于直接使用其他位置配置环境。有人可以帮助我如何配置环境,以便使用 'fileinfo.yaml' 文件
的内容初始化 FileConfig
中的变量
使用 @PropertySource("classpath:testapplication.properties")
而不是使用上面的代码行会产生错误。
如果我没有正确理解您的问题,您可以在 application.properties
文件中指定 fileInfo.yaml
文件的位置。并且此值应设置为 属性 class FileConfig
.
我认为那是不可能的,因为没有评估 SpEL 表达式。
这里是来自 @ConfigurationProperties
的 java 文档的一部分:
Annotation for externalized configuration. Add this to a class definition or a @Bean method in a @Configuration class if you want to bind and validate some external Properties (e.g. from a .properties file).
Note that contrary to @Value, SpEL expressions are not evaluated since property values are externalized.