22- namespace class loader depth analysis and Case Study

Namespace class loader depth analysis and Case Study

Namespace relationship different class loader:

  • Classes in the same namespace are mutually visible.
  • Namespace child loader namespace contains all of the parent class loader. Thus by the sub-loaded by the class loader loads you can see the parent class. For example the system class loader to load a class can see the root class loader to load a class.
  • Loaded by the class loader can not see the parent child class loader loads.
  • If there is no direct or indirect parent-child relationship between two loaders, they are each loaded class is not visible to each other.

Example one:

public class MyPerson {
    private MyPerson myPerson;
    public void setMyPerson(Object object) {
        this.myPerson = (MyPerson)object;
    }
}

public class MyTest20 {
    public static void main(String[] args) throws Exception {
        //MyTest16是一个自定义类加载器
        MyTest16 loader1 = new MyTest16("loader1");
        MyTest16 loader2 = new MyTest16("loader2");

        Class<?> clazz1 = loader1.loadClass("Jvm.MyPerson");
        Class<?> clazz2 = loader2.loadClass("Jvm.MyPerson");

        System.out.println(clazz1==clazz2);

        Object object1 = clazz1.newInstance();
        Object object2 = clazz2.newInstance();

        Method method = clazz1.getMethod("setMyPerson",Object.class);
        method.invoke(object1,object2);
    }
}
运行结果:
	true
  • First, print the result is: System.out.println (clazz1 == clazz2); to load because loader1, loader2 to load MyPerson when parents are commissioned by the mechanism to request its parent class loader for the first time loader1 request parent class loader (the system class loader) to complete the loading; loader2 when the second load, the parent class loader request (system class loader) to load found MyPerson has been loaded, it is no longer reloaded , the result can be loaded directly back.

Example 2:

//前提:删除out文件夹中的MyPerson.class,并将其复制到桌面上去。
public class MyTest21 {
    public static void main(String[] args) throws Exception {
        MyTest16 loader1 = new MyTest16("loader1");
        MyTest16 loader2 = new MyTest16("loader2");
        loader1.setPath("C:\\Users\\admin\\Desktop\\");
        loader2.setPath("C:\\Users\\admin\\Desktop\\");
        Class<?> clazz1 = loader1.loadClass("Jvm.MyPerson");
        Class<?> clazz2 = loader2.loadClass("Jvm.MyPerson");

        System.out.println(clazz1==clazz2);

        Object object1 = clazz1.newInstance();
        Object object2 = clazz2.newInstance();

        Method method = clazz1.getMethod("setMyPerson",Object.class);
        method.invoke(object1,object2);
    }
}
运行结果:
   findClass invoked:Jvm.MyPerson
   class loader name:loader1
   findClass invoked:Jvm.MyPerson
   class loader name:loader2
   -------------------
   false
   Exception in thread "main" java.lang.reflect.InvocationTargetException
   Caused by: java.lang.ClassCastException: Jvm.MyPerson cannot be cast to Jvm.MyPerson
  • The reason for the false: ** If there is no direct or indirect parent-child relationship between two loaders, they are each loaded class is not visible to each other. ** and so loader1 loader2 are two different class loaders (different class loaders have different namespaces, however, the two different namespaces have the same class MyPerson).
Published 25 original articles · won praise 0 · Views 1451

Guess you like

Origin blog.csdn.net/qq_40574305/article/details/104793474