【问题标题】:Change search location in Spring boot 1.3.0在 Spring Boot 1.3.0 中更改搜索位置
【发布时间】:2015-12-15 16:23:59
【问题描述】:

我想外部化我的应用程序的配置,我使用 Spring Boot 1.3.0,我知道类 ConfigFileApplicationListener 有一个默认值 DEFAULT_SEARCH_LOCATIONS。在此类加载默认属性源之前,如何更改配置源?

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    您可以在配置类上使用@PropertySource 注解(注解为@SpringBootApplication 或@Configuration)。

    像这样:

    @SpringBootApplication
    @PropertySource(value = { "classpath:other.properties", "file:./other-external.properties" }, ignoreResourceNotFound = true)
    public class MyApplication {
    

    它将首先在您的类路径中搜索 other.properties,然后在您的 jar 或 war 文件之外查找外部文件 other-external.properties。最新文件会覆盖其他文件的值(如果存在)

    更多详情请见https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html。

    【讨论】:

    • 这是一个解决方案,但没有最好的解决方案。最后,我构建了自己的 DefaultConfigFileApplicationListener。
    • 您能否解释一下,为什么这不是最佳解决方案?
    猜你喜欢
    • 2016-03-10
    • 2015-10-19
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多