【发布时间】:2015-12-15 16:23:59
【问题描述】:
我想外部化我的应用程序的配置,我使用 Spring Boot 1.3.0,我知道类 ConfigFileApplicationListener 有一个默认值 DEFAULT_SEARCH_LOCATIONS。在此类加载默认属性源之前,如何更改配置源?
【问题讨论】:
标签: spring spring-boot
我想外部化我的应用程序的配置,我使用 Spring Boot 1.3.0,我知道类 ConfigFileApplicationListener 有一个默认值 DEFAULT_SEARCH_LOCATIONS。在此类加载默认属性源之前,如何更改配置源?
【问题讨论】:
标签: spring spring-boot
您可以在配置类上使用@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。
【讨论】: