2. explain in detail classloader
3. To achieve a simple hot deployment
1. What is hot deployment
What is hot deployment: without restarting the java virtual machine automatically detects the change class file and update class runtime behavior
2. explain in detail classloader
ClassLoader role:
1. The class is loaded into the JVM2. Review each class who is responsible for loading
3. The class byte code to re-parse the JVM uniform format requirements of an object
ClassLoader Categories:
1. Start the class loader
2. extension classloader
3. The system class loader
4. Custom loader
Class loading process:
.class loaded into the jvm, the raw material into the plant
How virtual machine to load these class files?
Loading .class .class verify whether the specifications jvm -> Preparation: allocate memory for static class variables, and initialization Default -> Analytical -> Initialization: in the correct values for the static class variable
The loading mechanism Classloader:
boot class loader: bootstrap classcloader
effect: loading java core library
extension class loader: Ext classloader
effect: loading jdkhome / lib / ext extension package and can add custom: :-D java.ext.dirs = ext directory specified
application loader (the system class loader): app classloader
effect: responsible for loading classes in the directory or jar package java classpath
Custom loader (which may have N): Inheritance java.lang.ClassLoader
Parents delegation model: son find his father, his father looking grandfather
Application Loader app classloader -> extension class loader Ext classloader -> Start class loader bootstarp classloader If the boot loader can not find the class: Start class loader -> extension class loader -> Application Loader a layer layer returns to the
Custom java.lang.String can not print?
the java.lang Package;
public class String {
public static void main (String [] args) {
System.out.println ( "Hello World");
}
}
can not be printed, is first loaded up one level will find loader , found that when found start class loader java core classes have class, so when the run is running is the core of the String class, the core class String is not the main method, on the incorrect report:
Exception in thread "main" java.lang.NoSuchMethodException: java.lang.String.main ([Ljava.lang.String;) why not run, find it in the top class loaded
Parent delegation model benefits:
1. Security: Protecting core java class can not be covered
2. Unity: With priority
For a fully qualified name of the java class can only be loaded once, the need to use custom class loaderhow jvm determines whether two identical class:
1. Analyzing two fully qualified class name name is tantamount +
2. determine whether the same embodiment of a class loader to load
3. To achieve a simple hot deployment
/ **
* the Created by YZ ON 2018/2/7.
* Custom class loader
* /
public class MyClassLoader the extends ClassLoader {
/ **
* + override inherited methods the Insert the Alt
* @param name: package name + class name path
* @return
* @throws a ClassNotFoundException
* /
@Override
protected class <?> the findClass (String name) throws a ClassNotFoundException {
convert the passed in // java class file into a file
String fileName = name.substring (name.lastIndexOf ( " . . ") + 1'd) +" class ";
. this.getClass the InputStream IS = () the getResourceAsStream (fileName);
the try {
byte [] = new new byte B [is.available ()];
is.read (B);
return defineClass(name,b,0,b.length);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return super.findClass(name);
}
}
/**
* Created by yz on 2018/2/7.*/
public class HelloService {
public void sayHello(){
System.out.println("我是hello world version 2");
}
}
/ **
* the Created by YZ ON 2018/2/7.
* Test principle hot deployment
* /
public class the App {
public static void main (String [] args) throws Exception {
loadHelo ();
// replace the dynamic class
System.gc (); // release the current class file jvm occupied since become loaded into the running, so to release
Thread.sleep (1000);
File = newFile new new File ( "C: \\ \\ Desktop YZ the Users \\ \\ \\ NioDemo HelloService.class hostwap-Demo \\");
newFile.renameTo (Old); // new replacing the old document file
loadHelo (); // reloaded
}
/ **
* using a custom class loader
* @throws Exception
* /
public static void loadHelo () throws Exception {
MyClassLoader new new MyClassLoader Loader = ();
class < ?> = clazz loader.findClass ( "com.yz.hostwap.demo.HelloService");
// by reflectively
Object = clazz.newInstance Object ();
Method, Method clazz.getMethod = ( "the sayHello");
Method. Invoke (Object);
System.out.println(object.getClass()+" "+object.getClass().getClassLoader());
}
}
Hot deployment principle is applied:
Tomcat jsp加载:org.apache.jasper.servlet.JasperLoader