Java_动态加载类(英文)

It is possible to load and reload classes at runtime in Java, though it is not as straightforward as one might have hoped. This text will explain when and how you can load and reload classes in Java.

You can argue whether Java‘s dynamic class loading features are really part of Java Reflection, or a part of the core Java platform. Anyways, the article has been put in the Java Reflection trail in lack of a better place to put it.

The ClassLoader

All classes in a Java application are loaded using some subclass of java.lang.ClassLoader. Loading classes dynamically must therefore also be done using a java.lang.ClassLoader subclass.

When a class is loaded, all classes it references are loaded too. This class loading pattern happens recursively, until all classes needed are loaded. This may not be all classes in the application. Unreferenced classes are not loaded until the time they are referenced.

The ClassLoader Hierarchy

Class loaders in Java are organized into a hierarchy. When you create a new standard Java ClassLoaderyou must provide it with a parent ClassLoader. If a ClassLoader is asked to load a class, it will ask its parent class loader to load it. If the parent class loader can‘t find the class, the child class loader then tries to load it itself.

Class Loading

The steps a given class loader uses when loading classes are:

  1. Check if the class was already loaded.
  2. If not loaded, ask parent class loader to load the class.
  3. If parent class loader cannot load class, attempt to load it in this class loader.

When you implement a class loader that is capable of reloading classes you will need to deviate a bit from this sequence. The classes to reload should not be requested loaded by the parent class loader. More on that later.

Dynamic Class Loading

Loading a class dynamically is easy. All you need to do is to obtain a ClassLoader and call its loadClass()method. Here is an example:

public class MainClass {

  public static void main(String[] args){

    ClassLoader classLoader = MainClass.class.getClassLoader();

    try {
        Class aClass = classLoader.loadClass("com.jenkov.MyClass");
        System.out.println("aClass.getName() = " + aClass.getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

Dynamic Class Reloading

Dynamic class reloading is a bit more challenging. Java‘s builtin Class loaders always checks if a class is already loaded before loading it. Reloading the class is therefore not possible using Java‘s builtin class loaders. To reload a class you will have to implement your own ClassLoader subclass.

Even with a custom subclass of ClassLoader you have a challenge. Every loaded class needs to be linked. This is done using the ClassLoader.resolve() method. This method is final, and thus cannot be overridden in your ClassLoader subclass. The resolve() method will not allow any given ClassLoader instance to link the same class twice. Therefore, everytime you want to reload a class you must use a new instance of yourClassLoader subclass. This is not impossible, but necessary to know when designing for class reloading.

Designing your Code for Class Reloading

As stated earlier you cannot reload a class using a ClassLoader that has already loaded that class once. Therefore you will have to reload the class using a different ClassLoader instance. But this poses som new challenges.

Every class loaded in a Java application is identified by its fully qualified name (package name + class name), and the ClassLoader instance that loaded it. That means, that a class MyObject loaded by class loader A, is not the same class as the MyObject class loaded with class loader B. Look at this code:

MyObject object = (MyObject)
    myClassReloadingFactory.newInstance("com.jenkov.MyObject");

Notice how the MyObject class is referenced in the code, as the type of the object variable. This causes theMyObject class to be loaded by the same class loader that loaded the class this code is residing in.

If the myClassReloadingFactory object factory reloads the MyObject class using a different class loader than the class the above code resides in, you cannot cast the instance of the reloaded MyObject class to the MyObjecttype of the object variable. Since the two MyObject classes were loaded with different class loaders, the are regarded as different classes, even if they have the same fully qualified class name. Trying to cast an object of the one class to a reference of the other will result in a ClassCastException.

It is possible to work around this limitation but you will have to change your code in either of two ways:

  1. Use an interface as the variable type, and just reload the implementing class.
  2. Use a superclass as the variable type, and just reload a subclass.

Here are two coresponding code examples:

MyObjectInterface object = (MyObjectInterface)
    myClassReloadingFactory.newInstance("com.jenkov.MyObject");
MyObjectSuperclass object = (MyObjectSuperclass)
    myClassReloadingFactory.newInstance("com.jenkov.MyObject");

Either of these two methods will work if the type of the variable, the interface or superclass, is not reloaded when the implementing class or subclass is reloaded.

To make this work you will of course need to implement your class loader to let the interface or superclass be loaded by its parent. When your class loader is asked to load the MyObject class, it will also be asked to load the MyObjectInterface class, or the MyObjectSuperclass class, since these are referenced from within theMyObject class. Your class loader must delegate the loading of those classes to the same class loader that loaded the class containing the interface or superclass typed variables.

ClassLoader Load / Reload Example

The text above has contained a lot of talk. Let‘s look at a simple example. Below is an example of a simpleClassLoader subclass. Notice how it delegates class loading to its parent except for the one class it is intended to be able to reload. If the loading of this class is delegated to the parent class loader, it cannot be reloaded later. Remember, a class can only be loaded once by the same ClassLoader instance.

As said earlier, this is just an example that serves to show you the basics of a ClassLoader‘s behaviour. It is not a production ready template for your own class loaders. Your own class loaders should probably not be limited to a single class, but a collection of classes that you know you will need to reload. In addition, you should probably not hardcode the class paths either.

public class MyClassLoader extends ClassLoader{

    public MyClassLoader(ClassLoader parent) {
        super(parent);
    }

    public Class loadClass(String name) throws ClassNotFoundException {
        if(!"reflection.MyObject".equals(name))
                return super.loadClass(name);

        try {
            String url = "file:C:/data/projects/tutorials/web/WEB-INF/" +
                            "classes/reflection/MyObject.class";
            URL myUrl = new URL(url);
            URLConnection connection = myUrl.openConnection();
            InputStream input = connection.getInputStream();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int data = input.read();

            while(data != -1){
                buffer.write(data);
                data = input.read();
            }

            input.close();

            byte[] classData = buffer.toByteArray();

            return defineClass("reflection.MyObject",
                    classData, 0, classData.length);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}

Below is an example use of the MyClassLoader.

public static void main(String[] args) throws
    ClassNotFoundException,
    IllegalAccessException,
    InstantiationException {

    ClassLoader parentClassLoader = MyClassLoader.class.getClassLoader();
    MyClassLoader classLoader = new MyClassLoader(parentClassLoader);
    Class myObjectClass = classLoader.loadClass("reflection.MyObject");

    AnInterface2       object1 =
            (AnInterface2) myObjectClass.newInstance();

    MyObjectSuperClass object2 =
            (MyObjectSuperClass) myObjectClass.newInstance();

    //create new class loader so classes can be reloaded.
    classLoader = new MyClassLoader(parentClassLoader);
    myObjectClass = classLoader.loadClass("reflection.MyObject");

    object1 = (AnInterface2)       myObjectClass.newInstance();
    object2 = (MyObjectSuperClass) myObjectClass.newInstance();

}

Here is the reflection.MyObject class that is loaded using the class loader. Notice how it both extends a superclass and implements an interface. This is just for the sake of the example. In your own code you would only have to one of the two - extend or implement.

public class MyObject extends MyObjectSuperClass implements AnInterface2{
    //... body of class ... override superclass methods
    //    or implement interface methods
}

转自:http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html#classloader
时间: 2024-10-03 04:23:50

Java_动态加载类(英文)的相关文章

[javaSE] 反射-动态加载类

Class.forName(“类的全称”) ①不仅表示了类的类类型,还代表了动态加载类 ②请大家区分编译,运行 ③编译时刻加载类是静态加载类,运行时刻加载类是动态加载类 Ⅰ所有的new对象都是静态加载类 在编译的时刻就要去检测该类是否存在,如果不存在,编译失败. //对于这种情况,静态加载不适用,因为我们需要根据输入来确定加载哪个类 package com.tsh.reflect; class ReflectLoadDemo { public static void main(String[]

Java-反射之动态加载类

在Java当中,加载类分为动态加载和静态加载,其中,在编译时刻加载类叫做静态加载类,在运行时刻加载类叫做动态加载类. 产生Class对象的方式中,有一个是Class.forName("类的全称"),这个不仅仅表示类的类类型,而且还表示了动态加载类. 1 package com.example.demo; 2 3 public class Demo { 4 public static void main(String[] args) { 5 if (args[0].equals(&quo

java 反射机制与动态加载类学习要点

获取Class类的对象: 假设Foo是一个类,Foo foo = new Foo():则 第一种:Class c1 = Foo.class; 第二种:Class c2 = foo.getClass(); 第三种:Class c3 = Class.forName("com.nudt.reflection.Foo"); //会抛出异常 此时  c1 == c2 == c3 为true 也可以通过c1\c2\c3创建Foo的实例: Foo foo = (Foo)c1.newInstance(

Class类动态加载类的用法

编译时刻加载类出现的问题:一个功能有错,所有功能都用不了 动态加载类:

tomcat 5.5 动态加载类

转载于:http://www.itxuexiwang.com/a/javadianzishu/tomcat/2016/0225/161.html?1456480735 开发使用的是tomcat5.5.27,对于WEB-INF/classes下面的类做了修改,tomcat就会就会自动重启,然后紧接着就是内存溢出.调试比较麻烦.昨天研究了一下tomcat的源代码,对类的加载机制作了一点点修改,让它动态加载类,这样调试的时候如果修改了java文件就不用重启 tomcat了.具体步骤如下: 修改Weba

Java反射第二课 动态加载类

在第一节课中我们讲的第三种方法 Class.forName("类的全称"); 不仅表示了类的类类型,还代表了动态加载类 请大家区分编译,运行 编译时刻加载类是静态加载类,运行时刻加载类是动态加载类 现在的开发工具比如eclipse把编译过程给隐藏了 下面做一个实验 比如用记事本编写下面的Office.java文件 class Office { public static void main(String[] args) { if(("Word").equals(ar

Java--Reflect(反射)专题2——动态加载类

转载请注明:http://blog.csdn.net/uniquewonderq 首先区分什么是动态加载,什么是静态加载? Class.forName("类的全称");//第三种表达类类型的方式 不仅代表了类的类类型,还代表了动态加载类 要区分编译和运行. 编译时刻加载类是静态加载类.运行时刻加载类是动态加载类. 在编译时刻就需要加载所有的可能使用到的类. 演示代码如下:功能性的类,最好使用动态加载.如,更新,升级,增加新功能等. 首先是一个接口,用于动态加载的.稍候可体验出来. pa

java动态加载类和静态加载类笔记

JAVA中的静态加载类是编译时刻加载类  动态加载类指的是运行时刻加载类 二者有什么区别呢 举一个例子  现在我创建了一个类  实现的功能假设为通过传入的参数调用具体的类和方法 class office { public static void main(String args[]) { if("word".equals(args[0]) { word w=new word(); word.run(); } if("excel".equals(args[0]) {

Java 从Jar文件中动态加载类

由于开发的需要,需要根据配置动态加载类,所以简单测试了一下JAVA动态加载类 定义接口 package loader; public interface HelloIface {     public String hello();          public String sayHi(); } 实现接口 在其他插件类实现此接口,并导出为jar,如D:/tmp/test.jar package loader; public class HelloImpl implements HelloIf