【问题标题】:How to configure the Spring java environment without the locations configuration property?如何在没有位置配置属性的情况下配置 Spring java 环境?
【发布时间】:2017-09-02 06:01:35
【问题描述】:

我正在创建一个名为 FileConfig 的类,并带有以下注释:

@ConfigurationProperties(locations = { "classpath:application.properties" , "${file.config.location}" })

“application.properties”中存在以下行:

file.config.location = fileinfo.yaml

显然,我的编译器说,locations 属性已被弃用,有利于直接使用其他位置配置环境。有人可以帮我如何配置环境,以便使用“fileinfo.yaml”文件的内容初始化FileConfig 中的变量

使用@PropertySource("classpath:testapplication.properties") 而不是使用上面的代码行会产生错误。

【问题讨论】:

标签: java spring spring-boot properties-file


【解决方案1】:

如果我正确理解您的问题,请在您的application.properties 文件中指定fileInfo.yaml 文件的位置。并且这个值应该设置为你的属性类的位置FileConfig。

我认为这是不可能的,因为 SpEL 表达式没有被评估。

这里是来自@ConfigurationProperties的java doc部分:

外部化配置的注释。如果您想绑定和验证一些外部属性(例如,来自 .properties 文件),请将其添加到类定义或 @Configuration 类中的 @Bean 方法中。

请注意,与@Value 相反,SpEL 表达式不会被计算,因为 属性值被外部化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 2018-10-26
    • 2019-03-24
    • 2014-12-01
    • 1970-01-01
    • 2017-11-04
    • 2014-12-05
    • 2021-10-11
    相关资源
    最近更新 更多