论坛首页 Java企业应用论坛

ClassLoader双亲委派

浏览 6709 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-05-14  
       下午做spa安装程序的时候,由于在jar文件中包含了config.properties文件,导致花了近两个小时来解决"uri is not hierarchical"问题,真是郁闷。就因为一点小疏忽……
       不过,对JVM ClassLoader加载体系有了更加清楚的理解。
       JVM的类加载体系,相信读过《Inside JVM》的人可能比较熟悉一些。在1.2之后,类加载器体系就是双亲委派体系。依次从高到低级排序是:
       启动类加载器, 标准类加载器, 系统类加载器(也就是类路径加载器,JVM实现的用户类加载器),用户自定义类加载器。
       我们的资源一般都是通过系统类加载器来进行加载的,比如class文件,jar文件,以及其它资源。

        在eclipse项目中,属性文件config.properties直接放在src根目录下即可,在程序中,直接:
   
java 代码
 
  1. URL url = this.getClass().getClassLoader().getSystemResource("config.properties");  
  2. URI cfgUri = URI.create(url.toExternalForm());  
  3. File cfgFile = new File(cfgUri);  

     这样就OK了。它自然会在classpath上找到这个config.properties。

     那如果把这个文件放在JAR文件中,做为JAR的资源呢,其实也是没有关系,上面的代码也是可以找到JAR文件中的资源的,一点错误都没有。同时,也可以这么来加载:
      
java 代码
  1. URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);  
    一样,都是从JAR文件中去加载文件。

        但是,如果jar文件中包含了config.properties,并且jar文件外面也包含了config.properties文件,那么结果会怎么样呢?
注意,下面用====包围的解释是不正确的。我自己弄错了双亲委派,哈哈。详细说明:
http://java.sun.com/docs/books/tutorial/ext/basics/load.html

============================================================================================= 
因为classloader加载是如果自己加载不了,才让它的双亲classLoader来加载。
 
       这里,由于加载jar的classloader,暂且叫做jarloader可以在jar文件中找到config.properties文件,那么显然就不用去麻烦它的双亲去加载了。要达到加载外面config.properties文件的效果,必须在jar文件中将它去除,免得jarloader麻烦它的双亲classloader去加载它,因为jarloader是被系统类加载器加载的,因此,系统类加载器在classpath上继续查找,找到config.properties文件,这样就加载成功了。

============================================================================================= 
       应该是class path classloader加载资源顺序的问题。
   发表时间:2007-05-14  
"因为classloader加载是如果自己加载不了,才让它的双亲classLoader来加载。"

说反了吧..

     * <p> This method will first search the parent class loader for the
     * resource; if the parent is <tt>null</tt> the path of the class loader
     * built-in to the virtual machine is searched.  That failing, this method
     * will invoke {@link #findResource(String)} to find the resource.  </p>
0 请登录后投票
   发表时间:2007-05-14  
哦,是的,我犯了一个错误。

那这里的问题应该不是类的双亲委派的问题,而是class path类加载器本身加载资源的顺序问题。可能就是先从JAR里头的资源加载,没有找到,再从classpath加载。

谢谢你指出我的错误。


shaucle 写道
"因为classloader加载是如果自己加载不了,才让它的双亲classLoader来加载。"

说反了吧..

     * <p> This method will first search the parent class loader for the
     * resource; if the parent is <tt>null</tt> the path of the class loader
     * built-in to the virtual machine is searched.  That failing, this method
     * will invoke {@link #findResource(String)} to find the resource.  </p>
0 请登录后投票
   发表时间:2007-05-14  
呵呵,loadClass也是一样

学ClassLoader看OSGI
学J2EE用法看spring
学J2EE机制看jboss,hsqldb

个人认为学习有法(捷径)可行。
0 请登录后投票
   发表时间:2007-05-15  
前两句半赞同赞同。为什么hsqldb也是学习J2EE的好方法呢?
我用hsqldb快半年了,没有看到它和J2EE有什么关系……

shaucle 写道
呵呵,loadClass也是一样

学ClassLoader看OSGI
学J2EE用法看spring
学J2EE机制看jboss,hsqldb

个人认为学习有法(捷径)可行。
0 请登录后投票
   发表时间:2007-05-15  
俺指的都是源码
0 请登录后投票
   发表时间:2007-05-15  
jdbc, transaction不是么

一般一个j2ee应用要开app server and db server
于是jboss,hsqldb...
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics