广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
1
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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> 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----------
 */