自定义类加载器

Dog.java

public class Dog {
    public Dog(){
        System.out.println("Dog    is loaded by:" + this.getClass().getClassLoader());
    }
}
View Code

Sample.java

public class Sample {
    private int v1 = 1;
    public Sample(){
        System.out.println("Sample is loaded by:" + this.getClass().getClassLoader());
        new Dog();
    }
}
View Code

MyClassLoader.java

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class MyClassLoader extends ClassLoader {
    private String name;

    private String path = "f:\\";

    private static final String fileType = ".class";

    public MyClassLoader(String name) {
        // 系统类加载器为该类的父类加载器
        super();
        this.name = name;
    }

    public MyClassLoader(ClassLoader parent, String name) {
        // 明确自己的父类加载器
        super(parent);
        this.name = name;
    }

    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] b = loadClassData(name);
        return defineClass(name, b, 0, b.length);
    }

    private byte[] loadClassData(String name) {
        InputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;

        this.name = this.name.replace(".", "\\");
        try {
            is = new FileInputStream(new File(path + name + fileType));

            baos = new ByteArrayOutputStream();

            int ch = 0;

            while (-1 != (ch = is.read())) {
                baos.write(ch);
            }
            data = baos.toByteArray();
            return data;

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                is.close();
                baos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        System.out.println("读取字节码失败。");
        return null;
    }

    public static void main(String[] args) throws Exception {
        MyClassLoader loader1 = new MyClassLoader("loader1");
        loader1
                .setPath("F:\\kuaipan\\work\\J2SE_workspace\\custom_classloader\\serverlib\\");

        MyClassLoader loader2 = new MyClassLoader(loader1, "loader2");
        loader2
                .setPath("F:\\kuaipan\\work\\J2SE_workspace\\custom_classloader\\clientlib\\");

        //父加载器为根加载器
        MyClassLoader loader3 = new MyClassLoader(null, "loader3");
        loader3
                .setPath("F:\\kuaipan\\work\\J2SE_workspace\\custom_classloader\\otherlib\\");

        test(loader2);
        test(loader3);
        
        Class objClass = loader1.loadClass("Sample");
        Object obj = objClass.newInstance();
    }

    public static void test(ClassLoader loader) throws Exception {
        Class classType = loader.loadClass("Sample");
        Object object = classType.newInstance();
    }
    

    public String toString() {
        return this.name;
    }

    public String getPath() {
        return path;
    }


    public void setPath(String path) {
        this.path = path;
    }

}
View Code

 
为了测试简单,以上三个文件都放在 default package下面
 
 
 
 
 
 

 

posted on 2013-06-11 16:26  lovebeauty  阅读(221)  评论(0编辑  收藏  举报

导航