如何使用属性文件是在导出JAR时将其排除

时间:2021-03-20 13:28:27

I have faced this as an interview question on Spring-boot.

我在Spring-boot上面对这个问题作为面试问题。

The question was like -- If i have a Spring Boot application developed , while exporting it as a JAR file , i excluded properties file and extracted the JAR out of my project , when importing this JAR into other project , and placing that properties file externally in some other path.

问题是 - 如果我开发了一个Spring Boot应用程序,在将其导出为JAR文件时,我将属性文件排除在外,并将JAR从我的项目中提取出来,将此JAR导入到其他项目中,并将该属性文件放在外部在其他一些道路上。

How will the classes of the JAR file imported will read the properties file placed at a new location.?

导入的JAR文件的类将如何读取放置在新位置的属性文件。

I did some search on the above scenario , am not sure what i read is correct or not , but in my opinion it should be done using externalized configuration by @configurationproperties annotation. That's what i found , but it can be totally wrong.

我对上面的场景进行了一些搜索,不确定我读到的内容是否正确,但在我看来应该通过@configurationproperties注释使用外部化配置来完成。这就是我发现的,但它可能是完全错误的。

Please suggest me the answer to this , and if my assumption is correct then also how will i achieve this . Please explain.?

请告诉我这个问题的答案,如果我的假设是正确的,那么我将如何实现这一目标。请解释。?

1 个解决方案

#1


0  

You can effectively use the externalized configuration mode, which can be enabled by specifiying --spring.config.location when lauching the JAR file.

您可以有效地使用外部化配置模式,可以通过在运行JAR文件时指定--spring.config.location来启用。

Example from the official docs :

来自官方文档的示例:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

#1


0  

You can effectively use the externalized configuration mode, which can be enabled by specifiying --spring.config.location when lauching the JAR file.

您可以有效地使用外部化配置模式,可以通过在运行JAR文件时指定--spring.config.location来启用。

Example from the official docs :

来自官方文档的示例:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties