package cn.huge.base.common.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.util.Locale; /** * @title: SpringContextUtil * @description: 获取bean的工具类,可用于在线程里面获取bean * @company: hugeinfo * @author: liyj * @time: 2021-11-05 16:51:48 * @version: 1.0.0 */ @Component public class SpringContextUtil implements ApplicationContextAware { /** * 环境变量 */ public static String DEV = "dev"; public static String TEST = "test"; public static String PROD = "prod"; private static ApplicationContext context = null; // 获取applicationContext @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } // 传入线程中 public static T getBean(String beanName) { return (T) context.getBean(beanName); } // 国际化使用 public static String getMessage(String key) { return context.getMessage(key, null, Locale.getDefault()); } // 获取当前环境 public static String getActiveProfile() { return context.getEnvironment().getActiveProfiles()[0]; } // 判断是否是开发环境 public static Boolean checkDev(){ String activeProfile = getActiveProfile(); if (DEV.equals(activeProfile)){ return true; }else{ return false; } } // 判断是否是开发测试环境 public static Boolean checkDevTest(){ String activeProfile = getActiveProfile(); if (DEV.equals(activeProfile) || TEST.equals(activeProfile)){ return true; }else{ return false; } } // 判断是否是生成环境 public static Boolean checkProd(){ String activeProfile = getActiveProfile(); if (PROD.equals(activeProfile)){ return true; }else{ return false; } } // 判断是否是开发环境 public static Boolean checkReturn(){ String activeProfile = getActiveProfile(); if (DEV.equals(activeProfile) || TEST.equals(activeProfile) || PROD.equals(activeProfile)){ return true; }else{ return false; } } } /** * -------------------_ooOoo_------------------- * ------------------o8888888o------------------ * ------------------88" . "88------------------ * ------------------(| -_- |)------------------ * ------------------O\ = /O------------------ * ---------------____/`---'\____--------------- * -------------.' \\| |// `.------------- * ------------/ \\||| : |||// \------------ * -----------/ _||||| -:- |||||- \----------- * -----------| | \\\ - /// | |----------- * -----------| \_| ''\---/'' | |----------- * -----------\ .-\__ `-` ___/-. /----------- * ---------___`. .' /--.--\ `. . __---------- * ------."" '< `.___\_<|>_/___.' >'"".------- * -----| | : `- \`.;`\ _ /`;.`/ - ` : | |----- * -----\ \ `-. \_ __\ /__ _/ .-` / /----- * ======`-.____`-.___\_____/___.-`____.-'====== * -------------------`=---=' * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * ---------佛祖保佑---hugeinfo---永无BUG---------- */