繁体   English   中英

Spring命令行中的多个外部属性文件

[英]Multiple external property file in spring boot from command line

我想从外部位置读取2个属性文件。 它需要通过命令行参数加载。

  1. configuration.properties - >这是一个普通的属性文件,包含公共信息。

  2. secure.properties - >这包含加密密码。

我给出以下命令行参数

-Dspring.config.location=file:C:\Project\properties\configuration.properties, file:C:\Project\properties\security\dev\secure.properties

configuration.properties的属性工作正常。 因为我不是直接加载文件,而是使用属性。

在这里,对于这个加密文件,我需要传递路径并明确加载,这不是发生的。

此外,util EncryptedPropertiesReader在jar中可用,因此无法修改它。

这里,我需要使用secure.properties

我们有一个类EncryptedPropertiesReader 它包含一个load(string path)方法。

因此,我需要传递secure.properties文件的路径。

String env = "dev" 
Properties p = EncryptedPropertiesReader.load("security\" + env + "\secure.properties");
System.out.println(p); // null

在这里,我不能给出绝对路径,因为在不同的环境(Unix)中,我会有不同的路径。

因此命令行是我的选择,需要保持外部属性。

这是我试过的组合:


  • 命令行 :

    1. 为安全提供classpath。 属性

       -Dspring.config.location=file:C:\\Project\\properties\\configuration.properties, classpath:C:\\Project\\properties\\security\\dev\\ 
    2. spring.config.nane

       -D spring.config.name=configuration, secure - Dspring.config.location=file:C:\\Project\\properties\\configuration.properties, file:C:\\Project\\properties\\security\\dev\\secure.properties 
    3. 试图改变\\到/


  • 网址的路径通过

    1. ("secure.properties"); //当我加载类路径时,期望工作

    2. ("/secure.properties"); //当我加载类路径时,期望工作


上述组合均无效。

知道出了什么问题吗? 或者我错过了什么?

为长期问题道歉。

24.3应用程序属性文件

SpringApplication从以下位置的application.properties文件加载属性,并将它们添加到Spring环境中:

  1. 当前目录的A / config子目录
  2. 当前目录
  3. 一个classpath / config包
  4. 类路径根

列表按优先级排序(在列表中较高位置定义的属性将覆盖在较低位置中定义的属性)。

[注意]您也可以使用YAML('.yml')文件替代'.properties'。

如果您不喜欢application.properties作为配置文件名,则可以通过指定spring.config.name环境属性来切换到另一个文件名。 您还可以使用spring.config.location环境属性(以逗号分隔的目录位置或文件路径列表)来引用显式位置。

以下示例显示如何指定其他文件名:

$ java -jar myproject.jar --spring.config.name=myproject

以下示例显示如何指定两个位置:

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

[警告] spring.config.name和spring.config.location很早就用来确定哪些文件必须加载,因此必须将它们定义为环境属性(通常是OS环境变量,系统属性或命令) -line参数)。

如果spring.config.location包含目录(而不是文件),则它们应该以/结尾(并且在运行时,在加载之前附加从spring.config.name生成的名称,包括特定于配置文件的文件名)。 spring.config.location中指定的文件按原样使用,不支持特定于配置文件的变体,并且被任何特定于配置文件的属性覆盖。

以相反的顺序搜索配置位置。 默认情况下,配置的位置是

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

生成的搜索顺序如下:

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

使用spring.config.location配置自定义配置位置时,它们将替换默认位置。 例如,如果spring.config.location配置了值classpath:/ custom-config /,file:./ custom-config /,搜索顺序将变为以下内容:

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

或者,当使用spring.config.additional-location配置自定义配置位置时,除默认位置外,还会使用它们。 在默认位置之前搜索其他位置。 例如,如果配置了classpath:/ custom-config /,file:./ custom-config /的其他位置,则搜索顺序将变为以下内容:

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

此搜索顺序允许您在一个配置文件中指定默认值,然后有选择地覆盖另一个配置文件中的值。 您可以在其中一个默认位置的application.properties(或使用spring.config.name选择的任何其他基本名称)中为应用程序提供默认值。 然后,可以在运行时使用位于其中一个自定义位置的不同文件覆盖这些默认值。

[Note]如果使用环境变量而不是系统属性,大多数操作系统都不允许使用句点分隔的键名,但您可以使用下划线(例如,使用SPRING_CONFIG_NAME而不是spring.config.name)。

[Note]如果应用程序在容器中运行,则可以使用JNDI属性(在java:comp / env中)或servlet上下文初始化参数来代替环境变量或系统属性。

这就是你可以使用环境变量从任何位置加载属性的方法

-Dspring.config.location="C:\Project\properties\", -Dsecure.properties.location="C:\Project\properties\security\dev\"




 @PropertySources({ 
             @PropertySource("file:${spring.config.location}/configuration.properties"),
             @PropertySource("file:${secure.properties.location}/secure.properties")})

暂无
暂无

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

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