Spring容器如何创建出来
在我们写ssm整合的时候,其实最让我们难受的地方就是配置文件的使用了。
这里将谈谈spring的容器是如何通过配置文件的方式创建出来的。
一,创建spring容器的程序
<!-- 使用框架提供的ContextLoaderListener监听器来创建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- contextConfigLocation参数用来指定Spring的配置文件的位置,这里classpath:是指当前类路径 -->
<!-- 类路径:在maven编译后生成的target/classes/目录,classes就是类路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
二,spring容器的工作原理
前提:我们先知道Tomcat(先泛泛的代表一下其实是WebApplicationContext)是根据在context-params中配置contextClass和contextConfigLocation完成Spring容器的创建的。
那么想要弄明白Spirng到底如何创建的,就要从这个监听器入手,ContextLoaderListener。
1.ContextLoaderListener的继承关系
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
可以看到ContextLoaderListener继承于ContextLoader类,实现的是ServletContextListener接口。
继承ContextLoader有什么作用?
ContextLoaderListener可以指定在Web应用程序启动时载入Ioc容器,正是通过ContextLoader来实现的,可以说是Ioc容器的初始化工作。
实现ServletContextListener有什么作用?
ServletContextListener接口里的函数会结合Web容器的生命周期被调用。因为ServletContextListener是ServletContext的监听者,如果ServletContext发生变化,会触发相应的事件,而监听器一直对事件监听,如果接收到了变化,就会做出预先设计好的相应动作。由于ServletContext变化而触发的监听器的响应具体包括:在服务器启动时,ServletContext被创建的时候,服务器关闭时,ServletContext将被销毁的时候等,相当于web的生命周期创建与效果的过程。
那么ContextLoaderListener作用是什么?
ContextLoaderListener的作用就是启动Web容器时,读取在contextConfigLocation中定义的xml文件,自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。
简单来说,就是上面这段配置为项目提供了spring支持,初始化了Ioc容器。
那又是怎么为我们的项目提供spring支持的呢?
上面说到“监听器一直对事件监听,如果接收到了变化,就会做出预先设计好的相应动作”。而监听器的响应动作就是在服务器启动时contextInitialized会被调用,关闭的时候contextDestroyed被调用。这里我们关注的是WebApplicationContext如何完成创建。因此销毁方法就暂不讨论。
2.WebApplicationContext的销毁
@Override
public void contextInitialized(ServletContextEvent event) {
//初始化webApplicationCotext</font>
initWebApplicationContext(event.getServletContext());
}
3.WebApplicationContext的创建
public WebApplicationContext initWebApplicationContext(
ServletContext servletContext) {
// application对象中存放了spring context,则抛出异常
// 其中ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
if (servletContext
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - "
+ "check whether you have multiple ContextLoader* definitions in your web.xml!");
}
// 创建得到WebApplicationContext
// createWebApplicationContext最后返回值被强制转换为ConfigurableWebApplicationContext类型
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
// 只要上一步强转成功,进入此方法(事实上走的就是这条路)
if (this.context instanceof ConfigurableWebApplicationContext) {
// 强制转换为ConfigurableWebApplicationContext类型
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
// cwac尚未被激活,目前还没有进行配置文件加载
if (!cwac.isActive()) {
// 加载配置文件
configureAndRefreshWebApplicationContext(cwac, servletContext);
【点击进入该方法发现这样一段:
//为wac绑定servletContext
wac.setServletContext(sc);
//CONFIG_LOCATION_PARAM=contextConfigLocation
//getInitParameter(CONFIG_LOCATION_PARAM)解释了为什么配置文件中需要有contextConfigLocation项
//需要注意还有sevletConfig.getInitParameter和servletContext.getInitParameter作用范围是不一样的
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (initParameter != null) {
//装配ApplicationContext的配置信息
wac.setConfigLocation(initParameter);
}
】
}
}
// 把创建好的spring context,交给application内置对象,提供给监听器/过滤器/拦截器使用
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
this.context);
// 返回webApplicationContext
return this.context;
}
三,总的来说
第一步:在web启动的时候,tomcat会读取web.xml中的两个标签<=listener><=/listener>以及<=context-param><=/context-param>标签。
第二步:Tomcat会创建一个ServletContext对象,应用范围为真个WEB工程都能够使用这个对象。
第三步:Tomcat将读取到的<=context-param>信息转换成键值对,并交给ServletContext。
第四步:Tomcat读取<=listener><=/listener>监听器。
第五步:调用监听器的contextInitialized(ServletContextEvent event)初始化方法,这个方法可以通过envent.getServletContext().getInitParameter(“contextConfigLocation”);来得到context-param中的信息。继而执行this.context = this.createWebApplicationContext(servletContext);然后将得到的容器放入ServletContext作用域中:
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);