package com.zkn.newlearn.classloader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; /** * * @author zkn * */ public class ClassLoaderTest01 extends ClassLoader { /** * 路径 */ private String path = ""; /** * 后缀名 */ private String className = ".class"; private String name = ""; private ClassLoader loader = null; @Override protected Class<?> findClass(String name) { byte[] b =loadClassData(name); if(b != null && b.length > 0) return defineClass(name, b, 0, b.length);
return this.getClass(); } @SuppressWarnings("null") private byte[] loadClassData(String names) { names = names.replace(".", "\\"); String clazzName = path + names + className; InputStream is = null; ByteArrayOutputStream baos = null; byte[] bytes = new byte[0]; int i = 0; try{ is = new FileInputStream(new File(clazzName)); baos = new ByteArrayOutputStream(); while(-1!=(i = is.read())){ baos.write(i); } bytes = baos.toByteArray(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(is != null) is.close(); if(baos != null) baos.close(); }catch(Exception ee){ ee.printStackTrace(); } } return bytes; } public ClassLoaderTest01(String name) { super(); this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return " "+name; } public ClassLoaderTest01(String name, ClassLoader loader) { super(loader); this.name = name; this.loader = loader; } public static void main(String[] args) throws Exception{ ClassLoaderTest01 loader1 = new ClassLoaderTest01("loader1"); loader1.setPath("D:\\log4j\\sys\\"); ClassLoaderTest01 loader2 = new ClassLoaderTest01("loader2",loader1); loader2.setPath("D:\\log4j\\common\\"); ClassLoaderTest01 loader3 = new ClassLoaderTest01("loader3",null); loader3.setPath("D:\\log4j\\common\\"); try{ test(loader2,"com.zkn.newlearn.classloader.Parent"); }catch(Exception e){ loader2 = new ClassLoaderTest01("loader02"); loader2.setPath("D:\\log4j\\common\\"); test(loader2,"com.zkn.newlearn.classloader.Parent"); } test(loader3,"com.zkn.newlearn.classloader.Parent"); } private static void test(ClassLoader loader,String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class clazz = null; try{ clazz = loader.loadClass(name); }catch(Exception e){ e.printStackTrace(); } clazz.newInstance(); } }
package com.zkn.newlearn.classloader; /** * * @author zkn * */ public class Parent { public static int i = 1; public Parent(){ new Dog(); System.out.println("Parent类加载器为:"+Parent.class.getClassLoader()); } }
package com.zkn.newlearn.classloader; /** * * @author pc * */ public class Dog { public Dog(){ System.out.println("Dog 类 : 类加载器为--"+ Dog.class.getClassLoader()); } }
ClassLoaderTest01在sys下面:
1、如果Parent类在common下面,由子类加载,即loader2来加载。
<img src="http://img.blog.csdn.net/20160122230640800?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
2、如果Parent类在sys下面
由父类加载器加载,即loader1来加载。
<img src="http://img.blog.csdn.net/20160122230703769?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
如果Parent在系统目录下
<img src="http://img.blog.csdn.net/20160122230726426?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
对于loader3,无论Parent类在哪目录下,Parent类始终由loader3来加载。
时间: 2024-09-26 02:30:38