【问题标题】:Spring profile configuration based on multiple directories基于多个目录的spring profile配置
【发布时间】:2020-06-21 10:17:29
【问题描述】:

我的 spring boot application.properties 需要针对不同的环境进行设置,例如 dev、test、uat 和 prod。当前项目设置从 application.properties (classpath:resources) 和 config.properties 加载属性(这个具有覆盖 dev、test、prod 中的连接)。 每个环境我们有一堆其他的 .properties 文件和 jks 文件。 我想像这样拆分每个环境的 application.properties:

classpath: 
resources/default/* (loads each property everytime)
resources/local/* (only overrides for local + jks file for this env)
resources/dev/* (only overrides for local + jks file for this env)
resources/test/* (only overrides for local + jks file for this env)
resources/prod/* (only overrides for local + jks file for this env)

我正在使用@configurationproperties 和@value 来绑定值。我可以从清单中传递 spring.profiles.active 。我怎样才能做到这一点?

【问题讨论】:

  • 使用外部化配置。

标签: java spring-boot properties-file


【解决方案1】:

您需要为此设置一些系统属性(通常在 main 中完成,就在 SpringApplication.run(UserApiApplication.class, args); 之前

属性如下。 System.setProperty("spring.config.location", "classpath:/config/dev/");

注意资源文件夹被省略。 加载它们实际上是使用弹簧配置文件完成的,其中配置文件名称是文件名。

例如config/dev/application-something.yml 是个人资料 需要启用。这是一个简单的指南来帮助你。 https://www.baeldung.com/spring-profiles

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2016-12-11
    • 1970-01-01
    • 2016-07-13
    • 2016-11-03
    • 2014-02-14
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多