-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathSpringContext.java
More file actions
70 lines (64 loc) · 1.56 KB
/
SpringContext.java
File metadata and controls
70 lines (64 loc) · 1.56 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.kevinblandy.simple.webchat.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContext implements ApplicationContextAware{
private static final Logger LOGGER = LoggerFactory.getLogger(SpringContext.class);
/**
* ApplicationContext
*/
private static ApplicationContext applicationContext;
/**
* Spring调用注入
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
LOGGER.info("IOC容器注入:{}",applicationContext);
SpringContext.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
/**
*
*/
return SpringContext.applicationContext;
}
/**
* 根据类类型获取实例
* @param clazz
* @return
*/
public static <T> T getBean(Class<T> clazz){
try{
return getApplicationContext().getBean(clazz);
}catch(Exception e){
return null;
}
}
/**
* 根据bean的id和类类型获取实例
* @param clazz
* @param name
* @return
*/
public static <T> T getBean(Class<T> clazz,String name){
try{
return getApplicationContext().getBean(clazz, name);
}catch(Exception e){
return null;
}
}
/**
* 根据bean的id获取实例
* @param name
* @return
*/
public static Object getBean(String name){
try{
return getApplicationContext().getBean(name);
}catch(Exception e){
return null;
}
}
}