前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)

Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)

作者头像
用户1148394
发布2018-01-09 17:29:44
7080
发布2018-01-09 17:29:44
举报
文章被收录于专栏:余林丰余林丰

基于Spring-4.3.7.RELEASE

Spring的配置不仅仅局限在XML文件,同样也可以使用Java代码来配置。在这里我使用XML配置文件的方式来粗略地讲讲WebApplicationContext。

一提到Spring,首先就应该能想到的是IoC和AOP,什么是IoC、AOP不在这里做讲解。Spring提供一个最为基础的IoC容器——BeanFactory,但这个IoC容器所能提供给我们的功能比较少,所以我们通常选用另一个——ApplicationContext(应用上下文)来作为我们的IoC容器,其实ApplicationContext也是继承自BeanFactory,只是在BeanFactory接口基础上做了扩展。那我们这篇文章里要提到的WebApplicationContext不难猜测出它是ApplicationContext的一个实现,在Web应用中我们就利用WebApplicationContext作为我们的IoC容器。

在Web应用中要使用Spring的IoC容器,首要问题就是如何将IoC容器加载到Web容器中。以下是web.xml的部分配置:

代码语言:javascript
复制
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这段配置意为给Servlet新增一个监听器,这个监听器需要实现ServletContextListener接口,该接口中有两个方法:

代码语言:javascript
复制
public interface ServletContextListener extends EventListener {
    public void contextInitialized(ServletContextEvent sce);  //ServletContext初始化的时候执行此方法
    public void contextDestroyed(ServletContextEvent sce);  //ServletContext销毁的时候执行此方法
}

接着来看ContextLoaderListener:

代码语言:javascript
复制
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
    public ContextLoaderListener() {
    }

    public ContextLoaderListener(WebApplicationContext context) {
        super(context);
    }

    public void contextInitialized(ServletContextEvent event) {
        this.initWebApplicationContext(event.getServletContext());
    }

    public void contextDestroyed(ServletContextEvent event) {
        this.closeWebApplicationContext(event.getServletContext());
        ContextCleanupListener.cleanupAttributes(event.getServletContext());
    }
}

从ContextLoaderListener可以看出WebApplicationContext的初始化实际上是由ContextListener完成的:public void initWebApplicationContext(ServletContext servletContext) 

代码语言:javascript
复制
 1 public void initWebApplicationContext(ServletContext servletContext) 
 2     ......
 3     if(this.context == null) {
 4         this.context = this.createWebApplicationContext(servletContext);    //创建根上下文,在这之前会检查是否已经存在,如果存在则抛出IllegalStateExcpetion异常,跳到第22行
 5     }
 6     ......
 7     if(this.context instanceof ConfigurableWebApplicationContext) {    
 8         ConfigurableWebApplicationContext err = (ConfigurableWebApplicationContext)this.context;
 9         if(!err.isActive()) {
10             if(err.getParent() == null) {
11                 ApplicationContext elapsedTime = this.loadParentContext(servletContext);
12                 err.setParent(elapsedTime);
13             }
14         this.configureAndRefreshWebApplicationContext(err, servletContext);    //ApplicationContext上下文创建好后对其进行赋值和初始化,跳到第31行
15         }
16     }
17     //将WebApplicationContext根上下文绑定到Web应用程序的ServletContext上.
18     servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
19     .....
20      return this.context;
21 }
22 protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
23     Class contextClass = this.determineContextClass(sc);    //判断使用什么样的类在Web容器中作为IoC容器,跳到第26行
24     ......
25 }
26 protected Class<?> determineContextClass(ServletContext servletContext) {
27     String contexClassName = servlet.getInitParameter(CONTEXT_CLASS_PARAM);    //读取web.xml中的配置<context-param>contextClass</context-param>
28     //如果配置了需要使用的CONTEXT_CLASS,那就是用这个class,如果没有额外的配置,就是用默认的ContextClass也就是XmlWebApplicationContext.
29 }
30 //设置IoC容器的参数,并通过refresh启动容器的初始化
31 protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc){
32     String configLocationParam;
33 if(ObjectUtils.identityToString(wac).equals(wac.getId())) {
34     configLocationParam = sc.getInitParameter("contextId");
35     if(configLocationParam != null) {
36         wac.setId(configLocationParam);
37     } else {
38         wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath()));
39     }
40 }
41     wac.setServletContext(sc);
42     configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);    //contextConfigLocation,Spring根应用上下文重要的配置文件,很多bean的定义等等
43     ......
44     wac.refresh();    //启动容器的初始化
45 }

 以上代码第27行所述web.xml中配置指定的IoC容器:

代码语言:javascript
复制
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

以上代码第42行所述web.xml中配置指定的IoC容器:

代码语言:javascript
复制
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
</context-param>

上面这段配置就是自定义要使用的IoC容器而不使用默认的XmlApplicationContext容器.

第7行代码开始,当ApplicationContext上下文建立起来过后,也就是Web应用中的IoC容器建立起来过后,接下来就是applicationContext设置一些参数例如它的双亲.至此在Web应用环境中的IoC容器就已经完成了初始化,由于要考虑Web容器的环境特别,比如各种参数的设置,所以在上面的代码能看出首先创建了IoC容器,其次再为容器赋一些参数值,最后还有IoC容器和Web容器SevletContext的结合作为全局应用上下文.在接下来会介绍在启动Spring MVC时DispatcherServert在进行自己持有的上下文的初始化时,将ApplicationContext根应用上下文设置为DispatcherServlet的双亲上下文.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-04-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档