类加载器
java类加载器就是在运行时在JVM中动态地加载所需的类,java类加载器基于三个机制:委托,可见,单一。
把classpath下的那些.class文件加载进内存,处理后成为字节码,这些工作是类加载器做的。
默认类加载器
系统默认三个类加载器:
类加载器也是java类,而BootStrap不是。 验证代码:
public class ClassLoaderTest { public static void main(String[] args) { System.out.println(System.class.getClassLoader()); } }
输出:null
如果使用System.out.println(System.class.getClassLoader().toString);,则报空指针异常:
Exception in thread "main" java.lang.NullPointerException at com.iot.classloader.ClassLoaderTest.main(ClassLoaderTest.java:10) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
可见,System类是由BootStrap类加载器加载。
类加载器的委托机制
类加载器的树状图
类加载器
一般加载类的顺序:
自定义类加载器的编写原理
API:
Class ClassLoader
模板方法设计模式
父类:
loadClass(类加载的流程,模板)
findClass供子类覆盖的、被loadClass方法调用的类加载逻辑
defineClass得到class文件转换成字节码
子类:覆盖findClass方法
例子:
loadClass方法的源码
protected Class<&#63;> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // First, check if the class has already been loaded Class<&#63;> 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; } }
API文档中的例子:
class NetworkClassLoader extends ClassLoader { String host; int port; public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { // load the class data from the connection . . . } }