我在类路径资源中有一个内部application.yml文件,包含以下字段:
redis:
hostname: localhost
port: 6379
database: 0
password:有一个外部配置文件: config.properties。它定义了要在我的服务器上下文中重写的一些字段。文件config.properties:
redis.hostname = db.example.com
redis.password = my_password由于无法读取配置文件中的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),但未在外部文件中定义?
P.S:我知道发生了什么,因为当我在外部配置文件中添加redis.port=6379属性时,应用程序正常工作。
发布于 2019-01-14 00:12:44
步骤1:阅读弹簧启动文件
配置位置按反向顺序搜索。默认情况下,配置的位置是
classpath:/,classpath:/config/,file:./,file:./config/。得到的搜索顺序如下: 文件:./config/ file:./ classpath:/config/ classpath:/ 当使用spring.config.location配置自定义配置位置时,它们替换默认位置。例如,如果spring.config.location配置为值classpath:/custom-config/,file:./custom-config/,则搜索顺序如下: 文件:./config/classpath:config/
步骤2:指定正确的值:
-Dspring.config.location=classpath:/,file:///home/username/config.properties发布于 2019-01-14 04:26:54
file:和
classpath:在代码中绑定配置时需要指定。当您指定-D参数时,可以传递相对于jar文件位置的属性文件的地址。
https://stackoverflow.com/questions/54174333
复制相似问题