简体   繁体   English

Spring没有正确地从外部配置文件加载属性

[英]Spring not loading properties from external config file properly

I have an internal application.yml file located in the classpath resources with the following fields: 我有一个内部application.yml文件位于classpath资源中,包含以下字段:

redis:
  hostname: localhost
  port: 6379
  database: 0
  password:

There is an external config file: config.properties . 有一个外部配置文件: config.properties It defines some fields to be overridden in my server context. 它定义了一些要在我的服务器上下文中重写的字段。 File config.properties : 文件config.properties

redis.hostname = db.example.com
redis.password = my_password

The application fails to start because it is not able to read the redis.port property in the config file. 应用程序无法启动,因为它无法读取配置文件中的redis.port属性。 My doubt is that spring is not retaining fields of a property source( redis ) completely if it has already found some fields defined in the external file(hostname, password in this case). 我怀疑的是,如果已经发现外部文件中定义了一些字段(在这种情况下是主机名,密码),则spring不会完全保留属性源( redis )的字段。

I am running the application using the following command: 我正在使用以下命令运行应用程序:

java -jar -Dspring.config.location=file:///home/username/config.properties application.jar

How do I make spring properly override the internal configuration file so that it only overrides the extra properties(redis.hostname, redis.password) but still retain the other fields defined in the internal file(like redis.port, redis.database) but not defined in the external file? 如何使spring正确覆盖内部配置文件,以便它只覆盖额外的属性(redis.hostname,redis.password),但仍保留内部文件中定义的其他字段(如redis.port,redis.database)但没有在外部文件中定义?

PS: I know this is what is happening because when I add the redis.port=6379 property in the external config file, the applications works properly. PS:我知道这就是发生的事情,因为当我在外部配置文件中添加redis.port = 6379属性时,应用程序正常工作。

Step 1: Read the Spring Boot documentation : 第1步:阅读Spring Boot文档

Config locations are searched in reverse order. 以相反的顺序搜索配置位置。 By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/ . 默认情况下,配置的位置是classpath:/,classpath:/config/,file:./,file:./config/ The resulting search order is the following: 生成的搜索顺序如下:

 file:./config/ file:./ classpath:/config/ classpath:/ 

When custom config locations are configured by using spring.config.location , they replace the default locations . 使用spring.config.location配置自定义配置位置时,它们将替换默认位置 For example, if spring.config.location is configured with the value classpath:/custom-config/,file:./custom-config/ , the search order becomes the following: 例如,如果spring.config.location配置了值classpath:/custom-config/,file:./custom-config/ spring.config.location classpath:/custom-config/,file:./custom-config/ ,搜索顺序将变为以下内容:

 file:./custom-config/ classpath:custom-config/ 

Step 2: Specify the correct value: 第2步:指定正确的值:

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

and

classpath:

need to be specified when you are binding the configuration in your code. 在代码中绑定配置时需要指定。 When you are specifying the -D parameter, you could pass address of the property file relative to the jar file location. 指定-D参数时,可以相对于jar文件位置传递属性文件的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM