JVM学习记录-类加载器

前言

JVM设计团队把类加载阶段中的“通过一个类的全限定名来获取描述此类的二进制字节流”这个动作房东Java虚拟机外面去实现,以便让应用程序自己决定如何去获取所需要的类。实现这个动作的代码模块称为“类加载器”。

类与类加载器

类加载器虽然只用户实现类的加载动作,但它在Java程序中起到的作用却远远不限于类加载阶段。每个类都有一个独立的类名称空间,在比较两个类是否“相等”,只有两个类是由同一个类加载器加载的前提下才有意义,否则即使两个类来源于同一个Class文件,被同一个虚拟机加载,只要加载它们的类加载器不同,那这两个类就必定不相等。

这里的相等,包含Class对象的equals()方法、isAssignableFrom()方法、isInstance()方法的返回结果,也包括使用instanceof关键字做对象所属关系判定等情况。例如如下代码:

public class ClassLoaderTest {

    public static void main(String[] args) throws Exception{

        ClassLoader myClassLoader = new ClassLoader() {
            /**
             * Loads the class with the specified <a href="#name">binary name</a>.
             * This method searches for classes in the same manner as the {@link
             * #loadClass(String, boolean)} method.  It is invoked by the Java virtual
             * machine to resolve class references.  Invoking this method is equivalent
             * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
             * false)</tt>}.
             *
             * @param name The <a href="#name">binary name</a> of the class
             * @return The resulting <tt>Class</tt> object
             * @throws ClassNotFoundException If the class was not found
             */
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                try{
                    String fileName = name.substring(name.lastIndexOf(".")+1)+".class";

                    InputStream inputStream = getClass().getResourceAsStream(fileName);
                    if(null == inputStream){
                        return super.loadClass(name);
                    }
                    byte[] b = new byte[inputStream.available()];
                    inputStream.read(b);
                    return defineClass(name,b,0,b.length);
                }catch (IOException e){
                    throw new ClassNotFoundException(name);
                }

            }
        };

        Object obj = myClassLoader.loadClass("com.eurekaclient2.client2.shejimoshi.JVM.ClassLoaderTest").newInstance();

        System.out.println("来源:"+obj.getClass());

        System.out.println(obj instanceof com.eurekaclient2.client2.shejimoshi.JVM.ClassLoaderTest);
    }

}

运行结果:

来源:class com.eurekaclient2.client2.shejimoshi.JVM.ClassLoaderTest
false

从运行结果中我们可以看出来,obj对象确实属于ClassLoaderTest类的对象,但是从运行结果的第二行中可以看出来,这个对象与ClassLoaderTest类做所属类型检查时返回的false,因为虚拟机中存在了两个ClassLoaderTest类,一个是由系统应用程序类加载器加载的,另一个是由我们自定义的类加载器加载的,虽然都来自同一个Class文件,但依然是两个独立的类。

双亲委派模型

从虚拟机的角度来讲,只存在两种不同的类加载器:一种是启动类加载器(Bootstrap ClassLoader);另一种就是所有其他的类加载器,这些类加载器都是由Java语言实现,并且独立于虚拟机外部,并都继承自抽象类java.lang.ClassLoader。

从Java开人员的角度来看,类加载器可以分的更细一些,但是绝大部分java程序都会用到下面的这3种系统提供的类加载器。

启动类加载器(Bootstrap ClassLoader):它负责将放在<JAVA_HOME>\lib目录中的,或者被-Xbootclasspath参数所指定的路径中,并且是虚拟机识别的类库加载到虚拟机中。

扩展类加载器(Extension ClassLoader):它负责加载<JAVA_HOME>\lib\ext目录中的,或者被java.ext.dirs系统变量所指定的路径中的所有类库,开发者可以直接使用扩展类加载器。

应用程序类加载器(Application ClassLoader):它一般被称为系统类加载器,负责加载用户类路径上所指定的类库,开发者可以直接使用这个类加载器,若应用程序中没有自定义过类加载器,一般情况下默认的就是这个应用程序类加载器。

我们的应用程序都是由这3种类加载器相互配合进行加载的,如果有需要,还可以加入自定义的类加载器。这些类加载器的关系如下图:

类加载器的之间的这种层次关系,称为类加载器的双亲委派模式(Parents Delegation Model)。这种模型要求,除了顶层外,其余的类加载器都应当有自己的的父类加载器。这里的子父关系一般不会以继承方式来实现,而是使用组合关系来复用父加载器的代码。

双亲委派模型的工作过程:如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器去完成,每一个层次的类加载器都是这样,最终都应该传送到顶层的启动类加载器中,只有当父类反馈自己无法完成这个加载请求时,子加载器才会尝试自己去加载。

 使用双亲委派模型来组织类加载器之间的关系,有一个显而易见测好处就是Java类随着它的类加载器一起具备了一种带有优先级的层次关系。

记得以前看到过一个面试题,题目大概意思是:能不能自己写一个类叫java.lang.String?

答案:不可以。原因就是因为JVM的类加载器采用的这种双亲委派模型,当我们写了一个类叫java.lang.String时,类加载器发现已经加载过一个同样的类了,不用加载了,直接使用就可以了。所以自己写的这个java.lang.String这个类可以编译通过,但是无法被加载运行。

实现双亲委派的代码集中在java.lang.ClassCloader的loadClass()方法中,逻辑很简单:首先检查自己是否已经被加载过,如果没有加载则调用父加载器的loadClass()方法,若父加载器为空则默认使用启动类加载器作为父加载器。如果父加载器加载失败,则抛出ClassNotFoundException异常后,再调用自己的findClass()方法进行加载。

loadClass的源码:

/**
     * Loads the class with the specified <a href="#name">binary name</a>.  The
     * default implementation of this method searches for classes in the
     * following order:
     *
     * <ol>
     *
     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
     *   has already been loaded.  </p></li>
     *
     *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
     *   on the parent class loader.  If the parent is <tt>null</tt> the class
     *   loader built-in to the virtual machine is used, instead.  </p></li>
     *
     *   <li><p> Invoke the {@link #findClass(String)} method to find the
     *   class.  </p></li>
     *
     * </ol>
     *
     * <p> If the class was found using the above steps, and the
     * <tt>resolve</tt> flag is true, this method will then invoke the {@link
     * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
     *
     * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
     * #findClass(String)}, rather than this method.  </p>
     *
     * <p> Unless overridden, this method synchronizes on the result of
     * {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} method
     * during the entire class loading process.
     *
     * @param  name
     *         The <a href="#name">binary name</a> of the class
     *
     * @param  resolve
     *         If <tt>true</tt> then resolve the class
     *
     * @return  The resulting <tt>Class</tt> object
     *
     * @throws  ClassNotFoundException
     *          If the class could not be found
     */
    protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    if (parent != null) {
                        c = parent.loadClass(name, false);
                    } else {
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    c = findClass(name);

                    // this is the defining class loader; record the stats
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }

破坏双亲委派模型

双亲委派模型虽然在类加载器中很重要,但是并不是Java强制性要求的一个模型,而是Java设计者推荐给开发者的类加载器的实现方式。在Java世界中大部分的类加载器都遵循这个模型,但也有例外,双亲委派模型到目前为止主要出现过3次较大规模的“被破坏”情况。

  • 第一次:由于类加载器是JDK1.0就已经存在了,而双亲委派模型在JDK1.2之后才被引入,所以为了向前兼容,做了一些妥协。在JDK1.2以后已不再提倡用户去覆盖loadClass()方法,而应该把自己的实现逻辑写在findClass()方法中,这样在loadClass方法中如果父类加载器加载失败,就会调用自己的findClass方法来完成加载,这样就保证了自己实现的类加载器符合双亲委派模型了。
  • 第二次:双亲委派模型的规则是自低向上(由子到父)来进行加载的,但是有些情况下父类是需要调用子类的代码,这种情况就需要破坏这个模型了。为了解决这种情况,Java设计团队,引入了一个新的加载器:线程上下文加载器(Trhead Context ClassLoader)。这个类加载器可以通过java.lang.Thread类的setCOntextClassLoader()方法进行设置,通过getContextClassLoader()方法来获得。如果创建线程时还未设置,它会从父线程中继承一个,如果在应用程序的全局范围内都没有设置过的话,那这个类加载器就是应用程序类加载器了。Java中所有涉及SPI的加载动作基本上都采用这种方式,例如:JNDI、JDBC、JCE、JAXB、和JBI等。其实我们常用的Tomcat这种应用服务器也是使用的这种类加载器。
  • 第三次:为了实现热部署,热插拔,模块化等功能。就是说更新了一些模块而不需要重启,只需要把类和类加载器一同替换掉就可以实现热部署了。

猜你喜欢

转载自www.cnblogs.com/jimoer/p/9098142.html
今日推荐