forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringUtil.java
More file actions
87 lines (78 loc) · 2.41 KB
/
Copy pathSpringUtil.java
File metadata and controls
87 lines (78 loc) · 2.41 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.myframework.util;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* 完成SPRING 的BEAN获取和 SPRING 容器的刷新
* @author Administrator
*
*/
public class SpringUtil {
private static Log log = LogFactory.getLog(SpringUtil.class);
/**
* 取得SpringBean实例
* @param request
* @param beanId
* @return
*/
public static Object getBean(HttpServletRequest request, String beanId) {
return getWebApplicationContext(request).getBean(beanId);
}
/**
* 刷新WEB中SPRING的信息
* @param request
*/
public static void refreshSpring(HttpServletRequest request) {
WebApplicationContext wac = getWebApplicationContext(request);
refreshSpring(wac);
}
/**
* 刷新SPRING的信息
* @param aContext
*/
public static void refreshSpring(ApplicationContext aContext) {
Class[] cls = ClassUtils.getAllInterfaces(aContext);
for (Class item : cls) {
if (ConfigurableApplicationContext.class.getName().equals(
item.getName())) {
log.debug("refresh Spring start ");
long start = System.currentTimeMillis();
((ConfigurableApplicationContext) aContext).refresh();
log.debug("refresh Spring end ; cost time (ms) :"
+ (System.currentTimeMillis() - start));
}
}
}
/**
* 取得WebApplicationContext
* @param sc
* @return
*/
public static WebApplicationContext getWebApplicationContext(
ServletContext sc) {
// 通过web.xml 配置 org.springframework.web.context.ContextLoaderListener 加载。
WebApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(sc);
if (ctx == null) {
throw new RuntimeException(
"WebApplicationContext error ........ Can't find [WebApplicationContext] in any scope !");
}
return ctx;
}
/**
* 取得WebApplicationContext
* @param request
* @return
*/
public static WebApplicationContext getWebApplicationContext(
HttpServletRequest request) {
ServletContext sc = request.getSession().getServletContext();
return getWebApplicationContext(sc);
}
}