luxiaotao1123
2024-06-21 569d2b6bc754b037ade5b4205a4d1cdb8339b26b
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
package com.zy.asrs.framework.common;
 
import com.zy.asrs.framework.exception.CoolException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class SpringUtils implements ApplicationContextAware {
 
    private static ApplicationContext application;
 
    public SpringUtils() {}
 
    public static void init(ApplicationContext context) {
        SpringUtils.application = context;
    }
 
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringUtils.application = context;
    }
 
    public static ApplicationContext getApplicationContext() {
        if(application==null) {
            throw new CoolException(BaseRes.ERROR);
        }
        return application;
    }
 
    public static <T>T getBean(Class<T> prototype) {
        return getApplicationContext().getBean(prototype);
    }
 
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
 
}