为什么剂量弹簧配置名称不起作用?
如果仅设置--spring.config.location=classpath:test/
,它将找到application.properties
;如果仅设置--spring.config.name=override.properties
,它将找到根路径下的override.properties。
但是,如果我同时设置了--spring.config.location=classpath:test/
和--spring.config.name=override.properties
,则无法在/test
下找到override.properties。为什么?
答案 0 :(得分:0)
您可以在根路径下找到override.properties
,因为根路径是spring搜索的默认位置之一。
但是,指定spring.config.location
意味着您指定的位置将替换搜索到的默认位置集(而不是添加到这些位置)-因此,在您的情况下,spring现在只会在classpath:test
目录。
如果要在搜索中保留默认位置,请尝试使用spring.config.additional-location
而不是spring.config.location
-或将/test
添加到spring.config.location
中。
来自https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html处的文档:
以相反的顺序搜索配置位置。默认情况下,配置的位置是
classpath:/,
classpath:/ config /,
文件:./,
文件:./ config/。
结果搜索顺序如下:
文件:./ config /文件:./类路径:/ config /类路径:/
使用spring.config.location配置自定义配置位置时,它们 替换默认位置。例如,如果spring.config.location 配置了值
classpath:/ custom-config /, 文件:./ custom-config /,
搜索顺序如下:
文件:./ custom-config /类路径:custom-config /
或者,当使用来配置自定义配置位置时 spring.config.additional-location,除了 默认位置。在搜索其他位置之前, 默认位置。