gpt4 book ai didi

java - 从命令行启动Spring Boot中的多个外部属性文件

转载 作者:行者123 更新时间:2023-12-01 01:44:32 25 4
gpt4 key购买 nike

我想从外部位置读取 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 的属性工作正常。因为我不是直接加载文件,而是使用该属性。

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

    还有实用程序 EncryptedPropertiesReader在 jar 中可用,因此无法修改它。

    在这里,问题是我需要如何使用secure.properties

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

    因此我需要通过 secure.properties 的路径文件。

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

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

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

    这是我尝试过的组合:

  • 命令行:
  • 为安全提供类路径。特性
    -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");//当我加载类路径
  • 时预期工作
  • ("/secure.properties");//当我加载类路径
  • 时预期工作


    None of the above combination worked.

    Any idea what's going wrong? Or what am i missing out?

    Apologies for the long question.

    最佳答案

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

    -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")})

    关于java - 从命令行启动Spring Boot中的多个外部属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52405347/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com