customizeClassLoader = new CustomizeClassLoader(); Class clazz1 = customizeClassLoader.loadClass("com.shifpeng.scaffold.service.jvm.dao"); System.out.println(clazz1.hashCode());
// @Override public Class<?> loadClass(String name) throws ClassNotFoundException { //首先去找要load的文件,如果没有找到,就让父亲去load,如果找到了就自己load //我这里没去找是不是加载过,反正只要拿到类明,我就去加载 //如果要加载同一个Class是覆盖不了的,所以我直接把ClassLoader干掉就行了 File f = new File("/Users/Steven/test/" + name.replace(".", "/").concat(".class"));
if (!f.exists()) returnsuper.loadClass(name); try { InputStream is = new FileInputStream(f); byte[] b = newbyte[is.available()]; is.read(b); return defineClass(name, b, 0, b.length); } catch (IOException e) { e.printStackTrace(); }
returnsuper.loadClass(name); } }
//所以tomcat就是这么干的,直接把web应用的整个ClassLoader干掉,重新再加载一遍 publicstaticvoidmain(String[] args)throws Exception { MyLoader m = new MyLoader(); Class clazz = m.loadClass("com.shifpeng.scaffold.service.jvm.dao.Hello"); //重新new MyLoader m = new MyLoader(); Class clazzNew = m.loadClass("com.shifpeng.scaffold.service.jvm.dao.Hello");