pio*_*rek 6 java spring spring-boot
我希望开发人员能够在本地覆盖一些配置属性.(假设我们在google驱动器上工作,每个人都应该在自己的帐户上测试它).我不想使用命令行覆盖属性(因为它必须在每个IDE配置和每个CLI运行中设置).
我想要的是:应用程序应该使用所有标准的spring启动配置文件(application.yml
等),并查找例如local.yml
(在类路径上)或里面的一些文件user.home
.这些附加文件应覆盖其他设置.
如何添加新yml
资源并正确订购?
编辑:我知道春天的默认订单和位置.问题是关于添加新的
小智 6
在 Spring Boot v2.4 之后,有一种新方法可以使用spring.config.import
:https ://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4#导入附加配置
通过将此部分添加到您的application.yml
文件中,您应该能够导入附加配置:
spring:
config:
import: local.yml
Run Code Online (Sandbox Code Playgroud)
文章还有这一段:
进口可以被视为插入在声明它们的文件下方的附加文件。它们遵循与常规多文档文件相同的自上而下的顺序:导入只会导入一次,无论声明多少次。
因此,应将 的内容local.yml
处理为附加到 的末尾application.yml
,从而允许您覆盖中的任何属性application.yml
。
如果您查看 Spring Boot 文档中有关配置文件位置的信息 ( http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config ),您可以看到,它们是从以下位置加载的(其中包括):
There are two default locations where they are loaded from ( see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files):
Current directory in this case means the working directory for the Java process (Usually the directory where the JAR is located, or in case of running with in the IDE, usually the project root folder). So the developers just can place their own configuration files in that places and they are automatically loaded (and will override properties within the JARs). Add that files to .gitignore (or .svnignore or ...) and they won't accidentally committed into your repository.
SpringApplication将从以下位置的application.properties文件加载属性并将它们添加到Spring环境中:
该列表按优先级排序(列表中较高位置定义的属性优先于较低位置定义的属性)。
这也适用于 yaml,因此每个人都可以在config
运行 spring boot jar 的目录下添加 application.yml 。
local.yml
如果您愿意,您还可以使用以下命令自定义额外的配置文件spring.config.location
:
--spring.config.location=classpath:/application.yml,classpath:/local.yml
但请注意:
spring.config.name 和 spring.config.location 很早就被用来确定必须加载哪些文件,因此必须将它们定义为环境属性(通常是操作系统环境、系统属性或命令行参数)。
归档时间: |
|
查看次数: |
19262 次 |
最近记录: |