派生自 projectDept/qhighschool

胡仁荣
2023-08-04 2174b22bbbb45284765a23b8189df59583c65d29
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
/******************************************************************************
 * Copyright (C) 2015 Shenzhen Penguin Network Technology Co., Ltd
 * All Rights Reserved.
 * 本软件为深圳企鹅网络科技有限公司开发研制。未经本公司正式书面同意,其他任何个人、团体
 * 不得使用、复制、修改或发布本软件.
 *****************************************************************************/
 
package com.qxueyou.scc.base.util;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
/**
*
* Spring 工具类,提供获取Sping上下文环境及获取Bean的接口
* @history 2010-09-12 夏德虎 新建
*
*/
@Component
public class SpringUtil implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext;
    
    private SpringUtil(){}
    
    /**
     * 获取bean
     * @param <T> 泛型类型
     * @param name bean name
     * @param clz bean 类型
     * @param context servlet上下文
     * @return bean实例
     */
    public static <T> T getBean(String name,Class<T> clz){
        return applicationContext.getBean(name,clz);
    }
    
    /**
     * 获取bean
     * @param <T> 泛型类型
     * @param clz bean 类型
     * @param context servlet上下文
     * @return bean实例
     */
    public static <T> T getBean(Class<T> clz){
        return applicationContext.getBean(clz);
    }
    
    @SuppressWarnings("unchecked")  
    public static <T> T getBean(String name) throws BeansException {  
        return (T) applicationContext.getBean(name);
    }  
 
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.applicationContext= applicationContext;
    }
}