简体   繁体   English

子应用程序中的application.properties文件不会覆盖主应用程序中的application.properties文件

[英]application.properties file from child application not override application.properties file in main application

I am new to spring boot,I have a confusion that , when all the property source in spring application like application.properties , my.properties,application-{profile}.properties goes into Environment, then why don't they get override. 我是Spring Boot的新手,我感到困惑,当Spring应用程序中的所有属性源(例如application.properties,my.properties,application- {profile} .properties)进入Environment时,为什么不覆盖它们。 if there are some common key in two different properties file. 如果两个不同的属性文件中有一些公用密钥。

eg, If I create one child project and have one application.properties in it and having propety let say 'name=child' and create the jar of this project and add this jar to my main project and main project also having an application.properties file with same key but different value, let say 'name=parent' if I use Envionment class in spring and get env.getProperty(key) then it is showing parent. 例如,如果我创建一个子项目并在其中包含一个application.properties并具有吸引力,则说“ name = child”并创建该项目的jar,并将此jar添加到我的主项目中,并且该主项目也具有application.properties具有相同密钥但值不同的文件,如果我在春季使用Envionment类并获取env.getProperty(key),则说“ name = parent”,那么它将显示父级。 Acc to me it should be 'child' because if all property are going to same place then two same key with different value cannot exist. 对我来说,它应该是“子代”,因为如果所有属性都将移到同一位置,那么就不会存在两个具有不同值的相同键。

And also I have confusion of Ordering of properties file. 而且我对属性文件的排序感到困惑。 Does it mean if it get property in first file then it will not got to second file to search for that propety? 这是否意味着如果它在第一个文件中获得属性,那么就不会在第二个文件中搜索该属性?

Spring boot has an order for evaluating all property sources. Spring Boot具有评估所有属性源的顺序。 They are listed here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 它们在此处列出: https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

So basically, Spring knows about all the properties it needs to source. 因此,基本上,Spring知道它需要获取的所有属性。 It will then evaluate where to get them. 然后它将评估从何处获得它们。 In your case, it evaluates your {profile}.properties file first. 对于您而言,它将首先评估您的{profile} .properties文件。 If it can't find what it needs, then it will go into your regular application.properties. 如果找不到所需的内容,它将进入常规的application.properties。 So if you are expecting your application.properties values to overwrite the {profile} one, it's not going to. 因此,如果您希望application.properties值覆盖{profile}的值,则不会。

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

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