提交 | 用户 | 时间
|
e4c3b0
|
1 |
package com.jcdm.main.da.opcuaconfig.init; |
Y |
2 |
|
|
3 |
import org.springframework.beans.BeansException; |
|
4 |
import org.springframework.context.ApplicationContext; |
|
5 |
import org.springframework.context.ApplicationContextAware; |
|
6 |
import org.springframework.stereotype.Component; |
|
7 |
|
|
8 |
@Component |
|
9 |
public class BeanUtils implements ApplicationContextAware { |
|
10 |
protected static ApplicationContext applicationContext ; |
|
11 |
|
|
12 |
@Override |
|
13 |
public void setApplicationContext(ApplicationContext arg0) throws BeansException { |
|
14 |
if (applicationContext == null) { |
|
15 |
applicationContext = arg0; |
|
16 |
} |
|
17 |
|
|
18 |
} |
|
19 |
public static Object getBean(String name) { |
|
20 |
//name表示其他要注入的注解name名 |
|
21 |
return applicationContext.getBean(name); |
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象 |
|
26 |
*/ |
|
27 |
public static <T> T getBean(Class<T> clazz) { |
|
28 |
return applicationContext.getBean(clazz); |
|
29 |
} |
|
30 |
} |