【问题标题】:What to set for Spring's @PropertySource when using spring.config.location command line arg?使用 spring.config.location 命令行参数时为 Spring 的 @PropertySource 设置什么?
【发布时间】:2017-06-04 20:18:29
【问题描述】:

我已经定义了一个@Config 类,如下所示:

@Config
@PropertySource(value = "")
public class Foo {
...
}

我通过包含以下内容来运行我的程序:

 --spring.config.location=file:///Users/dev/workspace/application.yml

问题是我不能省略 PropertySource 注释的“值”属性。为什么需要设置它?这会覆盖我在--spring.config.location 中设置的内容吗?我应该使用SimpleCommandLinePropertySource 而不是--spring.config.location

【问题讨论】:

  • 为什么需要设置它? Spring Boot 已经根据spring.config.location 属性负责加载正确的application.properties
  • 我不明白你的评论。我是春天的新手。你能详细说明一下吗?我需要这些属性是外部的(与罐子分开)。
  • 如前所述,Spring Boot 会解决这个问题。我建议阅读 Spring Boot reference guide 来解释这一点。假设您正在加载 application.properties,则不需要 @PropertySource
  • 没错。我已经删除了它。感谢您的反馈!

标签: java spring spring-mvc spring-boot spring-properties


【解决方案1】:

当您使用 spring.config.location 使用外部属性文件时,您只需在配置类中设置值:

@Config
@PropertySource(value = "${spring.config.location}")
public class Foo {
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多