zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
package com.shlanbao.tzsc.utils.tools;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
/**
 * 
 * @项目名称     :     RDP【快速开发平台】
 * @类名        :    ApplicationContextUtil
 * @类描述    :    TODO 在ApplicationContext环境外获取bean的工具类.
 * @程序员    :    wave_love_snow
 * @版本号    :      V1.0 
 * @日期        :    2014-8-16 上午11:07:48
 */
public class ApplicationContextUtil implements ApplicationContextAware{
    /**
     *  向ApplicationContextUtil里设置ApplicationContext.
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
        ApplicationContextHolder.getInstance().setApplicationContext(applicationContext);
    }
 
 
    /**
     * 
     * @方法名    :    getApplicationContext
     * @功能描述    :      TODO 获得ApplicationContext.
     * @参数        :    @return   
     * @返回类型    :    ApplicationContext   
     * @程序员    :    杨波
     * @日期时间    :    2014-8-16 上午11:08:14
     */
    public static ApplicationContext getApplicationContext() {
        return ApplicationContextHolder.getInstance().getApplicationContext();
    }
    
    /**
     * 
     * @方法名    :    getBean
     * @功能描述    :      TODO 根据class获得bean.
     * @参数        :    @param clazz
     * @参数        :    @return   
     * @返回类型    :    T   
     * @程序员    :    杨波
     * @日期时间    :    2014-8-16 上午11:08:24
     */
    public static <T> T getBean(Class<T> clazz) {
        return ApplicationContextHolder.getInstance().getApplicationContext().getBean(clazz);
    }
    
   /**
    * 
    * @方法名    :    getBean
    * @功能描述    :      TODO 根据id获得bean.
    * @参数        :    @param id
    * @参数        :    @return   
    * @返回类型    :    T   
    * @程序员    :    杨波
    * @日期时间    :    2014-8-16 上午11:08:50
    */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String id) {
        return (T) ApplicationContextHolder.getInstance().getApplicationContext().getBean(id);
    }
   
 
     
     /**
          * 根据提供的bean名称得到对应于指定类型的服务类
          * @param beanId bean的id
          * @param clazz bean的类类型
          * @return 返回的bean类型,若类型不匹配,将抛出异常
          */
     public static <T> T getBean(String beanId, Class<T> clazz) {
         return ApplicationContextHolder.getInstance().getApplicationContext().getBean(beanId, clazz);
     }
}