|
@ -1,52 +0,0 @@
|
1
|
|
package com.ai.ipu.server.util;
|
2
|
|
|
3
|
|
import org.slf4j.Logger;
|
4
|
|
import org.slf4j.LoggerFactory;
|
5
|
|
import org.springframework.beans.BeansException;
|
6
|
|
import org.springframework.context.ApplicationContext;
|
7
|
|
import org.springframework.context.ApplicationContextAware;
|
8
|
|
import org.springframework.context.annotation.Lazy;
|
9
|
|
import org.springframework.stereotype.Component;
|
10
|
|
|
11
|
|
@Component
|
12
|
|
@Lazy(false)
|
13
|
|
public class SpringUtil implements ApplicationContextAware {
|
14
|
|
private static final Logger log = LoggerFactory.getLogger(SpringUtil.class);
|
15
|
|
private static ApplicationContext applicationContext;
|
16
|
|
|
17
|
|
private static void initApplicationContext(ApplicationContext applicationContext) {
|
18
|
|
if(SpringUtil.applicationContext == null) {
|
19
|
|
SpringUtil.applicationContext = applicationContext;
|
20
|
|
}
|
21
|
|
}
|
22
|
|
|
23
|
|
@Override
|
24
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
25
|
|
initApplicationContext(applicationContext);
|
26
|
|
log.debug("---------------------------------------------------------------------");
|
27
|
|
log.debug("ApplicationContext配置成功:");
|
28
|
|
log.debug("可以通过调用SpringUtils.getAppContext()获取applicationContext对象");
|
29
|
|
log.debug("applicationContext=" + SpringUtil.applicationContext);
|
30
|
|
log.debug("---------------------------------------------------------------------");
|
31
|
|
}
|
32
|
|
|
33
|
|
//获取applicationContext
|
34
|
|
public static ApplicationContext getApplicationContext() {
|
35
|
|
return applicationContext;
|
36
|
|
}
|
37
|
|
|
38
|
|
//通过name获取 Bean.
|
39
|
|
public static Object getBean(String name){
|
40
|
|
return getApplicationContext().getBean(name);
|
41
|
|
}
|
42
|
|
|
43
|
|
//通过class获取Bean.
|
44
|
|
public static <T> T getBean(Class<T> clazz){
|
45
|
|
return getApplicationContext().getBean(clazz);
|
46
|
|
}
|
47
|
|
|
48
|
|
//通过name,以及Clazz返回指定的Bean
|
49
|
|
public static <T> T getBean(String name,Class<T> clazz){
|
50
|
|
return getApplicationContext().getBean(name, clazz);
|
51
|
|
}
|
52
|
|
}
|