Java获取当前环境

配置环境

spring.profiles.active=dev

获取当前环境

方法一

通过@Value注解获取

@Value("${spring.profiles.active}")
private String env;

方法二

在配置文件中通过environment判断

@Bean
public Docket docket(Environment environment) {
    System.out.println(Arrays.asList(environment.getActiveProfiles()).contains("dev"));
}

方法三

判断多个环境

@Bean
public Docket docket(Environment environment) {
    Profiles profiles=Profiles.of("dev","test");
    boolean enable=environment.acceptsProfiles(profiles);
}
posted @ 2022-11-03 08:53  Bin_x  阅读(1491)  评论(0编辑  收藏  举报