关于getClass().getClassLoader()

关于getClass().getClassLoader()InputStream  is  =  getClass().getClassLoader().getResourceAsStream(“helloworld.properties”);中getClass()和getClassLoader()都是什么意思呀.getClass():取得当前对象所属的Class对象  getClassLoader():取得该Class对象的类装载器类装载器

大家好,又见面了,我是你们的朋友全栈君。

InputStream  is   getClass().getClassLoader().getResourceAsStream(“helloworld.properties”);中getClass()和getClassLoader()都是什么意思呀.
getClass():取得当前对象所属的Class对象  
getClassLoader():取得该Class对象的类装载器
类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象,在你说的问题哪里,通过它可以得到一个文件的输入流
getClass :
public final Class getClass()
Returns the runtime class of an object. That Class object is theobject that is locked by static synchronized methods of therepresented class.
Returns:
the object of type Class that represents the runtime class of theobject.

getClassLoader
public ClassLoader getClassLoader()
Returns the class loader for the class. Some implementations mayuse null to represent the bootstrap class loader. This method willreturn null in such implementations if this class was loaded by thebootstrap class loader.
If a security manager is present, and the caller´s class loader isnot null and the caller´s class loader is not the same as or anancestor of the class loader for the class whose class loader isrequested, then this method calls the security manager´scheckPermission method with a RuntimePermission(“getClassLoader”)permission to ensure it´s ok to access the class loader for theclass.

If this object represents a primitive type or void, null isreturned.

Returns:
the class loader that loaded the class or interface represented bythis object.
Throws:
SecurityException – if a security manager exists and itscheckPermission method denies access to the class loader for theclass.
See Also:
ClassLoader,SecurityManager.checkPermission(java.security.Permission),RuntimePermission

Class.getClassLoader()的一个小陷阱:)
昨天我的code总在Integer.class.getClassLoader().getResource(“*********”);这一句抛出空指针异常,定位为getClassLoader()返回null,查了一下jdk的文档,原来这里还有一个陷阱:
jdk中关于getClassLoader()的描述:
/**
    * Returns the class loader for the class. Some implementations mayuse
    * null to represent the bootstrap class loader. This method willreturn
    * null in such implementations if this class was loaded by thebootstrap
    * class loader.
    *
    * <p> If a security manager ispresent, and the caller’s class loader is
    * not null and the caller’s class loader is not the same as or anancestor of
    * the class loader for the class whose class loader is requested,then
    * this method calls the security manager’s<code>checkPermission</code>
    * method with a<code>RuntimePermission(“getClassLoader”)</code>
    * permission to ensure it’s ok to access the class loader for theclass.
    *
    * <p>If this object
    * represents a primitive type or void, null is returned.
.....

上面的英文可以用下面的话来理解:

装载类的过程非常简单:查找类所在位置,并将找到的Java类的字节码装入内存,生成对应的Class对象。Java的类装载器专门用来实现这样的过程,JVM并不止有一个类装载器,事实上,如果你愿意的话,你可以让JVM拥有无数个类装载器,当然这除了测试JVM外,我想不出还有其他的用途。你应该已经发现到了这样一个问题,类装载器自身也是一个类,它也需要被装载到内存中来,那么这些类装载器由谁来装载呢,总得有个根吧?没错,确实存在这样的根,它就是神龙见首不见尾的BootstrapClassLoader.为什么说它神龙见首不见尾呢,因为你根本无法在Java代码中抓住哪怕是它的一点点的尾巴,尽管你能时时刻刻体会到它的存在,因为java的运行环境所需要的所有类库,都由它来装载,而它本身是C++写的程序,可以独立运行,可以说是JVM的运行起点,伟大吧。在Bootstrap完成它的任务后,会生成一个AppClassLoader(实际上之前系统还会使用扩展类装载器ExtClassLoader,它用于装载Java运行环境扩展包中的类),这个类装载器才是我们经常使用的,可以调用ClassLoader.getSystemClassLoader()来获得,我们假定程序中没有使用类装载器相关操作设定或者自定义新的类装载器,那么我们编写的所有java类通通会由它来装载,值得尊敬吧。AppClassLoader查找类的区域就是耳熟能详的Classpath,也是初学者必须跨过的门槛,有没有灵光一闪的感觉,我们按照它的类查找范围给它取名为类路径类装载器。还是先前假定的情况,当Java中出现新的类,AppClassLoader首先在类传递给它的父类类装载器,也就是ExtionClassLoader,询问它是否能够装载该类,如果能,那AppClassLoader就不干这活了,同样ExtionClassLoader在装载时,也会先问问它的父类装载器。我们可以看出类装载器实际上是一个树状的结构图,每个类装载器有自己的父亲,类装载器在装载类时,总是先让自己的父类装载器装载(多么尊敬长辈),如果父类装载器无法装载该类时,自己就会动手装载,如果它也装载不了,那么对不起,它会大喊一声:Exception,classnotfound。有必要提一句,当由直接使用类路径装载器装载类失败抛出的是NoClassDefFoundException异常。如果使用自定义的类装载器loadClass方法或者ClassLoader的findSystemClass方法装载类,如果你不去刻意改变,那么抛出的是ClassNotFoundException。

这里jdk告诉我们:如果一个类是通过bootstrap载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的,比如说我用 newObject().getClass().getClassLoader()的话,会返回一个null,这样的话上面的代码就会出现NullPointer异常.所以保险起见我们最好还是使用我们自己写的类来获取classloader(”this.getClass().getClassLoader()“),这样一来就不会有问题。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/131358.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 过滤器和拦截器的区别和执行顺序图_压缩空气过滤器安装顺序

    过滤器和拦截器的区别和执行顺序图_压缩空气过滤器安装顺序一、两者的区别1、拦截器java里的拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提取action中可重用部分的方式。在AOP(Aspect-OrientedProgramming)中拦截器用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作…

  • DDD中的建模方法有哪些[通俗易懂]

    DDD中的建模方法有哪些[通俗易懂]一、背景在之前的文章中已经介绍了DDD相关的概念模式,DDD相关的业务技术架构,但是我们还没有找到一个核心的抓手去实践DDD。DDD的一个核心本质就是对业务建模,或者领域建模。说的很简单,但是做好确实很难,一个需求过来意淫几个实体对象就差不多解决了。深入看,全局看只在脑海中进行的建模实际上并不一定正确和稳定。因此我们需要找到正确的方法帮助对业务领域进行分析,得到建模结构,共享建模成果。二、四色建模法2.1起源&概念&要素关于四色建模的概念我们可与追溯到90年代,起源于四色原型。四色

  • Ubuntu 18.04 firefox浏览器装flash[通俗易懂]

    Ubuntu 18.04 firefox浏览器装flash[通俗易懂]进入http://get.adobe.com/cn/flashplayer/即flash官网(注意下载和系统浏览器相对应的包)在左边选择.tar.gz格式的文件然后解压  tar-zxvfinstall_flash_player_11_linux.x86_64.tar.gz进入解压后的usr文件夹,把libflashplayer.so文件放到路径/usr/lib/mozilla/…

  • SpringBoot入门(个人总结)

    @纯属个人学习总结,不喜勿喷哈。(学习来自慕课网)我觉得学习SpringBoot需要具备的前置知识熟悉maven构建项目;懂得Spring注解开发的知识;了解restful API的理论知识(http://www.ruanyifeng.com/blog/2011/09/restful.html)一、SpringBoot介绍我们在做java的项目中经常被xml的配置搞得头大…

    2021年11月30日
  • SDN基本概念

    SDN基本概念

  • python读取pkl_Python 读取文件

    python读取pkl_Python 读取文件使用python读取pkl文件内容可能会出现一些错误,下面将介绍一些解决的方法。importcPicklef=open(‘subj0.pkl’)#文件所在路径inf=cPickle.load(f)#读取pkl内容printinff.close()有时候,还是出现错误EOFEORROR,可以通过合并第2,3行,即:inf=cPickle.load(open(‘subj

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号