派生自 projectDept/qhighschool

Administrator
2022-11-29 8c99e2d8b6c1e0d9cde6abbe80b4df75be19f6d1
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;
    }
}