spring 中的多个外部 属性 文件从命令行启动
Multiple external property file in spring boot from command line
我想从外部位置读取 2 个属性文件。
它需要通过命令行参数加载。
configuration.properties -> 这是一个普通的 属性 文件,包含 public 信息。
secure.properties -> 这包含加密密码。
我给出以下命令行参数
-Dspring.config.location=file:C:\Project\properties\configuration.properties, file:C:\Project\properties\security\dev\secure.properties
configuration.properties
的属性工作正常。因为我不是直接加载文件,而是使用 属性.
在这里,对于这个加密文件,我需要明确传递路径并加载,这不是 Happening。
此外,util EncryptedPropertiesReader
在 jar 中可用,因此无法修改它。
在这里,问题是我需要如何使用 secure.properties
我们有一个 class EncryptedPropertiesReader
。
它包含一个 load(string path)
方法。
因此我需要传递 secure.properties
文件的路径。
即
String env = "dev"
Properties p = EncryptedPropertiesReader.load("security\" + env + "\secure.properties");
System.out.println(p); // null
在这里,我不能给出绝对路径,因为在不同的环境(Unix)中,我会有不同的路径。
因此命令行是我的选择并且也需要将属性保持在外部。
这是我试过的组合:
命令行:
为安全提供class路径。属性
-Dspring.config.location=file:C:\Project\properties\configuration.properties, classpath:C:\Project\properties\security\dev\
给予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
尝试将 \ 更改为 /
url 的路径已通过
("secure.properties");
//当我加载 class path
时期望工作
("/secure.properties");
//当我加载 class path
时期望工作
None of the above combination worked.
Any idea what's going wrong? Or what am i missing out?
Apologies for the long question.
24.3 应用程序 属性 文件
Spring应用程序从以下位置的 application.properties 文件加载属性并将它们添加到 Spring 环境:
- 当前目录的/config 子目录
- 当前目录
- 类路径/配置包
- 类路径根目录
列表按优先级排序(在列表较高位置定义的属性会覆盖较低位置定义的属性)。
[注意]您还可以使用 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 环境变量、系统 属性 或命令行参数)。
如果 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 选择的任何其他基本名称)中为您的应用程序提供默认值。然后可以在运行时使用位于自定义位置之一的不同文件覆盖这些默认值。
[注意] 如果您使用环境变量而不是系统属性,大多数操作系统不允许使用句点分隔的键名,但您可以使用下划线代替(例如,SPRING_CONFIG_NAME 而不是 spring.config.name ).
[注意] 如果您的应用程序在容器中运行,则可以使用 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")})
我想从外部位置读取 2 个属性文件。 它需要通过命令行参数加载。
configuration.properties -> 这是一个普通的 属性 文件,包含 public 信息。
secure.properties -> 这包含加密密码。
我给出以下命令行参数
-Dspring.config.location=file:C:\Project\properties\configuration.properties, file:C:\Project\properties\security\dev\secure.properties
configuration.properties
的属性工作正常。因为我不是直接加载文件,而是使用 属性.
在这里,对于这个加密文件,我需要明确传递路径并加载,这不是 Happening。
此外,util EncryptedPropertiesReader
在 jar 中可用,因此无法修改它。
在这里,问题是我需要如何使用 secure.properties
我们有一个 class EncryptedPropertiesReader
。
它包含一个 load(string path)
方法。
因此我需要传递 secure.properties
文件的路径。
即
String env = "dev"
Properties p = EncryptedPropertiesReader.load("security\" + env + "\secure.properties");
System.out.println(p); // null
在这里,我不能给出绝对路径,因为在不同的环境(Unix)中,我会有不同的路径。
因此命令行是我的选择并且也需要将属性保持在外部。
这是我试过的组合:
命令行:
为安全提供class路径。属性
-Dspring.config.location=file:C:\Project\properties\configuration.properties, classpath:C:\Project\properties\security\dev\
给予
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
尝试将 \ 更改为 /
url 的路径已通过
("secure.properties");
//当我加载 class path 时期望工作
("/secure.properties");
//当我加载 class path 时期望工作
None of the above combination worked.
Any idea what's going wrong? Or what am i missing out?
Apologies for the long question.
24.3 应用程序 属性 文件
Spring应用程序从以下位置的 application.properties 文件加载属性并将它们添加到 Spring 环境:
- 当前目录的/config 子目录
- 当前目录
- 类路径/配置包
- 类路径根目录
列表按优先级排序(在列表较高位置定义的属性会覆盖较低位置定义的属性)。
[注意]您还可以使用 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 环境变量、系统 属性 或命令行参数)。
如果 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 选择的任何其他基本名称)中为您的应用程序提供默认值。然后可以在运行时使用位于自定义位置之一的不同文件覆盖这些默认值。
[注意] 如果您使用环境变量而不是系统属性,大多数操作系统不允许使用句点分隔的键名,但您可以使用下划线代替(例如,SPRING_CONFIG_NAME 而不是 spring.config.name ).
[注意] 如果您的应用程序在容器中运行,则可以使用 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")})