zoukankan      html  css  js  c++  java
  • 类加载器的命名空间

    同一个命名空间中,类只加载一份

    AppClassLoader加载程序中自定义的类。无论加载多少次,只要是被AppClassLoader加载的,其Class信息hashcode都是相同的。

    子加载器可见父加载器加载的类

    到处都是例子。比如核心类库的类,AppClassLoader所加载的类,都能使用String

    父加载器不可见子加载器所加载的类,只能验证加载不到

    应用中有Parent类,Son类。Parent类中有个getSon方法中,使用Son类。
    编译后,在classpath下,移除Son类到子加载器的加载目录中,让AppClassLoader无法加载。
    达到我们的运行环境要求:Parent由父加载器加载,Son由子加载器加载.
    调用Parent的getSon方法的时候要new Son类,会触发Son类的加载,但是加载不到Son类,报错信息为:java.lang.NoClassDefFoundError: com/rock/Son

    public class Parent {
    
        private Object son;
    
        public Object getSon(){
            return new Son();
        }
        public Object setSon(Object son){
            this.son = son;
            return this.son;
        }
    }  
    public class Son {
    }
    
    /**
         * 父加载器加载不了,子加载器所加载的类,
         * 父加载器加载Parent
         * 子加载器加载son
         * Parent#getSon 方法里 new Son()对象.//报错,找不到Son的类定义.
         */
    
    
        
    不同命名空间的互相不可见

    自定义类加载器的多个实例,分别加载Man类,并反射实例化对象,对象之间互相调用setFather赋值,会报错。a空间的不识别b空间的,即使全限定名相同;但也不识别。

    public class Man {
        private Man father;
        public void setFather(Object obj){
            father = (Man)obj;
        }
    }
    
     
    class1 : com.rock.classLoader.CustomClassLoader01@1f28c152
    class1 : 2006034581
    class2 : com.rock.classLoader.CustomClassLoader01@1f28c152
    class2 : 488044861
    ...
    Caused by: java.lang.ClassCastException: com.rock.Man cannot be cast to com.rock.Man
        at com.rock.Man.setFather(Man.java:6)
        ... 27 more
    



  • 相关阅读:
    HBuilder手机Iphone运行提示“未受信用的企业级开发者”
    在阿里云服务器ubuntu14.04运行netcore
    微信图片上传
    一段sql的优化
    设计模式之单例模式(Singleton)
    PDF.NET+EasyUI实现只更新修改的字段
    操作系统进程调度之分时,优先,分时优先
    2020最新Servlet+form表单实现文件上传(图片)
    Php7+Mysql8实现简单的网页聊天室功能
    JavaSwing+Mysql实现简单的登录界面+用户是否存在验证
  • 原文地址:https://www.cnblogs.com/lanqingzhou/p/13602419.html
Copyright © 2011-2022 走看看