Spring 配置文件的正确顺序是什么?

问题描述 投票:0回答:1

告诉我如何正确配置 Spring 配置文件的顺序?

我有带有 application-lib.yml 文件的 lib 库

lib:
   test:
     name: Lib

应用程序本身有两个配置文件 application.yml

server:
   port: 8080
lib:
   test:
     name: App

application-local.yml

server:
   port: 4444

我按照以下配置文件顺序启动应用程序 - lib、{其他配置文件}、默认、本地 但由于某种原因,该属性对我来说并没有改变

lib.test.name - 它仍然是旧值Lib

我找到了文档,但不太清楚如何设置我需要的订单

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

spring spring-boot properties
1个回答
0
投票

正如您提到的文档中提到的:

配置数据文件按以下顺序考虑:

  1. 应用程序属性打包在 jar 内 (

    application.properties
    和 YAML 变体)。

  2. 特定于配置文件的应用程序属性打包在 jar 中 (

    application-{profile}.properties
    和 YAML 变体)。

  3. 打包 jar 之外的应用程序属性 (

    application.properties
    和 YAML 变体)。

  4. 打包 jar 之外的特定于配置文件的应用程序属性 (

    application-{profile}.properties
    和 YAML 变体)。

application.properties
,或者根据您的情况,始终首先考虑
application.yml
。因此,将
default
配置文件放在活动配置文件列表中不会影响考虑
application.yml
的顺序,并且值
lib.test.name
会被配置文件特定的属性覆盖。

考虑引入新的配置文件,例如

application-app.yml

lib:
   test:
     name: App

最后,将

default
配置文件替换为
app
配置文件:
lib, { other profiles }, app, local

© www.soinside.com 2019 - 2024. All rights reserved.