springboot获取profile的方法

阅读 73

2022-04-26

通过代码获取profile

@Component
public class ProfileUtils implements ApplicationContextAware {

private static ApplicationContext context = null;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.context = applicationContext;
}

public static String getActiveProfile(){
    String[] profiles = context.getEnvironment().getActiveProfiles();
    if(profiles.length != 0){
        return profiles[0];
    }
    return "";
}
}

通过注解的方式来获取Profile

@Profile("dev","test")
//下面的配置信息只有在dev环境和test环境会生效
@Service

精彩评论(0)

0 0 举报