自定义classloader
MapleClassLoader
package com.maple;import java.io.*;public class MapleClassLoader extends ClassLoader { private String path = "E:\\Workspaces\\loader\\target\\classes\\"; public MapleClassLoader(String path){ this.path = path ; } public MapleClassLoader(ClassLoader parent,String path){ super(parent); this.path = path; } /*@Override protected Class findClass(String name) throws ClassNotFoundException { byte[] data = loadClassData(name); return defineClass(name,data,0,data.length); }*/ /*private byte[] loadClassData(String name) { try { name = name.replaceAll(".", "\\"); FileInputStream is = new FileInputStream(new File(path + name + ".class")); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b; while ((b = is.read()) != -1) { bos.write(b); } return bos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return null; }*/ @Override public Class loadClass(String name) throws ClassNotFoundException { try { String fileName = name.replaceAll("\\.", "\\/"); FileInputStream is; try { is = new FileInputStream(new File(path + fileName + ".class")); } catch (Exception e) { is = null; } if (is == null) { return super.loadClass(name); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b; while ((b = is.read()) != -1) { bos.write(b); } byte[] data = bos.toByteArray(); return defineClass(name, data, 0, data.length); } catch (Exception e) { throw new ClassNotFoundException(name); } }}
测试
public class Demo1 { public static void main(String[] args) throws Exception { MapleClassLoader mc = new MapleClassLoader(Thread.currentThread().getContextClassLoader(),"E:\\Workspaces\\loader\\target\\classes\\"); Object obj = (Object)mc.loadClass("com.maple.Demo1").newInstance(); System.out.println("load: "+ obj.getClass()); System.out.println("local: "+ Demo1.NUM); System.out.println(obj instanceof Demo1); } private static int NUM = 1; private static Integer addNum(){ return NUM++; }}
测试结果
load: class com.maple.Demo1local: 1false
优化版ClassLoader
package com.maple.cl;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.net.URL;import java.net.URLClassLoader;/** * App Class Loader * * @author craneding * @date 16/1/28 */public class AppClassLoader extends ClassLoader { private String path = "E:\\Workspaces\\loader\\target\\classes\\"; public AppClassLoader(String path) { super(ClassLoader.getSystemClassLoader()); this.path = path; } public AppClassLoader(ClassLoader parent,String path) { super(parent); this.path = path; } @Override public Class loadClass(String name) throws ClassNotFoundException { if (name.startsWith("com.maple.test.S") || name.startsWith("com.isuwang.org.apache.thrift") || name.startsWith("com.isuwang.dapeng.transaction.api") || name.startsWith("com.google.gson")) return ClassLoaderManager.shareClassLoader.loadClass(name); try { Class c = findLoadedClass(name); if(c==null){ String fileName = name.replaceAll("\\.", "\\/"); FileInputStream is; try { is = new FileInputStream(new File(path + fileName + ".class")); } catch (Exception e) { is = null; } if (is == null) { return super.loadClass(name); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b; while ((b = is.read()) != -1) { bos.write(b); } byte[] data = bos.toByteArray(); return defineClass(name, data, 0, data.length); } return c; } catch (Exception e) { throw new ClassNotFoundException(name); } }}