ProfileUtils.java 1.3 KB
Newer Older
武汉红喜's avatar
武汉红喜 已提交
1
package org.hongxi.whatsmars.boot.sample.swagger.common;
S
swagger  
shenhongxi 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by shenhongxi on 2017/8/2.
 */
@Component
public class ProfileUtils implements InitializingBean {

    @Autowired
    private Environment env;

    private static final String PROFILE_PROD = "prod";
    private static final String PROFILE_TEST = "test";
    private static final String PROFILE_DEV = "dev";

    private static List<String> profiles = Collections.unmodifiableList(new ArrayList<String>());

    @Override
    public void afterPropertiesSet() throws Exception {
        profiles = Collections.unmodifiableList(Arrays.asList(env.getActiveProfiles()));
    }

    public static boolean isProd() {
        return profiles.contains(PROFILE_PROD);
    }

    public static boolean isTest() {
        return profiles.contains(PROFILE_TEST);
    }

    public static boolean isDev() {
        return profiles.contains(PROFILE_DEV);
    }

    public static List<String> getProfiles() {
        return profiles;
    }

}