spring - Spring 配置文件:拒绝用户运行来自同一“组”的许多配置文件
问题描述
我想调节配置文件。例如,我有两组配置文件:a)DEV、PROD、TEST b)ProfileDB1、ProfileDB2、ProfileDB3
我想强制该应用程序将使用第一组的一个配置文件和第二组的一个配置文件运行。但没有了。可能吗 ?
解决方案
您可以编写某种 ActiveProfilesVerifier 组件,在其中Environment
注入并验证活动配置文件:
@Component
public class ActiveProfilesVerifier {
private static final List<String> ENV_PROFILES = Arrays.asList("DEV", "PROD", "TEST");
private static final List<String> DBASE_PROFILES = Arrays.asList("ProfileDB1", "ProfileDB2", "ProfileDB3");
private final Environment environment;
public ActiveProfilesVerifier(Environment environment) {
this.environment = environment;
}
@PostConstruct
public void verifyProfiles() {
String[] activeProfiles = environment.getActiveProfiles();
boolean hasSingleEnvProfile = Arrays.stream(activeProfiles).filter(ENV_PROFILES::contains).count() == 1;
if (!hasSingleEnvProfile) {
throw new IllegalArgumentException("Select exactly one environment profile");
}
boolean hasSingleDbaseProfile = Arrays.stream(activeProfiles).filter(DBASE_PROFILES::contains).count() == 1;
if (!hasSingleDbaseProfile) {
throw new IllegalArgumentException("Select exactly one database profile");
}
}
}
推荐阅读
- c - 下面程序 556 的输出如何?
- android - 如何知道一个动作是全局完成的
- javascript - WebGL 画布背景
- apache-spark - 使用 spark.master.rest.enabled=true 的 spark 独立集群上的 Spark 身份验证和加密
- c# - 无法确定 1 到 0..1 关系中的类型之间关联的主体端
- javascript - 如何对这个 Wordpress PHP 函数进行代理调用,一次没有参数,另一次有参数?
- python - 当我运行我的代码时会弹出一个警告。我不知道如何解决
- python - 如何使用 get_pty=False 将 Ctrl+C 发送到 Python Paramiko 客户端?
- javascript - 在小屏幕上多行拆分工具栏标题
- r - R:异步并行 lapply