自定义类加载器只重写的findClass,打破了双亲委派

protected Class<?> findClass(String name) throws ClassNotFoundException  {
		Class<?> findClass = null;		
		try {
			byte[] byteArray = getByteArray();
			findClass = this.defineClass(name,byteArray, 0, byteArray.length);				
		} catch (IOException e) {
			e.printStackTrace();
		}							
		return findClass;
	}

自定义类加载器只重写了findClass

 

public static void main(String[] args) {		
		try {		
			Class<?> class1 = Class.forName("com.eplugger.person");		
			myClassLoader myClassLoader = new myClassLoader();				
			Class<?> class2 = myClassLoader.findClass("com.eplugger.person");
			System.out.println(class1 == class2);
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

测试类

 

[Loaded com.eplugger.myClassLoader from file:/F:/Eclipse-Eplugger/ClassLoader/bin/]
[Loaded com.eplugger.person from __JVM_DefineClass__]
false

输出结果

 

为什么能加载两次person,不应该只加载一次吗

你猜为啥classLoader需要这个东西?

img