@Profile使用及SpringBoot获取profile值

参考了上面的链接中的代码,但上面的代码有一个错误(判断profiles是否为空),修改后如下:

@Component
public class ProfileUtil implements ApplicationContextAware {

    private static ApplicationContext context = null;

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

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