关getClass().getClassLoader()
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 the
object that is locked by static synchronized methods of the
represented class.
Returns:
the object of type Class that represents the runtime class of the
object.
getClassLoader
public ClassLoader getClassLoader()
Returns the class loader for the class. Some implementations may
use null to represent the bootstrap class loader. This method will
return null in such implementations if this class was loaded by the
bootstrap class loader.
If a security manager is present, and the caller´s class loader is
not null and the caller´s class loader is not the same as or an
ancestor of the class loader for the class whose class loader is
requested, then this method calls the security manager´s
checkPermission method with a RuntimePermission("getClassLoader")
permission to ensure it´s ok to access the class loader for the
class.
If this object represents a primitive type or void, null is
returned.
Returns:
the class loader that loaded the class or interface represented by
this object.
Throws:
SecurityException - if a security manager exists and its
checkPermission method denies access to the class loader for the
class.
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 may
use
* null to represent the bootstrap class loader. This method will
return
* null in such implementations if this class was loaded by the
bootstrap
* class loader.
*
* <p> If a security manager is
present, and the caller's class loader is
* not null and the caller's class loader is not the same as or an
ancestor 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 the
class.
*
* <p>If this object
* represents a primitive type or void, null is returned.
.....
上面的英文可以用下面的话来理解:
装载类的过程非常简单:查找类所在位置,并将找到的Java类的字节码装入内存,生成对应的Class对象。Java的类装载器专门用来实现这样的过程,JVM并不止有一个类装载器,事实上,如果你愿意的话,你可以让JVM拥有无数个类装载器,当然这除了测试JVM外,我想不出还有其他的用途。你应该已经发现到了这样一个问题,类装载器自身也是一个类,它也需要被装载到内存中来,那么这些类装载器由谁来装载呢,总得有个根吧?没错,确实存在这样的根,它就是神龙见首不见尾的Bootstrap
ClassLoader.
为什么说它神龙见首不见尾呢,因为你根本无法在Java代码中抓住哪怕是它的一点点的尾巴,尽管你能时时刻刻体会到它的存在,因为java的运行环境所需要的所有类库,都由它来装载,而它本身是C++写的程序,可以独立运行,可以说是JVM的运行起点,伟大吧。在Bootstrap完成它的任务后,会生成一个AppClassLoader(实际上之前系统还会使用扩展类装载器ExtClassLoader,它用于装载Java运行环境扩展包中的类),这个类装载器才是我们经常使用的,可以调用ClassLoader.getSystemClassLoader()
来获得,我们假定程序中没有使用类装载器相关操作设定或者自定义新的类装载器,那么我们编写的所有java类通通会由它来装载,值得尊敬吧。AppClassLoader查找类的区域就是耳熟能详的Classpath,也是初学者必须跨过的门槛,有没有灵光一闪的感觉,我们按照它的类查找范围给它取名为类路径类装载器。还是先前假定的情况,当Java中出现新的类,AppClassLoader首先在类传递给它的父类类装载器,也就是Extion
ClassLoader,询问它是否能够装载该类,如果能,那AppClassLoader就不干这活了,同样Extion
ClassLoader在装载时,也会先问问它的父类装载器。我们可以看出类装载器实际上是一个树状的结构图,每个类装载器有自己的父亲,类装载器在装载类时,总是先让自己的父类装载器装载(多么尊敬长辈),如果父类装载器无法装载该类时,自己就会动手装载,如果它也装载不了,那么对不起,它会大喊一声:Exception,class
not
found。有必要提一句,当由直接使用类路径装载器装载类失败抛出的是NoClassDefFoundException异常。如果使用自定义的类装载器loadClass方法或者ClassLoader的findSystemClass方法装载类,如果你不去刻意改变,那么抛出的是ClassNotFoundException。
这里jdk告诉我们:如果一个类是通过bootstrap
载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的,比如说我用 new
Object().getClass().getClassLoader()的话,会返回一个null,这样的话上面的代码就会出现NullPointer异常.所以保险起见我们最好还是使用我们自己写的类来获取classloader("this.getClass().getClassLoader()“),这样一来就不会有问题。
关getClass().getClassLoader()的更多相关文章
- 关于getClass().getClassLoader()
关于getClass().getClassLoader() InputStream is = getClass().getClassLoader().getResourceAsStre ...
- 理解根目录,classpath, getClass().getResourceAsStream和getClass().getClassLoader().getResourceAsStream的区别
一: 理解根目录 <value>classpath*:/application.properties</value> <value>classpath:/appli ...
- java-关于getClass().getClassLoader()
源地址:http://blog.sina.com.cn/s/blog_6ec6be0e01011xof.html InputStream is = getClass().getClassLoader( ...
- 关于obj.class.getResource()和obj.getClass().getClassLoader().getResource()的路径问题
感谢原文作者:yejg1212 原文链接:https://www.cnblogs.com/yejg1212/p/3270152.html 注:格式内容与原文有轻微不同. Java中取资源时,经常用到C ...
- How to call getClass() from a static method in Java?
刚才在学习Java 使用properties类,遇到这样的错误: Cannot make a static reference to the non-static method getClass() ...
- Is it always safe to call getClass() within the subclass constructor?(转)
14down votefavorite An article on classloading states that the method getClass() should not be cal ...
- Maven项目读取resources下文件的路径问题(getClassLoader的作用)
读取resources下文件的方法 网上有问答如下:问: new FileInputStream("src/main/resources/all.properties") new ...
- 比较Class.getResource与Class.getClassLoader().getResource两种方式读取资源文件
/** * @author zhangboqing * @date 2018/7/10 */ public class FileDemo { public static void main(Strin ...
- this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream 文件地址获取
本文部分转自:http://xixinfei.iteye.com/blog/1256291 this.getClass().getClassLoader().getResource("tem ...
随机推荐
- Win7安装MySQL-5.7.16过程
1.在C盘新建MYSQL文件夹:2.将mysql-5.7.16-winx64拷贝到C:\MYSQL文件夹下,更名为mysql-5.7.16:3.在mysql-5.7.16目录下,建my.ini文件,内 ...
- Ognl表达式基本原理和使用方法
Ognl表达式基本原理和使用方法 1.Ognl表达式语言 1.1.概述 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,他是一个 ...
- Android(1)—Mono For Android 环境搭建及破解
0.前言 最近公司打算开发一款Android平台的简单报表查询软件,因本人之前一直是.NET开发的,和领导商定之后决定采用Mono For Android 进行开发,暂时采用破解版进行开发: 下文是记 ...
- Vue.js——使用$.ajax和vue-resource实现OAuth的注册、登录、注销和API调用
概述 上一篇我们介绍了如何使用vue resource处理HTTP请求,结合服务端的REST API,就能够很容易地构建一个增删查改应用.这个应用始终遗留了一个问题,Web App在访问REST AP ...
- SAE+WordPress快速搭建个人博客
前些天一时冲动,买了个域名,我想总不能放着不用吧,干脆就搭建了一个个人博客.下面我把搭建的过程分享给大家.注意,此文并不是攻略,只是为了记录下这个从无到有的过程,当然,假如解决了你的疑惑,那当然是极好 ...
- C# BS消息推送 SignalR介绍(一)
1. 前言 本文是根据网上前人的总结得出的. 环境: SignalR2.x,VS2015,Win10 介绍 1)SignalR能用来持久客户端与服务端的连接,让我们便于开发一些实时的应用,例如聊天室在 ...
- 【PRINCE2是什么】PRINCE2认证之七大主题(1)
进入第一个主题,PRINCE2商业论证:PRINCE2指出,商业论证就是进行判断是否值得对项目进行投资,值不值的问题.PRINCE2的商业论证有四个 在项目开始时,开发商业论证.在整个项目生命周期中, ...
- 在网上摘录一段对于IOC的解析,比较直观,大家观摩观摩
其实IoC非常简单,基本思想就是面向接口的编程,只是老外给起了个名字名充分利用之. 简单的说,传统模式下,如果你要用钱,你需要去银行取,IoC模式下,银联在你家安了一个取款机,你直接找取款机要钱就可以 ...
- Android的编码规范
一.Android编码规范 1.学会使用string.xml文件 在我看来,当一个文本信息出现的次数大于一次的时候就必须要使用string.xml 比如一个保存按钮 , 不规范写法: <Butt ...
- WCF学习之旅—HTTP双工模式(二十)
WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...