Multiple properties file for a single spring profile

前端 未结 4 558
广开言路
广开言路 2020-12-20 06:53

We are using spring boot 2.0.0. We have three environments dev, staging, production. Our current config structure

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 07:39

    Yes, it's possible. spring.config.location is used to externalize the config file location in Spring boot applications. This can be used to provide a location of the file in the filesystem or even in the classpath. Based on how you want to provide your application access to the files, you can choose the URI.

    Doing it programmatically:

    @SpringBootApplication
    public class Application {
    
      public static void main(String[] args) {
    
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(Application.class)
            .properties("spring.config.location:classpath:/application-dev.yml,classpath:/application-dev-sqs.yml,classpath:/application-dev-redis.yml")
            .build()
            .run(args);
        }
    }
    

    Doing it via environment variables:

    set SPRING_CONFIG_LOCATION=classpath:/application-dev.yml, \
      classpath:/application-dev-sqs.yml, \
      classpath:/application-dev-redis.yml 
    

    So, you can provide your files as comma-separated values.

    I've used classpath here, it can also be a location in the file system:

    /home/springboot-app/properties/application-dev.yml,/home/springboot-app/properties/application-sqs.yml,/home/springboot-app/properties/application-redis.yml
    

提交回复
热议问题