Document directory
- Classloader is mainly responsible for the work
- Classloader's find Class Method
- Classloader category
- Context classloader
- If childfirst is used, the search path is:
Java classloader Review
Take Sun JDK as an Example
Classloader is mainly responsible for the work
Find class: Find the class
Links class: class and File Link
Define class: converts a binary file to an instance of the class.
Classloader's find Class Method
- Findloadedclass: First, check whether it has been loaded by the current classloader.
- Parent. loadclass: Call the parent loader to load data. If there is no parent loader, call the native findbootstrapclass * method to obtain the data.
- Findclass: If none of the above is found, call the current findclass
Class c = findLoadedClass(name); if (c == null) { try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClass0(name); } } catch (ClassNotFoundException e) { // If still not found, then invoke findClass in order // to find the class. c = findClass(name); } }
Classloader category
- Bootstraploader: Native. It is started after JVM is started. It is responsible for loading the core lib/class (including the following two classloader) in JRE ).
- Extclassloader: loaded by bootstraploader, responsible for loading the ext package in JRE, parent = NULL
- Appclassloader: loaded by bootstraploader, responsible for loading classes in classpath, parent = extclassloader
- Userclassloader: Custom classloader, specifying the resource location through URL
Context classloader
Implement the requirement for parent classloader to load the class in the sub-classloader URL
Jetty classloader
Webappclassloader. loadclass:
1. Find the existing class
Class<?> c= findLoadedClass(name);
2. If parentfirst or system_class is not server_class, parentfirst is used.
if (c == null && _parent!=null && (_context.isParentLoaderPriority() || system_class) && !server_class) { tried_parent= true; try { c= _parent.loadClass(name); if (Log.isDebugEnabled()) Log.debug("loaded " + c); } catch (ClassNotFoundException e) { ex= e; } }
3. Otherwise, the current findclass is called.
if (c == null) { try { c= this.findClass(name); } catch (ClassNotFoundException e) { ex= e; } }
4. Find the current Trojan and find the parent.
if (c == null && _parent!=null && !tried_parent && !server_class ) c= _parent.loadClass(name);
5. Resolve
if (resolve) resolveClass(c);
If childfirst is used, the search path is:
Webappclassloader --> bootstraploader --> extclassloader --> appclassloader