gpt4 book ai didi

java - 基于多个目录的Spring Profile配置

转载 作者:行者123 更新时间:2023-12-01 18:02:44 25 4
gpt4 key购买 nike

我的 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来绑定(bind)值。我可以从 list 中传递 spring.profiles.active 。我怎样才能实现这个目标?

最佳答案

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

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

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

例如config/dev/application-something.yml个人资料是什么需要启用该功能。这是一个可以帮助您的简单指南。 https://www.baeldung.com/spring-profiles

关于java - 基于多个目录的Spring Profile配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60602008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com