原文出处: xieyu_zyweb
为啥写这个文章呢?spring各个版本不一样,以及和系统框架套在一块儿不一样,致使获取的方式不一样,网络上各类版本,太乱了,写获取方式的人都不写这个获取方式是在本地仍是在WEB,在那种应用服务器下,在spring那个版本下,太过度了!spring
我这写一些,常见的,可能常常要用的版本;数据库
首先了解,为何要获取这个东西:当你想经过spring获取一个你指定的类的实例的时候,而又没有经过spring加载到当前调用的类里面,例如你在filter里面,可能要对人员角色作断定,此时还没到业务层代码,可是又要访问数据库或其余的服务类。编程
而后再确保一点:这个context是一个全局变量,spring加载的时候,根handle信息就被装载,不管是本地应用程序仍是web应用都是这样,下面分别说下若是是本地程序和其余状况的获取方式。tomcat
若是是main方法,你要启动spring,有不少方法,有基于annotation的注解来说配置文件装载起来,固然,你想获取applicationCntext可在main方法中这样获取:服务器
1
|
XmlBeanFactory factory =
new
XmlBeanFactory(
new
ClassPathResource(
"beans.xml"
));
//这样来加载配置文件
|
还有没有其余的方式呢?有的网络
1
|
ApplicationContext context =
new
ClassPathXmlApplicationContext(
new
String[] {
"a.xml"
,
"b.xml"
});
|
还有没有其余的?有session
1
2
3
4
5
6
|
XmlWebApplicationContext context =
new
XmlWebApplicationContext();
context.setConfigLocations(
new
String[] {
"aaa.xml"
,
"bb.xml"
});
MockServletContext msc =
new
MockServletContext();
context.setServletContext(msc);
context.refresh();
msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
|
其实方法差很少,他们有着继承关系,因此方法不少,你每次new的时候,至关于从新建立一个applicationContext,他会从新装载,因此不适合反复调用,若是本身new,你就应当把它放到一个全局变量中,用main启动的,固然你经过直接或间接的static应用到这个application便可。架构
而在WEB上呢,有一种是经过spring来加载spring自己的方式是:app
经过实现接口:
1
|
org.springframework.context.ApplicationContextAware
|
而后spring反射,来源文章:http://blog.163.com/xuyang1974@126/blog/static/2684016320101028101923914/
这种方式适在spring 二、3当中均有效:
编写类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
org.springframework.beans.BeansException;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.ApplicationContextAware;
import
org.springframework.stereotype.Service;
public
class
SpringContextHolder
implements
ApplicationContextAware {
private
static
ApplicationContext applicationContext;
@Override
public
void
setApplicationContext(ApplicationContext applicationContext)
throws
BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
public
static
ApplicationContext getApplicationContext() {
return
applicationContext;
}
public
static
Object getBean(String beanName) {
return
applicationContext.getBean(beanName);
}
public
static
<T>T getBean(String beanName , Class<T>clazz) {
return
applicationContext.getBean(beanName , clazz);
}
}
|
我这里是经过annotation注解的,若是不是annotation,那么能够经过配置文件:
1
|
<
bean
class
=
"xxx.xxx.xxx.SpringContextHolder"
></
bean
>
|
来进行注入操做,结果同样,若是的spring配置中,没有设置byName的话,bean的配置里面记得要加参数来设置applicationContext来反射进去。
而你要加载spring,不少时候,并非进入业务层的,由于反射是反射到业务层的,你尚未进入业务层,怎么来获取这个反射的东西呢?除非你反射的时候,用static变量来获取,那么就没有问题了;因此上面的例子中他也用的是static;
当你不想用static来反射,而常常想要用到它的时候,就有不少种获取方式了。
在spring 3之前的版本,咱们在WEB应用中一般是这样获取的:
1
|
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(context);
|
而contexnt是什么呢?若是是servlet中,是能够直接经过getServletContext()获取,
而经过request要这样获取:
对于全部的tomcat通用的写法是:
1
|
ServletContext context = req.getSession().getServletContext();
|
对于tomcat 7以上的写法是(也就是tomcat 7能够直接从request中获取servletContext,tomcat6不行,必须经过session才能够):
1
|
ServletContext context = req.getServletContext();
|
其实从spring 3事后,获取的方法就有所改变,变得很诡异,由于居然不兼容之前的获取方法,spring 3当中将其进行了进一步的包装,你在其余地方可能看到各类各样的版本。
spring 2中之因此能够那样获取,是由于spring 2当中一般会配置一个listener,由他来加载spring,他在filter以前;spring 3当中,经过org.springframework.web.servlet.DispatcherServlet来装载spring的信息,初始化在其父亲类:org.springframework.web.servlet.FrameworkServlet中方法:initWebApplicationContext();
跟踪方法明显看到内部获取增长了一个参数:
1
|
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext(),attrName);
|
这个参数是什么呢?
通过跟踪能够发现是:
1
|
FrameworkServlet.SERVLET_CONTEXT_PREFIX + getServletName()
|
而SERVLET_CONTEXT_PREFIX的定义是:
1
|
public
static
final
String SERVLET_CONTEXT_PREFIX = FrameworkServlet.
class
.getName() +
".CONTEXT."
;
|
也就是:
1
|
“org.springframework.web.servlet.FrameworkServlet.CONTEXT.”
|
而getServletName()呢?他是当前请求的servlet,能够获取到的一个web.xml里面配置的名称,例如,
若是你的web.xml中配置的是:
1
2
3
4
5
|
<
servlet
>
<
servlet-name
>spring</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
|
说明getServletName()的结果就是spring,不然就是其余,那么若是是spring,就是:
1
|
org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring
|
ok,若是按照上面的配置,获取方式就是:
1
|
request.getSession().getServletContext().getAttribute(
"org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring"
);
|
tomcat 7以上能够写成:
1
|
request.getServletContext().getAttribute(
"org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring"
);
|
更为好的写法是:
1
|
request.getSession().getServletContext().getAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX +"spring");
|
如下为spring为了方便,作的一些扩展:
spring为了业务代码中获取这个参数方便,在进入业务代码前作了一个操做,在DispatcherServlet的方法:doService中doDispatch调用以前:
1
|
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
|
也就是,当你进入Controller之后,获取就不用那么麻烦了,你只须要这样就能获取到:
1
|
request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
固然,你能够将值写进去,看定义是:
1
|
public
static
final
String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.
class
.getName() +
".CONTEXT"
;
|
那么值就应该是:
1
|
org.springframework.web.servlet.DispatcherServlet.CONTEXT
|
因此在Controller中你还能够这样来获取:
1
|
request.getAttribute(
"org.springframework.web.servlet.DispatcherServlet.CONTEXT"
)
|
通过spring包装后,你也能够经过:
1
|
RequestContextUtils.getWebApplicationContext(request , context)
|
来获取,源码以下:
其实它获取的方式和上面给的方法是同样的,RequestContextUtils.getWebApplicationContext在spring 3当中,若是没有启动ContextLoaderListener(固然你能够配置监听),是不会成功的。
ContextLoaderListener的简单配置为(web.xml中):
1
2
3
|
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
|
spring 3之后基本不这样配置了。
问啊-定制化IT教育平台,牛人一对一服务,有问必答,开发编程社交头条 官方网站:www.wenaaa.com
QQ群290551701 汇集不少互联网精英,技术总监,架构师,项目经理!开源技术研究,欢迎业内人士,大牛及新手有志于从事IT行业人员进入!