Java Spring未正确从外部配置文件加载属性

Java Spring未正确从外部配置文件加载属性,java,spring-boot,Java,Spring Boot,我在类路径资源中有一个内部application.yml文件,其中包含以下字段: redis: hostname: localhost port: 6379 database: 0 password: 有一个外部配置文件:config.properties。它定义了一些要在我的服务器上下文中重写的字段。文件config.properties: 应用程序无法启动,因为它无法读取配置文件中的redis.port属性。我的疑问是,如果spring已经找到了外部文件中定义的一些字段(在

我在类路径资源中有一个内部application.yml文件,其中包含以下字段:

redis:
  hostname: localhost
  port: 6379
  database: 0
  password:
有一个外部配置文件:config.properties。它定义了一些要在我的服务器上下文中重写的字段。文件config.properties:

应用程序无法启动,因为它无法读取配置文件中的redis.port属性。我的疑问是,如果spring已经找到了外部文件中定义的一些字段(在本例中是主机名、密码),那么它并没有完全保留属性源(redis)的字段

我正在使用以下命令运行应用程序:

java -jar -Dspring.config.location=file:///home/username/config.properties application.jar
如何使spring正确覆盖内部配置文件,使其仅覆盖额外属性(redis.hostname、redis.password),但仍保留内部文件中定义但未在外部文件中定义的其他字段(如redis.port、redis.database)

附言:我知道这是因为当我在外部配置文件中添加redis.port=6379属性时,应用程序工作正常。

步骤1:阅读:

配置位置按相反顺序搜索。默认情况下,配置的位置是
classpath:/,classpath:/config/,file:./,file:./config/
。结果搜索顺序如下所示:

file:./config/
file:./
classpath:/config/
classpath:/
file:./custom-config/
classpath:custom-config/
使用
spring.config.location
配置自定义配置位置时,它们将替换默认位置。例如,如果
spring.config.location
配置了值
classpath:/custom config/,file:./custom config/
,则搜索顺序如下:

file:./config/
file:./
classpath:/config/
classpath:/
file:./custom-config/
classpath:custom-config/
步骤2:指定正确的值:

-Dspring.config.location=classpath:/,file:///home/username/config.properties


在代码中绑定配置时需要指定。指定-D参数时,可以传递属性文件相对于jar文件位置的地址。

应用程序似乎可以正确启动,设置了跟踪日志记录级别,从生成的大量日志中可以看出,这很奇怪,因为默认值总是DEBUG,我在internal application.yml文件中将其显式设置为INFO。所以我不确定它在我的情况下是否有效。SpringBoot1.x和2.x有不同的行为。Spring boot 1.x未替换“默认位置”
classpath: