#
Junjie
2025-07-05 a757961fc5b8f5ee5b79cc30615bd22d321d0d72
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
package com.zy.common;
 
import com.zy.common.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 static void init(ApplicationContext context) {
        application = context;
    }
 
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        application = context;
    }
 
    private static ApplicationContext getApplicationContext() {
        if (application == null) {
            throw new CoolException("500-服务器错误");
        } else {
            return application;
        }
    }
 
    public static <T> T getBean(Class<T> prototype) {
        return (T)getApplicationContext().getBean(prototype);
    }
 
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
}