双亲委派模型是Java加载类的机制.采用双亲委派模型的好处是Java类随着它的类加载器一起具备了一种带有优先级的层次关系,通过这种层级关系可以避免类的重复加载.
1.双亲委派模型最大的好处就是让Java类同其类加载器一起具备了一种带优先级的层次关系。这句话可能不好理解,我们举个例子。比如我们要加载java.lang.Object类,无论我们用哪个类加载器去加载Object类,这个加载请求最终都会委托给Bootstrap ClassLoader,这样就保证了所有加载器加载的Object类都是同一个类。如果没有双亲委派模型,那就乱了套了,完全可以搞出多个不同的Object类。
2.自上而下每个类都会尽力加载.
1.首先加载类调用的loadClass方法,我们找到ClassLoader的loadClass():
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; } }
protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); }
1.首先需要一个编译好的class文件,笔者用了一个之前写的斐波那契的类Fib.class(所在路径:C:/Users/Think/crabapple),下面是用idea通过反编译方式打开的class文件,注意记下class文件的包名,在后续代码中需要使用类的全限定名称.
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package crabapple; public class Fib { public static int fib(int num) { return num < 2 ? num : fib(num - 2) + fib(num - 1); } }
2.继承ClassLoader,重写findClass方法:
class MyClassLoader extends ClassLoader { private String classPath; // 保存的地址 /** * 传入地址构造函数 * @param classPath */ public MyClassLoader(String classPath) { this.classPath = classPath; } /** * 读取class文件 * @param name * @return * @throws Exception */ private byte[] loadByte(String name) throws Exception { String inPath = classPath + "/" + name + ".class"; FileInputStream fis = new FileInputStream(inPath); int len = fis.available(); byte[] data = new byte[len]; fis.read(data); fis.close(); return data; } /** * 重写findClass方法,让加载的时候调用findClass方法 * @param name * @return * @throws ClassNotFoundException */ protected Class<?> findClass(String name) throws ClassNotFoundException { try { byte[] data = loadByte(name); // 将字节码载入内存 return defineClass(name, data, 0, data.length); } catch (Exception e) { e.printStackTrace(); } return null; } }
3.测试,一下将使用一些反射机制和class类的方法.
public class ClassLoaderTest extends ClassLoader { //main函数本该抛出异常有 ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException,为了好看,简写成Exception public static void main(String[] args) throws Exception { //初始化类加载器 MyClassLoader myClassLoader=new MyClassLoader("C:/Users/Think/crabapple"); //加载Fib类,笔者class文件包名为crabapple Class myClass=myClassLoader.loadClass("crabapple.Fib"); //获取加载类的实例 Object object=myClass.newInstance(); //获取该类一个名为fib,且参数为int的方法 Method method=myClass.getMethod("fib",int.class); //执行这个方法 int result=method.invoke(object,4); //打印结果 System.out.print(result); //output /** * 3 * Process finished with exit code 0 */ } }
如上便是笔者对双亲委派模型的总结,如有错误,欢迎指正.