国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

關(guān)于getClass().getClassLoader()

2019-11-10 20:41:56
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

InputStream   is   =   getClass().getClassLoader().getResourceAsStream("helloworld.PRoperties");中g(shù)etClass()和getClassLoader()都是什么意思呀.getClass():取得當(dāng)前對(duì)象所屬的Class對(duì)象  getClassLoader():取得該Class對(duì)象的類裝載器類裝載器負(fù)責(zé)從java字符文件將字符流讀入內(nèi)存,并構(gòu)造Class類對(duì)象,在你說(shuō)的問(wèn)題哪里,通過(guò)它可以得到一個(gè)文件的輸入流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.

getClassLoaderpublic 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), RuntimePermissionClass.getClassLoader()的一個(gè)小陷阱:)昨天我的code總在Integer.class.getClassLoader().getResource("*********");這一句拋出空指針異常,定位為getClassLoader()返回null,查了一下jdk的文檔,原來(lái)這里還有一個(gè)陷阱:jdk中關(guān)于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......

上面的英文可以用下面的話來(lái)理解:

裝載類的過(guò)程非常簡(jiǎn)單:查找類所在位置,并將找到的Java類的字節(jié)碼裝入內(nèi)存,生成對(duì)應(yīng)的Class對(duì)象。Java的類裝載器專門用來(lái)實(shí)現(xiàn)這樣的過(guò)程,JVM并不止有一個(gè)類裝載器,事實(shí)上,如果你愿意的話,你可以讓JVM擁有無(wú)數(shù)個(gè)類裝載器,當(dāng)然這除了測(cè)試JVM外,我想不出還有其他的用途。你應(yīng)該已經(jīng)發(fā)現(xiàn)到了這樣一個(gè)問(wèn)題,類裝載器自身也是一個(gè)類,它也需要被裝載到內(nèi)存中來(lái),那么這些類裝載器由誰(shuí)來(lái)裝載呢,總得有個(gè)根吧?沒(méi)錯(cuò),確實(shí)存在這樣的根,它就是神龍見(jiàn)首不見(jiàn)尾的Bootstrap ClassLoader. 為什么說(shuō)它神龍見(jiàn)首不見(jiàn)尾呢,因?yàn)槟愀緹o(wú)法在Java代碼中抓住哪怕是它的一點(diǎn)點(diǎn)的尾巴,盡管你能時(shí)時(shí)刻刻體會(huì)到它的存在,因?yàn)閖ava的運(yùn)行環(huán)境所需要的所有類庫(kù),都由它來(lái)裝載,而它本身是C++寫(xiě)的程序,可以獨(dú)立運(yùn)行,可以說(shuō)是JVM的運(yùn)行起點(diǎn),偉大吧。在Bootstrap完成它的任務(wù)后,會(huì)生成一個(gè)AppClassLoader(實(shí)際上之前系統(tǒng)還會(huì)使用擴(kuò)展類裝載器ExtClassLoader,它用于裝載Java運(yùn)行環(huán)境擴(kuò)展包中的類),這個(gè)類裝載器才是我們經(jīng)常使用的,可以調(diào)用ClassLoader.getSystemClassLoader() 來(lái)獲得,我們假定程序中沒(méi)有使用類裝載器相關(guān)操作設(shè)定或者自定義新的類裝載器,那么我們編寫(xiě)的所有java類通通會(huì)由它來(lái)裝載,值得尊敬吧。AppClassLoader查找類的區(qū)域就是耳熟能詳?shù)腃lasspath,也是初學(xué)者必須跨過(guò)的門檻,有沒(méi)有靈光一閃的感覺(jué),我們按照它的類查找范圍給它取名為類路徑類裝載器。還是先前假定的情況,當(dāng)Java中出現(xiàn)新的類,AppClassLoader首先在類傳遞給它的父類類裝載器,也就是Extion ClassLoader,詢問(wèn)它是否能夠裝載該類,如果能,那AppClassLoader就不干這活了,同樣Extion ClassLoader在裝載時(shí),也會(huì)先問(wèn)問(wèn)它的父類裝載器。我們可以看出類裝載器實(shí)際上是一個(gè)樹(shù)狀的結(jié)構(gòu)圖,每個(gè)類裝載器有自己的父親,類裝載器在裝載類時(shí),總是先讓自己的父類裝載器裝載(多么尊敬長(zhǎng)輩),如果父類裝載器無(wú)法裝載該類時(shí),自己就會(huì)動(dòng)手裝載,如果它也裝載不了,那么對(duì)不起,它會(huì)大喊一聲:Exception,class not found。有必要提一句,當(dāng)由直接使用類路徑裝載器裝載類失敗拋出的是NoClassDefFoundException異常。如果使用自定義的類裝載器loadClass方法或者ClassLoader的findSystemClass方法裝載類,如果你不去刻意改變,那么拋出的是ClassNotFoundException。

這里jdk告訴我們:如果一個(gè)類是通過(guò)bootstrap 載入的,那我們通過(guò)這個(gè)類去獲得classloader的話,有些jdk的實(shí)現(xiàn)是會(huì)返回一個(gè)null的,比如說(shuō)我用 new Object().getClass().getClassLoader()的話,會(huì)返回一個(gè)null,這樣的話上面的代碼就會(huì)出現(xiàn)NullPointer異常.所以保險(xiǎn)起見(jiàn)我們最好還是使用我們自己寫(xiě)的類來(lái)獲取classloader("this.getClass().getClassLoader()“),這樣一來(lái)就不會(huì)有問(wèn)題。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 涟源市| 稷山县| 巴彦县| 西贡区| 环江| 高雄市| 英超| 绵竹市| 茌平县| 临颍县| 元阳县| 军事| 葵青区| 维西| 沧源| 和平县| 五大连池市| 榆林市| 凤庆县| 湖南省| 木兰县| 泸州市| 罗平县| 无极县| 嘉鱼县| 久治县| 芮城县| 游戏| 新郑市| 兴安盟| 浦东新区| 铜陵市| 卢湾区| 玛多县| 即墨市| 正宁县| 巩义市| 清流县| 柘城县| 富民县| 隆德县|