<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-e2445db1a8.css">
<div class="htmledit_views">
<h1><a name="t0"></a><span style="font-size:32px;">Java类加载机制</span></h1>
<h2><br></h2>
<h2><a name="t2"></a><span style="font-size:24px;">类加载器</span></h2>
<p><span style="font-family:'宋体';"> </span></p>
<p><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';"></span><span style="font-size:18px;">虚拟机设计团队把类加载阶段中的</span></span><span style="font-size:18px;">“<span style="font-family:'宋体';">通过一个类的全限定名来获取描述此类的二进制字节流</span>”<strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">这个动作放到</span>Java<span style="font-family:'宋体';">虚拟机外部去实现</span></span></strong><span style="font-family:'宋体';">,以便让</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">应用程序自己决定如何去获取所需要的类</span></span></strong><span style="font-family:'宋体';">。实现这个动作的</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">代码模块称为</span></span><span style="color:rgb(255,0,0);">“</span><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">类加载器</span></span><span style="color:rgb(255,0,0);">”</span></strong>。</span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';"> </span><span style="font-family:'宋体';"> </span>类加载器可以说是</span>Java<span style="font-family:'宋体';">语言的一项创新,也是</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">语言流行的重要原因之一,它最初是为了满足</span><span style="font-family:'Times New Roman';">Java
Applet</span><span style="font-family:'宋体';">的需求而开发出来的。虽然目前</span><span style="font-family:'Times New Roman';">Java Applet</span><span style="font-family:'宋体';">技术基本上已经“死掉”,但类加载器却在类层次划分、</span><span style="font-family:'Times New Roman';">OSGi</span><span style="font-family:'宋体';">、热部署、代码加密等领域大放异彩,成为了</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">技术体系中一块重要的基石,可谓是失之桑榆,收之东隅。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span></span>类加载器虽然只用于实现类的加载动作</span>,</span><span style="font-size:18px;font-family:'宋体';">但它在</span><span style="font-size:18px;">Java</span><span style="font-size:18px;font-family:'宋体';">程序中起到的作用却远远不限于类加载阶段。对于任意一个类,都需要</span><span style="font-size:18px;"><span style="color:rgb(255,0,0);font-family:'宋体';"><strong>由加载它的类加载器和这个类本身一同确立其在</strong></span><span style="color:#ff0000;"><strong>Java</strong></span><span style="font-family:'宋体';"><span style="color:#ff0000;"><strong>虚拟机中的唯一性,每一个类,都拥有一个独立的类名称空间。</strong></span><span style="color:#333333;">这句话可以表达得更通俗一些:</span><strong><span style="color:#ff0000;">比较两个类是否“相等”,只有在这两个类是由同一个类加载器加载的前提下才有意义</span></strong><span style="color:#333333;">。</span></span></span><span style="font-size:18px;font-family:'宋体';">否则,即使这两个类来源于同一个</span><span style="font-size:18px;">Class</span><span style="font-size:18px;font-family:'宋体';">文件,被同一个虚拟机加载,只要加载它们的<strong><span style="color:#ff0000;">类加载器不同,那这两个类就必定不相等</span></strong>。</span></p>
<p><span style="font-size:18px;"><br></span></p>
<p></p>
<h2><a name="t3"></a><span style="font-size:24px;">双亲委派模型</span></h2>
<p></p>
<p><br></p>
<p><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-size:18px;">从</span></span><span style="font-size:18px;">Java<span style="font-family:'宋体';">虚拟机的角度来讲,只存在两种不同的类加载器:一种是启动类加载器(</span><span style="font-family:'Times New Roman';">Bootstrap
ClassLoader</span><span style="font-family:'宋体';">),这个类加载器使用</span><span style="font-family:'Times New Roman';">C++</span><span style="font-family:'宋体';">语言实现,是虚拟机自身的一部分;另一种就是所有其他的类加载器,这些类加载器都由</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">语言实现,独立于虚拟机外部,并且全都继承自抽象类</span><span style="font-family:'Times New Roman';">java.lang.ClassLoader</span><span style="font-family:'宋体';">。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>从</span>Java<span style="font-family:'宋体';">开发人员的角度来看, 类加载器还可以划分得更细致一些, 绝大部分</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">程序都会使用到以下</span><span style="font-family:'Times New Roman';">3</span><span style="font-family:'宋体';">种系统提供的类加载器。</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>1)<span style="font-family:'宋体';">启动类加载器(</span>Bootstrap ClassLoader<span style="font-family:'宋体';">):前面已经介绍过,这个类加载器负责将存放在<</span><span style="font-family:'Times New Roman';">JAVA_HOME</span><span style="font-family:'宋体';">></span><span style="font-family:'Times New Roman';">\lib</span><span style="font-family:'宋体';">目录中的,或者被</span><span style="font-family:'Times New Roman';">-Xbootclasspath</span><span style="font-family:'宋体';">参数所指定的路径中的,并且是虚拟机识别的(仅按照文件名识别,如</span><span style="font-family:'Times New Roman';">rt.jar</span><span style="font-family:'宋体';">,名字不符合的类库即使放在</span><span style="font-family:'Times New Roman';">lib</span><span style="font-family:'宋体';">目录中也不会被加载)类库加载到虚拟机内存中。</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">启动类加载器无法被</span>Java<span style="font-family:'宋体';">程序直接引用</span></span></strong>。</span></p>
<p><span style="font-size:18px;"><span></span><span></span>2)<span style="font-family:'宋体';">扩展类加载器(</span>Extension ClassLoader<span style="font-family:'宋体';">):这个加载器由</span><span style="font-family:'Times New Roman';">sun.misc.Launcher</span></span><span style="font-size:18px;">.ExtClassLoader</span><span style="font-size:18px;font-family:'宋体';">实现,它负责加载<</span><span style="font-size:18px;font-family:'Times New Roman';">JAVA_HOME</span><span style="font-size:18px;font-family:'宋体';">></span><span style="font-size:18px;font-family:'Times New Roman';">\lib\ext</span><span style="font-size:18px;font-family:'宋体';">目录中的,或者被</span><span style="font-size:18px;font-family:'Times New Roman';">java.ext.dirs</span><span style="font-size:18px;font-family:'宋体';">系统变量所指定的路径中的所有类库,<strong><span style="color:#ff0000;">开发者可以直接使用扩展类加载器</span></strong>。</span></p>
<p><span style="font-size:18px;"><span></span><span></span>3)<span style="font-family:'宋体';">应用程序类加载器(</span>Application ClassLoader<span style="font-family:'宋体';">):这个类加载器由</span><span style="font-family:'Times New Roman';">sun.misc.Launcher.AppClassLoader</span><span style="font-family:'宋体';">实现。由于</span><span style="color:rgb(255,0,0);font-family:'宋体';"><strong>这个类加载器是</strong></span><span style="color:rgb(255,0,0);">ClassLoader</span><span style="color:rgb(255,0,0);font-family:'宋体';"><strong>中的</strong></span><span style="color:rgb(255,0,0);font-family:'Times New Roman';"><strong>getSystemClassLoader()</strong></span><span style="font-family:'宋体';"><span style="color:rgb(255,0,0);">方法的返回值,所以一般也称它为系统类加载器。</span>它负责加载用户类路径(Class
Path)上所指定的类库,开发者可以直接使用这个类加载器,如果应用程序中<strong><span style="color:#ff0000;">没有自定义过自己的类加载器,一般情况下这个就是程序中默认的类加载器</span></strong>。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>我们的<strong><span style="color:#ff0000;">应用程序都是由这</span></strong></span><strong><span style="color:#ff0000;">3</span></strong><span style="font-family:'宋体';"><strong><span style="color:#ff0000;">种类加载器互相配合进行加载的</span></strong>,如果有必要,还可以加入自己定义的类加载器。这些类加载器之间的关系一般如下图所示。</span></span></p>
<p style="text-align:center;"><span style="font-family:'宋体';"><img src="https://img-blog.csdn.net/20170226110715016" alt=""><br></span></p>
<p style="text-align:left;"><span style="font-family:'宋体';"></span></p>
<p><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-size:18px;">图中展示的类加载器之间的这种层次关系,称为类加载器的双亲委派模型(</span></span><span style="font-size:18px;">Parents Delegation
Model<span style="font-family:'宋体';">)。双亲委派模型要求除了顶层的启动类加载器外,其余的类加载器都应当有自己的父类加载器。这里<strong><span style="color:#ff0000;">类加载器之间的父子关系一般不会以继承(</span></strong></span><strong><span style="color:#ff0000;"><span style="font-family:'Times New Roman';">Inheritance</span><span style="font-family:'宋体';">)的关系来实现,而是都使用组合(</span><span style="font-family:'Times New Roman';">Composition</span><span style="font-family:'宋体';">)关系来复用父加载器的代码。</span></span></strong><span style="font-family:'宋体';">类加载器的双亲委派模型在</span>JDK
1.2<span style="font-family:'宋体';">期间被引入并被广泛应用于之后几乎所有的</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">程序中,但它并不是一个强制性的约束模型,而是</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">设计者推荐给开发者的一种类加载器实现方式。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>双亲委派模型的工作过程是:</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器去完成,每一个层次的类加载器都是如此</span></span></strong><span style="font-family:'宋体';">,因此所有的加载请求最终都应该传送到顶层的启动类加载器中,只有</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">当父加载器反馈自己无法完成这个加载请求(它的搜索范围中没有找到所需的类)时,子加载器才会尝试自己去加载</span></span></strong><span style="font-family:'宋体';">。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>使用双亲委派模型来组织类加载器之间的关系,有一个显而易见的好处就是</span>Java<span style="font-family:'宋体';">类随着它的类加载器一起</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">具备了一种带有优先级的层次关系</span></span></strong><span style="font-family:'宋体';">。例如类</span>java.lang.Object<span style="font-family:'宋体';">,它存放在</span><span style="font-family:'Times New Roman';">rt.jar</span><span style="font-family:'宋体';">之中,无论哪一个类加载器要加载这个类,最终都是委派给处于模型最顶端的启动类加载器进行加载,因此</span><span style="font-family:'Times New Roman';">Object</span><span style="font-family:'宋体';">类在程序的各种类加载器环境中都是同一个类。相反,如果没有使用双亲委派模型,由各个类加载器自行去加载的话,如果用户自己编写了一个称为</span><span style="font-family:'Times New Roman';">java.lang.Object</span><span style="font-family:'宋体';">的类,并放在程序的</span><span style="font-family:'Times New Roman';">Class</span> Path<span style="font-family:'宋体';">中,那系统中将会出现多个不同的</span><span style="font-family:'Times New Roman';">Object</span><span style="font-family:'宋体';">类,</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">类型体系中最基础的行为也就无法保证,应用程序也将会变得一片混乱。如果读者有兴趣的话,可以尝试去编写一个与</span><span style="font-family:'Times New Roman';">rt.jar</span><span style="font-family:'宋体';">类库中已有类重名的</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">类,将会发现可以正常编译,但永远无法被加载运行</span>。</span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>双亲委派模型对于保证</span>Java<span style="font-family:'宋体';">程序的稳定运作很重要,但它的实现却非常简单,实现双亲委派的代码都集中在</span><span style="font-family:'Times New Roman';">java.lang.ClassLoader</span><span style="font-family:'宋体';">的</span><span style="font-family:'Times New Roman';">loadClass</span><span style="font-family:'宋体';">()方法之中,如以下代码</span><span style="font-family:'宋体';">所示,逻辑清晰易懂:</span></span></p>
<p><span style="font-family:'宋体';"></span></p>
<pre οnclick="hljs.copyCode(event)"><code class="language-java hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">synchronized</span> Class<?> loadClass(String name, <span class="hljs-keyword">boolean</span> resolve)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">throws</span> ClassNotFoundException</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">//首先, 检查请求的类是否已经被加载过了</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> Class c=findLoadedClass(name);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>( c== <span class="hljs-keyword">null</span> ){</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">try</span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>( parent != <span class="hljs-keyword">null</span> ){</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> c = parent.loadClass(name,<span class="hljs-keyword">false</span>);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> } <span class="hljs-keyword">else</span> {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> c = findBootstrapClassOrNull(name);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> } <span class="hljs-keyword">catch</span> (ClassNotFoundException e) {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">//如果父类加载器抛出ClassNotFoundException</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">//说明父类加载器无法完成加载请求</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>( c == <span class="hljs-keyword">null</span> ) {</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">//在父类加载器无法加载的时候</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">//再调用本身的findClass方法来进行类加载</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> c = findClass(name);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="22"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> } </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="23"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">if</span>(resolve){</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="24"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> resolveClass(c);</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="25"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="26"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">return</span> c;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="27"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button" data-title="复制"></div></pre>
<p><span style="font-family:SimSun;font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>先检查是否已经被加载过,若没有加载则调用父加载器的loadClass()方法,若父加载器为空则默认使用启动类加载器作为父加载器。如果父类加载失败,抛出ClassNotFoundException异常后,再调用自己的findClass()方法进行加载。双亲委派的具体逻辑就实现在这个loadClass()方法之中,JDK
1.2之后已不提倡用户再去覆盖loadClass()方法,而应当把自己的类加载逻辑写到findClass()方法中,在loadClass()方法的逻辑里如果父类加载失败,则会调用自己的findClass()方法来完成加载,这样就可以保证新写出来的类加载器是符合双亲委派规则的。<br></span></p>
<p><span style="font-size:18px;"><br></span></p>
<p style="text-align:left;"><span style="font-family:'宋体';"></span></p>
<h2><a name="t4"></a><span style="font-size:24px;">打破双亲委派模型</span></h2>
<p></p>
<p><br></p>
<p><span style="font-family:'宋体';"><span></span><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>上文提到过<strong><span style="color:#ff0000;">双亲委派模型并不是一个强制性的约束模型,而是</span></strong></span></span><span style="font-size:18px;"><span style="color:#ff0000;">Java</span></span><span style="font-size:18px;font-family:'宋体';"><strong><span style="color:#ff0000;">设计者推荐给开发者的类加载器实现方式</span></strong>。在</span><span style="font-size:18px;font-family:'Times New Roman';">Java</span><span style="font-size:18px;font-family:'宋体';">的世界中大部分的类加载器都遵循这个模型,但也有例外</span><span style="font-size:18px;">。</span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>双亲委派模型的</span>一<span style="font-family:'宋体';">次</span>“<span style="font-family:'宋体';">被破坏</span>”<span style="font-family:'宋体';">是由这个模型自身的缺陷所导致的,双亲委派很好地</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">解决了各个类加载器的基础类的统一问题(越基础的类由越上层的加载器进行加载)</span></span></strong><span style="font-family:'宋体';">,</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">基础类之所以称为</span></span><span style="color:rgb(255,0,0);">“</span><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">基础</span></span><span style="color:rgb(255,0,0);">”</span><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">,是因为它们总是作为被用户代码调用的</span>API</span></strong><span style="font-family:'宋体';">,但世事往往没有绝对的完美,如果</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">基础类又要调用回用户的代码</span></span></strong><span style="font-family:'宋体';">,那该怎么办</span>?<span style="font-family:'宋体';">这并非是不可能的事情,一个典型的例子便是</span>JNDI<span style="font-family:'宋体';">服务,</span><span style="font-family:'Times New Roman';">JNDI</span><span style="font-family:'宋体';">现在已经是</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">的标准服务,它的代码由启动类加载器去加载(在</span><span style="font-family:'Times New Roman';">JDK
1.3</span><span style="font-family:'宋体';">时放进去的</span><span style="font-family:'Times New Roman';">rt.jar</span><span style="font-family:'宋体';">),但</span><span style="font-family:'Times New Roman';">JNDI</span><span style="font-family:'宋体';">的目的就是对资源进行集中管理和查找,它需要调用由独立厂商实现并部署在应用程序的</span><span style="font-family:'Times New Roman';">Class</span> Path<span style="font-family:'宋体';">下的</span><span style="font-family:'Times New Roman';">JNDI</span><span style="font-family:'宋体';">接口提供者(</span><span style="font-family:'Times New Roman';">SPI</span>,Service
Provider Interface<span style="font-family:'宋体';">)的代码,但启动类加载器不可能</span>“<span style="font-family:'宋体';">认识</span>”<span style="font-family:'宋体';">这些代码</span><span style="font-family:SimSun;">,因为启动类加载器的搜索范围中找不到用户应用程序类,</span><span style="font-family:'宋体';">那该怎么办?为了解决这个问题,</span>Java<span style="font-family:'宋体';">设计团队只好引入了一个不太优雅的设计:线程上下文类加载器(</span><span style="font-family:'Times New Roman';">Thread
Context ClassLoader</span><span style="font-family:'宋体';">)。这个类加载器可以通过</span><span style="font-family:'Times New Roman';">java.lang.Thread</span><span style="font-family:'宋体';">类的</span><span style="font-family:'Times New Roman';">setContextClassLoa</span>der()<span style="font-family:'宋体';">方法进行设置,如果创建线程时还未设置,它将会从父线程中继承一个,</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">如果在应用程序的全局范围内都没有设置过的话,那这个类加载器默认就是应用程序类加载器(</span>Application
ClassLoader<span style="font-family:'宋体';">)。</span></span></strong></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>有了线程上下文类加载器,就可以做一些</span>“<span style="font-family:'宋体';">舞弊</span>”<span style="font-family:'宋体';">的事情了,</span>JNDI<span style="font-family:'宋体';">服务使用这个线程上下文类加载器去加载所需要的</span><span style="font-family:'Times New Roman';">SPI</span><span style="font-family:'宋体';">代码,也就是</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">父类加载器请求子类加载器去完成类加载的动作</span></span></strong><span style="font-family:'宋体';">,</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">这种行为实际上就是打通了双亲委派模型的层次结构来逆向使用类加载器</span></span></strong><span style="font-family:'宋体';">,实际上已经违背了双亲委派模型的一般性原则,但这也是无可奈何的事情。</span>Java<span style="font-family:'宋体';">中所有涉及</span><span style="font-family:'Times New Roman';">SPI</span><span style="font-family:'宋体';">的加载动作基本上都采用这种方式,例如</span><span style="font-family:'Times New Roman';">JNDI</span><span style="font-family:'宋体';">、</span><span style="font-family:'Times New Roman';">JDBC</span><span style="font-family:'宋体';">、</span><span style="font-family:'Times New Roman';">JCE</span><span style="font-family:'宋体';">、</span><span style="font-family:'Times New Roman';">JAXB</span><span style="font-family:'宋体';">和</span><span style="font-family:'Times New Roman';">JBI</span><span style="font-family:'宋体';">等</span>。</span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>双亲委派模型的</span>另一<span style="font-family:'宋体';">次</span>“<span style="font-family:'宋体';">被破坏</span>”<span style="font-family:'宋体';">是由于用户对程序动态性的追求而导致的,这里所说的</span>“<span style="font-family:'宋体';">动态性</span>”<span style="font-family:'宋体';">指的是当前一些非常</span>“<span style="font-family:'宋体';">热门</span>”<span style="font-family:'宋体';">的名词:</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">代码热替换(</span>HotSwap<span style="font-family:'宋体';">)、模块热部署(</span><span style="font-family:'Times New Roman';">HotDeployment</span><span style="font-family:'宋体';">)等</span></span></strong><span style="font-family:'宋体';">,说白了就是希望</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">应用程序能像我们的计算机外设那样,接上鼠标、</span>U<span style="font-family:'宋体';">盘,不用重启机器就能立即使用</span></span></strong><span style="font-family:'宋体';">,鼠标有问题或要升级就换个鼠标,不用停机也不用重启。对于个人计算机来说,重启一次其实没有什么大不了的,但对于一些生产系统来说,关机重启一次可能就要被列为生产事故,这种情况下<strong><span style="color:#ff0000;">热部署就对软件开发者,尤其是企业级软件开发者具有很大的吸引力</span></strong>。</span>Sun<span style="font-family:'宋体';">公司所提出的</span><span style="font-family:'Times New Roman';">JSR-294</span><span style="font-family:'宋体';">、</span><span style="font-family:'Times New Roman';">JSR-277</span><span style="font-family:'宋体';">规范在与</span><span style="font-family:'Times New Roman';">JCP</span><span style="font-family:'宋体';">组织的模块化规范之争中落败给</span><span style="font-family:'Times New Roman';">JSR-291</span><span style="font-family:'宋体';">(即</span><span style="font-family:'Times New Roman';">OSGi
R4.2</span><span style="font-family:'宋体';">),虽然</span><span style="font-family:'Times New Roman';">Sun</span><span style="font-family:'宋体';">不甘失去</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">模块化的主导权,独立在发展</span><span style="font-family:'Times New Roman';">Jigsaw</span><span style="font-family:'宋体';">项目,但目前</span><span style="font-family:'Times New Roman';">OSGi</span><span style="font-family:'宋体';">已经成为了业界</span>“<span style="font-family:'宋体';">事实上</span>”<span style="font-family:'宋体';">的</span>Java<span style="font-family:'宋体';">模块化标准,而</span><span style="font-family:'Times New Roman';">OSGi</span><span style="font-family:'宋体';">实现模块化热部署的关键则是它自定义的类加载器机制的实现。每一个程序模块(</span><span style="font-family:'Times New Roman';">OSGi</span><span style="font-family:'宋体';">中称为</span><span style="font-family:'Times New Roman';">Bundle</span><span style="font-family:'宋体';">)都有一个自己的类加载器,当需要更换一个</span><span style="font-family:'Times New Roman';">Bundle</span><span style="font-family:'宋体';">时,就把</span><span style="font-family:'Times New Roman';">Bundle</span><span style="font-family:'宋体';">连同类加载器一起换掉以实现代码的热替换。</span></span></p>
<p><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-size:18px;">在</span></span><span style="font-size:18px;">OSGi<span style="font-family:'宋体';">环境下,类加载器不再是双亲委派模型中的树状结构,而是进一步发展为更加复杂的网状结构,当收到类加载请求时,</span><span style="font-family:'Times New Roman';">OSGi</span><span style="font-family:'宋体';">将按照下面的顺序进行类搜索:</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>1<span style="font-family:'宋体';">)将以</span><span style="font-family:'Times New Roman';">java.*</span><span style="font-family:'宋体';">开头的类委派给父类加载器加载。</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>2<span style="font-family:'宋体';">)否则,将委派列表名单内的类委派给父类加载器加载。</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>3<span style="font-family:'宋体';">)否则,将</span><span style="font-family:'Times New Roman';">Import</span><span style="font-family:'宋体';">列表中的类委派给</span><span style="font-family:'Times New Roman';">Export</span><span style="font-family:'宋体';">这个类的</span><span style="font-family:'Times New Roman';">Bundle</span><span style="font-family:'宋体';">的类加载器加载。</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>4<span style="font-family:'宋体';">)否则,查找当前</span><span style="font-family:'Times New Roman';">Bundle</span><span style="font-family:'宋体';">的</span><span style="font-family:'Times New Roman';">Class</span> Path<span style="font-family:'宋体';">,使用自己的类加载器加载。</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>5<span style="font-family:'宋体';">)否则,查找类是否在自己的</span><span style="font-family:'Times New Roman';">Fragment Bundle</span><span style="font-family:'宋体';">中,如果在,则委派给</span><span style="font-family:'Times New Roman';">Fragment
Bundle</span><span style="font-family:'宋体';">的类加载器加载。</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>6<span style="font-family:'宋体';">)否则,查找</span><span style="font-family:'Times New Roman';">Dynamic Import</span><span style="font-family:'宋体';">列表的</span><span style="font-family:'Times New Roman';">Bundle</span><span style="font-family:'宋体';">,委派给对应</span><span style="font-family:'Times New Roman';">Bundle</span><span style="font-family:'宋体';">的类加载器加载。</span></span></p>
<p><span style="font-size:18px;"><span></span><span></span>7<span style="font-family:'宋体';">)否则,类查找失败。</span></span></p>
<p><span style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>上面的查找顺序中只有开头两点仍然符合双亲委派规则,其余的类查找都是在平级的类加载器中进行的。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>只要有足够意义和理由,突破已有的原则就可认为是一种创新。正如</span>OSGi<span style="font-family:'宋体';">中的类加载器并不符合传统的双亲委派的类加载器,并且业界对其为了实现热部署而带来的额外的高复杂度还存在不少争议,但在</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">程序员中基本有一个共识:</span><span style="font-family:'Times New Roman';">OSGi</span><span style="font-family:'宋体';">中对类加载器的使用是很值得学习的,弄懂了</span><span style="font-family:'Times New Roman';">OSGi</span><span style="font-family:'宋体';">的实现,就可以算是掌握了类加载器的精髓</span>。</span></p>
<p><span style="font-size:18px;"><br></span></p>
<h1><a name="t5"></a><span style="font-family:'宋体';">Tomcat的类加载器架构</span></h1>
<p style="text-align:left;"></p>
<p style="font-family:'宋体';"><span style="font-family:'宋体';"> </span></p>
<p style="font-family:'宋体';"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-size:18px;">主流的</span></span><span style="font-size:18px;">Java Web<span style="font-family:'宋体';">服务器</span>(<span style="font-family:'宋体';">也就是</span>Web<span style="font-family:'宋体';">容器</span>)<span style="font-family:'宋体';">,如</span>Tomcat、Jetty<span style="font-family:'宋体';">、</span><span style="font-family:'Times New Roman';">WebLogic</span><span style="font-family:'宋体';">、</span><span style="font-family:'Times New Roman';">WebSphere</span><span style="font-family:'宋体';">或其他笔者没有列举的服务器,都实现了自己定义的类加载器(一般都不止一个)。因为一个功能健全的</span><span style="font-family:'Times New Roman';">Web</span>容<span style="font-family:'宋体';">器,要解决如下几个问题:</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>1)部署在<strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">同一个</span>Web<span style="font-family:'宋体';">容器上</span></span></strong>的<strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">两个</span>Web<span style="font-family:'宋体';">应用程序</span></span></strong>所<strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">使用的</span>Java<span style="font-family:'宋体';">类库可以实现相互隔离</span></span></strong>。这是最基本的需求,两个不同的应用程序可能会依赖同一个第三方类库的不同版本,不能要求一个类库在一个服务器中只有一份,服务器应当保证两个应用程序的类库可以互相独立使用。</span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>2)部署在<strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">同一个</span>Web<span style="font-family:'宋体';">容器上</span></span></strong>的<strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">两个</span>Web<span style="font-family:'宋体';">应用程序所使用的</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">类库可以互相共享</span></span></strong><span style="font-family:'宋体';">。这个需求也很常见,例如,用户可能有</span>10<span style="font-family:'宋体';">个使用</span><span style="font-family:'Times New Roman';">Spring</span><span style="font-family:'宋体';">组织的应用程序部署在同一台服务器上,如果把</span><span style="font-family:'Times New Roman';">10</span><span style="font-family:'宋体';">份</span><span style="font-family:'Times New Roman';">Spring</span><span style="font-family:'宋体';">分别存放在各个应用程序的隔离目录中,将会是很大的资源浪费——这主要倒不是浪费磁盘空间的问题,而是指</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">类库在使用时都要被加载到</span>Web<span style="font-family:'宋体';">容器的内存,如果类库不能共享,虚拟机的方法区就会很容易出现过度膨胀的风险</span></span></strong>。</span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>3)Web<span style="font-family:'宋体';">容器需要尽可能地</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">保证自身的安全不受部署的</span>Web<span style="font-family:'宋体';">应用程序影响</span></span></strong>。目前,有<strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">许多主流的</span>Java
Web<span style="font-family:'宋体';">容器自身也是使用</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">语言来实现</span></span></strong><span style="font-family:'宋体';">的。因此,</span>Web<span style="font-family:'宋体';">容器本身也有类库依赖的问题,一般来说,基于安全考虑,容器所使用的类库应该与应用程序的类库互相独立。</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>4)<span style="font-family:'宋体';">支持</span>JSP<span style="font-family:'宋体';">应用的</span><span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">容器,大多数都需要支持</span><span style="font-family:'Times New Roman';">HotSwap</span><span style="font-family:'宋体';">功能。我们知道,</span><span style="font-family:'Times New Roman';">JSP</span><span style="font-family:'宋体';">文件最终要编译成</span><span style="font-family:'Times New Roman';">Java
Class</span><span style="font-family:'宋体';">才能由虚拟机执行,但</span><strong><span style="color:rgb(255,0,0);">JSP<span style="font-family:'宋体';">文件由于其纯文本存储的特性,运行时修改的概率远远大于第三方类库或程序自身的</span><span style="font-family:'Times New Roman';">Class</span><span style="font-family:'宋体';">文件</span></span></strong><span style="font-family:'宋体';">。而且</span>ASP<span style="font-family:'宋体';">、</span><span style="font-family:'Times New Roman';">PHP</span><span style="font-family:'宋体';">和</span><span style="font-family:'Times New Roman';">JSP</span><span style="font-family:'宋体';">这些网页应用也把</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">修改后无须重启作为一个很大的</span>“优势”来看待</span></strong><span style="font-family:'宋体';">,因此</span>“主流”的<span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">容器都会</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">支持</span>JSP<span style="font-family:'宋体';">生成类的热替换</span></span></strong><span style="font-family:'宋体';">,当然也有</span>“非主流”的,如运行在生产模式(<span style="font-family:'Times New Roman';">Production
Mode</span><span style="font-family:'宋体';">)下的</span><span style="font-family:'Times New Roman';">WebLogic</span><span style="font-family:'宋体';">服务器默认就不会处理</span><span style="font-family:'Times New Roman';">JSP</span><span style="font-family:'宋体';">文件的变化。</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>由于存在上述问题,在部署</span>Web<span style="font-family:'宋体';">应用时,单独的一个</span><span style="font-family:'Times New Roman';">Class
Path</span><span style="font-family:'宋体';">就无法满足需求了,所以各种</span><span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">容都“不约而同”地</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">提供了好几个</span>Class Path<span style="font-family:'宋体';">路径供用户存放第三方类库</span></span></strong><span style="font-family:'宋体';">,这些路径一般都以</span>“<span style="font-family:'Times New Roman';">lib</span><span style="font-family:'宋体';">”或“</span><span style="font-family:'Times New Roman';">classes</span><span style="font-family:'宋体';">”命名。被放置到不同路径中的类库,具备不同的访问范围和服务对象,通常,</span><strong><span style="color:rgb(255,0,0);"><span style="font-family:'宋体';">每一个目录都会有一个相应的自定义类加载器去加载放置在里面的</span>Java<span style="font-family:'宋体';">类库</span></span></strong><span style="font-family:'宋体';">。现在,就以</span>Tomcat<span style="font-family:'宋体';">容器为例,看一看</span><span style="font-family:'Times New Roman';">Tomcat</span><span style="font-family:'宋体';">具体是如何规划用户类库结构和类加载器的。</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>在</span>Tomcat<span style="font-family:'宋体';">目录结构中,有</span><span style="font-family:'Times New Roman';">3</span><span style="font-family:'宋体';">组目录(</span>“/common/*”<span style="font-family:'宋体';">、</span>“/server/*”<span style="font-family:'宋体';">和</span>“/shared/*”<span style="font-family:'宋体';">)可以存放</span>Java<span style="font-family:'宋体';">类库,另外还可以加上</span><span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">应用程序自身的目录</span>“/WEB-INF/*”<span style="font-family:'宋体';">,一共</span>4<span style="font-family:'宋体';">组,把</span><span style="font-family:'Times New Roman';">Java</span><span style="font-family:'宋体';">类库放置在这些目录中的含义分别如下</span>:</span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>①<span style="font-family:'宋体';">放置在</span>/common<span style="font-family:'宋体';">目录中:类库可被</span><span style="font-family:'Times New Roman';">Tomcat</span><span style="font-family:'宋体';">和<span style="color:#ff0000;"><strong>所有的</strong></span></span><span style="font-family:'Times New Roman';"><span style="color:#ff0000;"><strong>Web</strong></span></span><span style="font-family:'宋体';"><span style="color:#ff0000;"><strong>应用程序共同使用</strong></span>。</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>②放置在/server<span style="font-family:'宋体';">目录中:类库可被</span>Tomcat<span style="font-family:'宋体';">使用,对所有的</span><span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">应用程序都不可见。</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>③放置在<span style="font-family:'Times New Roman';">/shared</span><span style="font-family:'宋体';">目录中:类库可被所有的</span><span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">应用程序共同使用,但对</span><span style="font-family:'Times New Roman';">Tomcat</span><span style="font-family:'宋体';">自己不可见。</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>④放置在<span style="font-family:'Times New Roman';">/WebApp/WEB-INF</span><span style="font-family:'宋体';">目录中:类库仅仅可以被此</span><span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">应用程序使用,对</span><span style="font-family:'Times New Roman';">Tomcat</span><span style="font-family:'宋体';">和其他</span><span style="font-family:'Times New Roman';">Web</span><span style="font-family:'宋体';">应用程序都不可见。</span></span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span></span>为了支持这套目录结构,并对目录里面的类库进行加载和隔离,</span>Tomcat<span style="font-family:'宋体';">自定义了多个类加载器,这些类加载器按照经典的双亲委派模型来实现,其关系如</span>下图<span style="font-family:'宋体';">所示</span>。</span></p>
<p style="font-family:'宋体';"><span style="font-size:18px;"><br></span></p>
<div style="text-align:center;"><span style="font-family:'宋体';"><img src="https://img-blog.csdn.net/20170226110723953" alt=""><br></span></div>
<div style="text-align:center;"><span style="font-family:'宋体';"><br></span></div>
<div style="text-align:left;"><span style="font-family:'宋体';"><br></span></div>
<p style="text-align:left;"><span style="font-family:'宋体';"></span></p>
<p><span style="font-family:'宋体';"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span></span><span style="font-family:SimSun;"><span style="font-size:18px;">上图中灰色背景的</span><span style="font-size:18px;">3个类加载器是JDK默认提供的类加载器,这3个加载器的作用已经介绍过了。而CommonClassLoader、CatalinaClassLoader、SharedClassLoader和WebappClassLoader则是Tomcat自己定义的类加载器,它们分别加载/common/*、/server/*、/shared/*和/WebApp/WEB-INF/*中的Java类库。<strong><span style="color:rgb(255,0,0);">其中WebApp类加载器和Jsp类加载器通常会存在多个实例</span></strong>,<strong><span style="color:rgb(255,0,0);">每一个Web应用程序对应一个WebApp类加载器</span></strong>,<strong><span style="color:rgb(255,0,0);">每一个JSP文件对应一个Jsp类加载器</span></strong>。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:SimSun;"> 从图中的委派关系中可以看出,<strong><span style="color:rgb(255,0,0);">CommonClassLoader能加载的类都可以被Catalina</span><span style="color:rgb(255,0,0);"> </span><span style="color:rgb(255,0,0);">ClassLoader和SharedClassLoader使用,而CatalinaClassLoader和Shared</span><span style="color:rgb(255,0,0);"> </span><span style="color:rgb(255,0,0);">ClassLoader自己能加载的类则与对方相互隔离</span></strong>。WebAppClassLoader可以使用SharedClassLoader加载到的类,但各个WebAppClassLoader实例之间相互隔离。而JasperLoader的加载范围仅仅是这个JSP文件所编译出来的那一个.Class文件,它出现的目的就是为了被丢弃:<strong><span style="color:rgb(255,0,0);">当</span><span style="color:rgb(255,0,0);">Web容器</span><span style="color:rgb(255,0,0);">检测到JSP文件被修改时,会替换掉目前的JasperLoader的实例,并通过再建立一个新的Jsp类加载器来实现JSP文件的HotSwap功能。</span></strong></span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"></span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:SimSun;">对于Tomcat的6.x版本,只有指定了tomcat/conf/catalina.properties配置文件的server.loader和share.loader项后才会真正建立Catalina ClassLoader和Shared
ClassLoader的实例,否则在用到这两个类加载器的地方都会用Common ClassLoader的实例代替,而默认的配置文件中没有设置这两个loader项,所以<strong><span style="color:rgb(255,0,0);">Tomcat 6.x顺理成章地把/common、/server和/shared三个目录默认合并到一起变成一个/lib目录,这个目录里的类库相当于以前/common目录中类库的作用</span></strong>。这是Tomcat设计团队为了简化大多数的部署场景所做的一项改进,如果默认设置不能满足需要,用户可以通过修改配置文件指定server.loader和share.loader的方式重新启用Tomcat
5.x的加载器架构。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:SimSun;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>Tomcat加载器的实现清晰易懂,并且采用了官方推荐的“正统”的使用类加载器的方式。如果读者阅读完上面的案例后,能完全理解Tomcat设计团队这样布置加载器架构的用意,那说明已经大致掌握了类加载器“主流”的使用方式,那么笔者不妨再提一个问题让读者思考一下:前面曾经提到过一个场景,如果有10个Web应用程序都是用Spring来进行组织和管理的话,可以把Spring放到Common或Shared目录下让这些程序共享。Spring要对用户程序的类进行管理,自然要能访问到用户程序的类,而用户的程序显然是放在/WebApp/WEB-INF目录中的,那么被CommonClassLoader或SharedClassLoader加载的Spring如何访问并不在其加载范围内的用户程序呢?如果研究过虚拟机类加载器机制中的双亲委派模型,相信读者可以很容易地回答这个问题。</span></span></p>
<p><span style="font-size:18px;"><span style="font-family:'宋体';"></span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:SimSun;">分析:如果<strong><span style="color:rgb(255,0,0);">按主流的双亲委派机制,显然无法</span><span style="color:rgb(255,0,0);">做到让</span><span style="color:rgb(255,0,0);">父类加载器加载的类</span><span style="color:rgb(255,0,0);">去</span><span style="color:rgb(255,0,0);">访问子类加载器加载的类</span></strong>,上面在类加载器一节中提到过通过线程上下文方式传播类加载器。</span></span></p>
<p><span style="font-family:SimSun;"><span style="font-size:18px;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>答案是<strong><span style="color:rgb(255,0,0);">使用线程上下文类加载器来实现</span></strong>的,使用<strong><span style="color:rgb(255,0,0);">线程</span><span style="color:rgb(255,0,0);">上下文加载器,可以让父类加载器请求子类加载器去完成类加载的动作</span></strong>。看spring源码发现,spring加载类所用的Classloader是通过Thread.currentThread().getContextClassLoader()</span><span style="font-size:18px;">来获取的,</span><span style="font-size:18px;">而当线程创建时会默认</span><span style="font-size:18px;">setContextClassLoader(AppClassLoader)</span><span style="font-size:18px;">,</span><span style="font-size:18px;">即线程上下文类加载器被设置为</span><span style="font-size:18px;">AppClassLoader</span><span style="font-size:18px;">,</span><span style="font-size:18px;">spring</span><span style="font-size:18px;">中始终可以获取到这个</span><span style="font-size:18px;">AppClassLoader(</span><span style="font-size:18px;">在</span><span style="font-size:18px;">Tomcat</span><span style="font-size:18px;">里就是</span><span style="font-size:18px;">WebAppClassLoader)</span><span style="font-size:18px;">子类加载器来加载</span><span style="font-size:18px;">bean</span><span style="font-size:18px;">,以后任何一个线程都可以通过</span><span style="font-size:18px;">getContextClassLoader()</span><span style="font-size:18px;">获取到</span><span style="font-size:18px;">WebAppClassLoader</span><span style="font-size:18px;">来</span><span style="font-size:18px;">getbean</span><span style="font-size:18px;">了</span><span style="font-size:18px;">。</span></span></p>
<p><span style="font-size:18px;"><br></span></p>
<p><span style="font-size:18px;"><span style="font-family:SimSun;"><span style="font-family:'宋体';font-size:18px;"> </span><span style="font-family:'宋体';font-size:18px;"> </span>本篇博文内容取材自《深入理解Java虚拟机:JVM高级特性与最佳实践》</span></span></p>
<br><p><span style="font-family:'宋体';"><br></span></p>
<br> </div>
</div>
Java类加载机制与Tomcat类加载器架构
最新推荐文章于 2021-07-22 16:13:49 发布