SpringContextUtil.java
1.14 KB
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
package com.cjs.site.util.lang;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* Spring手动获取Bean
*
* @author tongyufu
*
*/
@Lazy(false)
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.context = applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
public static Object getBean(String beanName) {
return SpringContextUtil.context.getBean(beanName);
}
public static <T> T getBean(Class<T> requiredType) {
return SpringContextUtil.context.getBean(requiredType);
}
public static <T> T getBean(String beanName, Class<T> requiredType) {
return SpringContextUtil.context.getBean(beanName, requiredType);
}
}