-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperTag.java
More file actions
51 lines (42 loc) · 1.13 KB
/
Copy pathSuperTag.java
File metadata and controls
51 lines (42 loc) · 1.13 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
package com.caozj.taglib;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* 父标签 主要是取到Spring的ApplicationContext容器,方便获取Bean引用
*
* @author caozj
*
*/
public class SuperTag extends BodyTagSupport {
private static final long serialVersionUID = -4948632853691953405L;
private ApplicationContext context;
public ApplicationContext getContext() {
return context;
}
@Override
public void setPageContext(PageContext pageContext) {
super.setPageContext(pageContext);
context = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());
}
/**
* 取Bean实例
*
* @param <T>
* @param clazz
* @return
*/
public <T> T getBean(Class<T> clazz) {
return getContext().getBean(clazz);
}
/**
* 取Bean实例
*
* @param beanId
* @return
*/
public Object getBean(String beanId) {
return getContext().getBean(beanId);
}
}