如何将 属性 源分配给 micronaut 中的特定环境?
How do I assign a property source to a certain environment in micronaut?
我正在通过我的应用程序 class 添加 属性 值,就像这样
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}
例如
我正在通过我的应用程序 class 添加 属性 值,就像这样
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}
例如