gpt4 book ai didi

java - 如何在 micronaut 中将属性源分配给特定环境?

转载 作者:行者123 更新时间:2023-12-02 09:02:04 25 4
gpt4 key购买 nike

我通过我的应用程序类添加属性值,如下所示

public class Application {

@Loggable
public static void main(String[] args) {

SecretManager secretManager = new SecretManager();

Micronaut.build(null)
.mainClass(Application.class)
.propertySources(PropertySource.of(
"name",
mapOf(
"datasources.default.username", secretManager.getValue(
"DATASOURCES_DEFAULT_USERNAME")
))).start();
}
}

我希望能够通过执行类似的操作来根据环境更改 datasources.default.username 的值。下面的代码不起作用,但是有没有办法做这样的事情?

public class Application {

@Loggable
public static void main(String[] args) {

SecretManager secretManager = new SecretManager();

if(environment == "Dev") {
Micronaut.build(null)
.mainClass(Application.class)
.propertySources(PropertySource.of(
"name",
mapOf(
"datasources.default.username", secretManager.getValue(
"DATASOURCES_DEFAULT_USERNAME")
))).start();
} else {
Micronaut.build(null)
.mainClass(Application.class)
.propertySources(PropertySource.of(
"name",
mapOf(
"datasources.default.username", secretManager.getValue(
"DATASOURCES_CUSTOM_USERNAME")
))).start();
}
}
}


有什么办法可以完成这个任务吗?

最佳答案

您可以创建一个 application-dev.yml 来从环境变量中分配属性

datasources.default.username: ${SOME_ENV} 例如

关于java - 如何在 micronaut 中将属性源分配给特定环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60086951/

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