(八)Tomcat源码阅读:Context组件分析

一、概述

 StandardContext的继承关系如下图,我们就按照下面的顺序进行介绍,其中有一些类比较常规,我在这里便不在赘述,如果有其它的类关乎流程又比较重要我也加入介绍。

二、阅读源码

1、ContextBind

(1)方法

这两个方法分别绑定和解绑类加载器,tomcat打破双亲委派机制就在这里。具体的实现,我们到具体的类中查看。

ClassLoader bind(boolean usePrivilegedAction, ClassLoader originalClassLoader);
void unbind(boolean usePrivilegedAction, ClassLoader originalClassLoader);

2、Context

该接口主要定义了一些对servlet的处理,seesiong的处理,以及资源的处理。具体的实现我们需要到具体的类中查看。

 

3、StandardContext

(1)方法

postWorkDirectory:

该方法设置了context的工作目录。 

setResources:

设置对应的资源集。

 resourcesStart:

 启动资源。

startInternal:

startInternal的工作流程是这样的,先设置工作目录,然后设置资源,之后将资源全部启动。我们可以知道资源是接下来分析的对象,因为资源类中装有资源,是启动流程的关键。

 

 

4、WebResourceRoot

根据上面设置资源的方法我们可以知道WebResourceRoot是装资源的资源类,我们由上到下进行学习。

(1)注释 

该类包含了一个web应用的全部资源集,资源集有如下分类:Pre(主类启动前的资源集),Main(主类资源集),JARs(jar包资源集),Post(启动后的资源集),对应了下面的枚举类。

/**
 * Represents the complete set of resources for a web application. The resources
 * for a web application comprise of multiple ResourceSets and when looking for
 * a Resource, the ResourceSets are processed in the following order:
 * <ol>
 * <li>Pre  - Resources defined by the &lt;PreResource&gt; element in the web
 *            application's context.xml. Resources will be searched in the order
 *            they were specified.</li>
 * <li>Main - The main resources for the web application - i.e. the WAR or the
 *            directory containing the expanded WAR</li>
 * <li>JARs - Resource JARs as defined by the Servlet specification. JARs will
 *            be searched in the order they were added to the ResourceRoot.</li>
 * <li>Post - Resources defined by the &lt;PostResource&gt; element in the web
 *            application's context.xml. Resources will be searched in the order
 *            they were specified.</li>
 * </ol>
 */
   enum ResourceSetType {
        PRE,
        RESOURCE_JAR,
        POST,
        CLASSES_JAR
    }

(2)方法

WebResourceRoot的主要方法还是拿资源,具体的方法实现,我们需要看具体的实现类。

6、StandardRoot

(1)属性

    private final List<WebResourceSet> preResources = new ArrayList<>();
    private WebResourceSet main;
    private final List<WebResourceSet> classResources = new ArrayList<>();
    private final List<WebResourceSet> jarResources = new ArrayList<>();
    private final List<WebResourceSet> postResources = new ArrayList<>();

(2)方法

createWebResourceSet:

该方法会根据对应的目录生成对应的资源集对象,并加入对应的资源集列表中。 

createMainResourceSet:

 该方法根据资源是war包或者文件夹,加载主类资源。

processWebInfLib:

processWebInfLib方法就是根据传参,创建对应的资源集。

 startInternal:

startInternal先创建主资源类,之后在创建其它的资源类。

7、WebResourceSet

 WebResourceSet我们主要知道它的结构就可以了。WebResourceSet下面有jar资源集,war资源集,file资源集,不同资源集有不同的资源结构。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小海海不怕困难

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值