如何在运行时向Spring Boot应用程序上下文添加属性

huangapple go评论84阅读模式
英文:

How to add properties to a spring boot application context on run time

问题

Here is the translated portion of your text:

我有一个Spring Boot应用程序和一个名为**config.properties**的属性文件,其中的值是加密的(请参见下面的示例)

config.properties

myapp.property1={5AES123}SafeR70/wqqwwqwqwqwqsdaWQmNs+O2afeIU/1MHoCWvTgxUYA30C/rrei4\=
myapp.property2={5AES342}MareV70/PLNqsasasaa*ksueoHH+O2afeIU/1MHoCWvTgxUYJQ30C/7rei4\=
myapp.property3={5AES111}TutoV10/xdtghshI5CVULQ7uevr+O2afeIU/1MHoCWvTgxUYJQ30C/1rei4\=

我正在使用一个特殊的API(作为我的应用程序的POM依赖项添加)来解密这些值。

请查看下面的**伪代码**以更好地解释我的意图和我希望在最终拥有的内容。

public static void main(String[] args) {

  // 1. 使用我的特殊API包解密config.properties的属性值。
  List<MyPropDecrypted> myPropDecryptedLst = mySpecialAPIPack.decrpyt("config.properties");

  // 2. 获取Spring上下文
  myAppSpringContext = getSpringContext();

  //3. 从步骤2中将解密后的属性添加到Spring上下文中。
  int idx = 0;
  for (MyPropDecrypted myPropDecrypted : myPropDecryptedLst){
     idx++;
     myAppSpringContext.setProperty("myapp.property"+idx, myPropDecrypted.getDecrypteValue();
  }

  SpringApplication.run(Application.class, args);
}

If you have any further questions or need assistance with the code, please feel free to ask.

英文:

I have a Spring boot Application and a properties file config.properties where the values are encrypted (please see an example below)

config.properties

myapp.property1={5AES123}SafeR70/wqqwwqwqwqwqsdaWQmNs+O2afeIU/1MHoCWvTgxUYA30C/rrei4\=
myapp.property2={5AES342}MareV70/PLNqsasasaa*ksueoHH+O2afeIU/1MHoCWvTgxUYJQ30C/7rei4\=
myapp.property3={5AES111}TutoV10/xdtghshI5CVULQ7uevr+O2afeIU/1MHoCWvTgxUYJQ30C/1rei4\=

I'm using a special API (added as POM dependency on my app) to decrypt those values.

Please find below a PSEUDOCODE to explain better my intentions and what I wish I have at the end of the day.

public static void main(String[] args) {

  // 1. decrypt the properties values of the config.properties using my special API package.
  List&lt;MyPropDecrypted&gt; myPropDecryptedLst = mySpecialAPIPack.decrpyt(&quot;config.properties&quot;);

  // 2. get the spring context
  myAppSpringContext = getSpringContext();

  //3. add the decrypted properties to the spring context from step 2.
  int idx = 0;
  for (MyPropDecrypted myPropDecrypted : myPropDecryptedLst){
     idx++;
     myAppSpringContext.setProperty(&quot;myapp.property&quot;+idx, myPropDecrypted.getDecrypteValue();
  }

  SpringApplication.run(Application.class, args);
}

My question is how can I programmatically add/inject those decrypted (with my special API) properties to the spring context, so that I can use it like properties loaded from a property file (@Value("${myapp.property1}"))?

答案1

得分: 1

你可以使用Spring Cloud Config并按照这里的说明实现一个自定义的EnvironmentRepository bean:

> 自定义复合环境存储库
除了使用Spring Cloud中的环境存储库之一,您还可以提供自己的EnvironmentRepository bean,以作为复合环境的一部分包含进来。要实现此目标,您的bean必须实现EnvironmentRepository接口。如果您想控制自定义EnvironmentRepository在复合环境中的优先级,还应该实现Ordered接口并覆盖getOrdered方法。如果不实现Ordered接口,您的EnvironmentRepository将具有最低的优先级。

文档

英文:

You could use Spring Cloud Config and implement a custom EnvironmentRepository bean as explained here:

> Custom Composite Environment Repositories
In addition to using one of the environment repositories from Spring Cloud, you can also provide your own EnvironmentRepository bean to be included as part of a composite environment. To do so, your bean must implement the EnvironmentRepository interface. If you want to control the priority of your custom EnvironmentRepository within the composite environment, you should also implement the Ordered interface and override the getOrdered method. If you do not implement the Ordered interface, your EnvironmentRepository is given the lowest priority.

Docs

答案2

得分: 1

我建议您创建一个配置类,类似于以下方式:

@ConfigurationProperties
@Configuration
class MyConfig {
  
  public Map<String, String> myapp;

  public String getProperty1() {
    return myapp.get("property1");
  }

  public String getProperty2() {
    // 
  }

  // 为所有三个属性创建getter和setter方法
}

现在,更新您的伪代码以使用setter方法设置解密后的值。然后,您可以将MyConfig类注入到任何其他类中以获取这些值。

英文:

I would suggest you to create a configuration class like this :

 @ConfigurationProperties
    @Configuration
    class MyConfig{
    
   public Map&lt;String,String &gt; myapp;
    
     public String getproperty1() {
      return myapp.get(&quot;property1&quot;);
    }
    
      public String getProperty2() {
    //
    }
    
    //getters and setter for all the three properties
    
    }

Now update your PSEUDO code to set the decrypted values using the setter methods. You can then inject MyConfig class in any other class and get the values.

huangapple
  • 本文由 发表于 2020年8月14日 00:39:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63399571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定