cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.core.spring;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
/**
 * 获取Spring配置文件中的bean
 * @version V1.0.0
 * @author 杨凯
 * @date Dec 2, 2013 7:28:25 PM
 */
public class SpringBeanUtil implements ApplicationContextAware {
 
 
    private static ApplicationContext staticContext;
    
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        staticContext = context;
    }
    
    /**
     * ȡ������Bean Names
     * @return
     * @version V1.0.0
     * @author yk
     * @date Sep 7, 2013 4:24:28 PM
     */
    public static String[] getSpringBeanNames() {
        return staticContext.getBeanDefinitionNames();
    }
    
 
    /**
     * ͨ��bean��name��ȡbean������
     * @param strBeanName beanName
     * @return
     */
    public static Object getBean(String strBeanName) {
        if (staticContext.containsBean(strBeanName)) {
            return staticContext.getBean(strBeanName);
        }
        return null;
    }
    
    public static <T> T getBean(Class<T> clazz){
        return staticContext.getBean(clazz);
    }
    
}