英文:
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<MyPropDecrypted> myPropDecryptedLst = mySpecialAPIPack.decrpyt("config.properties");
// 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("myapp.property"+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.
答案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<String,String > myapp;
public String getproperty1() {
return myapp.get("property1");
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论