娣卞叆鎺㈢┒JVM涔嬬被鍔犺浇涓庡弻浜插娲炬満鍒?/h1> Posted 鐮佸啘寮€鑺?/a>
娣卞叆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了娣卞叆鎺㈢┒JVM涔嬬被鍔犺浇涓庡弻浜插娲炬満鍒?/h1>
相关的知识,希望对你有一定的参考价值。
鎶€鏈共璐?馃憠 蹇呭畾濂変笂
鑳屾櫙浠嬬粛
鏂囩珷鐩綍
鍓嶈█
绫荤殑鐢熷懡鍛ㄦ湡
鍔犺浇
楠岃瘉
鍑嗗
瑙f瀽
鍒濆鍖?/p>
妗堜緥涓€
妗堜緥浜?/p>
妗堜緥涓?/p>
妗堜緥鍥?/p>
绫诲姞杞藉櫒
绫诲姞杞藉櫒鍜屽弻浜插娲炬ā鍨?/p>
鐮村潖鍙屼翰濮旀淳妯″瀷
绗竴娆?/p>
SPI
Tomcat
OSGI
鎬荤粨
鍓嶈█
鍓嶉潰瀛︿範浜嗚櫄鎷熸満鐨勫唴瀛樼粨鏋勩€佸璞$殑鍒嗛厤鍜屽垱寤猴紝浣嗗璞℃墍瀵瑰簲鐨勭被鏄€庝箞鍔犺浇鍒拌櫄鎷熸満涓潵鐨勫憿锛熷姞杞借繃绋嬩腑闇€瑕佸仛浜涗粈涔堬紵浠€涔堟槸鍙屼翰濮旀淳鏈哄埗浠ュ強涓轰粈涔堣鎵撶牬鍙屼翰濮旀淳鏈哄埗锛?/p>
绫荤殑鐢熷懡鍛ㄦ湡
绫荤殑鐢熷懡鍛ㄦ湡鍖呭惈浜嗗涓婄殑7涓樁娈碉紝鍏朵腑楠岃瘉銆?span class="mq-48">鍑嗗銆?span class="mq-49">瑙f瀽缁熺О涓?span class="mq-50">杩炴帴 锛岀被鐨勫姞杞戒富瑕佹槸鍓嶄簲涓樁娈碉紝姣忎釜闃舵鍩烘湰涓婁繚鎸佸涓婇『搴?/span>寮€濮?/span>锛堜粎浠呮槸寮€濮嬶紝瀹為檯涓婃墽琛屾槸浜ゅ弶娣峰悎鐨勶級锛屽彧鏈?span class="mq-52">瑙f瀽闃舵涓嶄竴瀹氾紝鍦?span class="mq-53">鍒濆鍖栧悗涔熸湁鍙兘鎵嶅紑濮嬫墽琛岃В鏋愶紝杩欐槸涓轰簡鏀寔鍔ㄦ€佽瑷€銆?/p>
鍔犺浇
鍔犺浇灏辨槸灏嗗瓧鑺傜爜鐨勪簩杩涘埗娴佽浆鍖栦负鏂规硶鍖?/span>鐨勮繍琛屾椂鏁版嵁缁撴瀯锛屽苟鐢熸垚绫绘墍瀵硅薄鐨凜lass瀵硅薄锛屽瓧鑺傜爜浜岃繘鍒舵祦鍙互鏄垜浠紪璇戝悗鐨刢lass鏂囦欢锛屼篃鍙互浠庣綉缁滀腑鑾峰彇锛屾垨鑰呰繍琛屾椂鍔ㄦ€佺敓鎴愶紙鍔ㄦ€佷唬鐞嗭級绛夌瓑銆?br class="mq-57">閭d粈涔堟椂鍊欎細瑙﹀彂绫诲姞杞藉憿锛熻繖涓湪铏氭嫙鏈鸿鑼冧腑娌℃湁鏄庣‘瀹氫箟锛屽彧鏄瀹氫簡浣曟椂闇€瑕佹墽琛屽垵濮嬪寲锛堢◢鍚庤缁嗗垎鏋愶級銆?/p>
楠岃瘉
杩欎釜闃舵寰堝ソ鐞嗚В锛屽氨鏄繘琛屽繀瑕佺殑鏍¢獙锛岀‘淇濆姞杞藉埌鍐呭瓨涓殑瀛楄妭鐮佹槸绗﹀悎瑕佹眰鐨勶紝涓昏鍖呭惈浠ヤ笅鍥涗釜鏍¢獙姝ラ锛堜簡瑙e嵆鍙級锛?/p>
鏂囦欢鏍煎紡鏍¢獙锛氳繖涓樁娈佃鏍¢獙鐨勪笢瑗块潪甯稿锛屼富瑕佺殑鏈変笅闈㈣繖浜涳紙瀹為檯涓婅繙杩滀笉姝級
鏄惁浠ラ瓟鏁?xCAFEBABE寮€澶淬€?/p>
涓汇€佹鐗堟湰鍙锋槸鍚﹀湪褰撳墠Java铏氭嫙鏈烘帴鍙楄寖鍥翠箣鍐呫€?/p>
甯搁噺姹犵殑甯搁噺涓槸鍚︽湁涓嶈鏀寔鐨勫父閲忕被鍨嬶紙妫€鏌ュ父閲弔ag鏍囧織锛夈€?/p>
鎸囧悜甯搁噺鐨勫悇绉嶇储寮曞€间腑鏄惁鏈夋寚鍚戜笉瀛樺湪鐨勫父閲忔垨涓嶇鍚堢被鍨嬬殑甯搁噺銆?/p>
CONSTANT_Utf8_info鍨嬬殑甯搁噺涓槸鍚︽湁涓嶇鍚圲TF-8缂栫爜鐨勬暟鎹€?/p>
Class鏂囦欢涓悇涓儴鍒嗗強鏂囦欢鏈韩鏄惁鏈夎鍒犻櫎鐨勬垨闄勫姞鐨勫叾浠栦俊鎭€?/p>
銆傘€傘€傘€傘€傘€?/p>
鍏冩暟鎹牎楠岋細瀵瑰瓧鑺傜爜鎻忚堪淇℃伅杩涜璇箟鍒嗘瀽銆?/p>
杩欎釜绫绘槸鍚︽湁鐖剁被锛堥櫎浜唈ava.lang.Object涔嬪锛屾墍鏈夌殑绫婚兘搴斿綋鏈夌埗绫伙級銆?/p>
杩欎釜绫荤殑鐖剁被鏄惁缁ф壙浜嗕笉鍏佽琚户鎵跨殑绫伙紙琚玣inal淇グ鐨勭被锛夈€?/p>
濡傛灉杩欎釜绫讳笉鏄娊璞$被锛屾槸鍚﹀疄鐜颁簡鍏剁埗绫绘垨鎺ュ彛涔嬩腑瑕佹眰瀹炵幇鐨勬墍鏈夋柟娉曘€?/p>
绫讳腑鐨勫瓧娈点€佹柟娉曟槸鍚︿笌鐖剁被浜х敓鐭涚浘锛堜緥濡傝鐩栦簡鐖剁被鐨刦inal瀛楁锛屾垨鑰呭嚭鐜颁笉绗﹀悎瑙勫垯鐨勬柟娉曢噸杞斤紝渚嬪鏂规硶鍙傛暟閮戒竴鑷达紝浣嗚繑鍥炲€肩被鍨嬪嵈涓嶅悓绛夛級銆?/p>
銆傘€傘€傘€傘€傘€?/p>
瀛楄妭鐮佹牎楠岋細纭繚绋嬪簭娌℃湁璇硶鍜岄€昏緫閿欒锛岃繖鏄暣涓獙璇侀樁娈垫渶澶嶆潅鐨勪竴涓楠ゃ€?/p>
淇濊瘉浠绘剰鏃跺埢鎿嶄綔鏁版爤鐨勬暟鎹被鍨嬩笌鎸囦护浠g爜搴忓垪閮借兘閰嶅悎宸ヤ綔锛屼緥濡備笉浼氬嚭鐜扮被浼间簬鈥滃湪鎿嶄綔鏍堟斁缃簡涓€涓?int 绫诲瀷鐨勬暟鎹紝浣跨敤鏃跺嵈鎸?long 绫诲瀷鏉ュ姞杞藉叆鏈湴鍙橀噺琛ㄤ腑鈥濊繖鏍风殑鎯呭喌銆?/p>
淇濊瘉浠讳綍璺宠浆鎸囦护閮戒笉浼氳烦杞埌鏂规硶浣撲互澶栫殑瀛楄妭鐮佹寚浠や笂銆?/p>
淇濊瘉鏂规硶浣撲腑鐨勭被鍨嬭浆鎹㈡€绘槸鏈夋晥鐨勶紝渚嬪鍙互鎶?涓瓙绫诲璞¤祴鍊肩粰鐖剁被鏁版嵁绫诲瀷锛岃繖鏄畨鍏ㄧ殑锛屼絾鏄妸鐖剁被瀵硅薄璧嬪€肩粰瀛愮被鏁版嵁绫诲瀷锛岀敋鑷虫妸瀵硅薄璧嬪€肩粰涓庡畠姣棤缁ф壙鍏崇郴銆佸畬鍏ㄤ笉鐩稿共鐨勪竴涓暟鎹被鍨嬶紝鍒欐槸鍗遍櫓鍜屼笉鍚堟硶鐨勩€?/p>
銆傘€傘€傘€傘€傘€?/p>
绗﹀彿寮曠敤楠岃瘉锛氳繖涓樁娈靛彂鐢熷湪绗﹀彿寮曠敤杞负鐩存帴寮曠敤鐨勬椂鍊欙紝鍗冲疄闄呬笂鏄湪瑙f瀽闃舵涓繘琛岀殑銆?/p>
绗﹀彿寮曠敤涓€氳繃瀛楃涓叉弿杩扮殑鍏ㄩ檺瀹氬悕鏄惁鑳芥壘鍒板搴旂殑绫汇€?/p>
鍦ㄦ寚瀹氱被涓槸鍚﹀瓨鍦ㄧ鍚堟柟娉曠殑瀛楁鎻忚堪绗﹀強绠€鍗曞悕绉版墍鎻忚堪鐨勬柟娉曞拰瀛楁銆?/p>
绗﹀彿寮曠敤涓殑绫汇€佸瓧娈点€佹柟娉曠殑鍙闂€? private銆?protected. public銆?)銆?/p>
鏄惁鍙褰撳墠绫昏闂€?/p>
銆傘€傘€傘€傘€傘€?/p>
鍑嗗
璇ラ樁娈垫槸涓?span class="mq-93">绫诲彉閲?/span>锛坰tatic锛夊垎閰嶅唴瀛樺苟璁剧疆闆跺€硷紝鍗崇被鍙缁忚繃鍑嗗闃舵鍏朵腑鐨勯潤鎬佸彉閲忓氨鏄彲浣跨敤鐨勪簡锛屼絾姝ゆ椂绫诲彉閲忕殑鍊艰繕涓嶆槸鎴戜滑鎯宠鐨勫€硷紝闇€瑕佺粡杩囧垵濮嬪寲闃舵鎵嶄細灏嗘垜浠笇鏈涚殑鍊艰祴鍊肩粰瀵瑰簲鐨勯潤鎬佸彉閲忋€?/p>
瑙f瀽
java.lang.NoSuchFieldError锛氭壘涓嶅埌瀛楁
java.lang.IllegalAccessError锛氫笉鍏锋湁璁块棶鏉冮檺
java.lang.NoSuchMethodError锛氭壘涓嶅埌鏂规硶
鍒濆鍖?/h2>
杩欐槸绫诲姞杞借繃绋嬩腑鐨勬渶鍚庝竴涓楠わ紝涓昏鏄敹闆嗙被鐨?span class="mq-101">闈欐€佸彉閲忕殑璧嬪€煎姩浣?/span>鍜?span class="mq-102">static鍧椾腑鐨勮鍙?/span>鍚堟垚<cinit>鏂规硶锛岄€氳繃璇ユ柟娉曟牴鎹垜浠殑鎰忔効涓?span class="mq-103">闈欐€佸彉閲忚祴鍊?/span>浠ュ強鎵цstatic鍧?/span>锛岃鏂规硶浼氳鍔犻攣锛岀‘淇濆绾跨▼鎯呭喌涓嬪彧鏈変竴涓嚎绋嬭兘鍒濆鍖栨垚鍔燂紝鍒╃敤璇ョ壒鎬у彲浠ュ疄鐜?span class="mq-105">鍗曚緥妯″紡銆傝櫄鎷熸満瑙勫畾浜?span class="mq-106">鏈変笖鍙湁
閬囧埌浠ヤ笅鎯呭喌鏃跺繀椤诲厛纭繚瀵瑰簲绫荤殑鍒濆鍖栧畬鎴愶紙鍔犺浇銆佸噯澶囧繀鐒跺湪姝や箣鍓嶏級锛?/p>
閬囧埌new銆乬etstatic銆乸utstatic鎴杋nvokestatic杩欏洓鏉″瓧鑺傜爜鎸囦护鏃躲€傝兘澶熺敓鎴愯繖鍥涙潯鎸囦护鐨勫吀鍨婮ava浠g爜鍦烘櫙鏈夛細
浣跨敤new鍏抽敭瀛楀疄渚嬪寲瀵硅薄鐨勬椂鍊欍€?/p>
璇诲彇鎴栬缃竴涓被鍨嬬殑闈欐€佸瓧娈碉紙琚玣inal淇グ銆佸凡鍦ㄧ紪璇戞湡鎶婄粨鏋滄斁鍏ュ父閲忔睜鐨勯潤鎬佸瓧娈甸櫎澶栵級鐨勬椂鍊欍€?/p>
璋冪敤涓€涓被鍨嬬殑闈欐€佹柟娉曠殑鏃跺€欍€?/p>
鍙嶅皠璋冪敤绫绘椂銆?/p>
褰撳垵濮嬪寲绫荤殑鏃跺€欙紝濡傛灉鍙戠幇鍏剁埗绫昏繕娌℃湁杩涜杩囧垵濮嬪寲锛屽垯闇€瑕佸厛瑙﹀彂鍏剁埗绫荤殑鍒濆鍖栥€?/p>
褰撹櫄鎷熸満鍚姩鏃讹紝鐢ㄦ埛闇€瑕佹寚瀹氫竴涓鎵ц鐨勪富绫伙紙鍖呭惈main()鏂规硶鐨勯偅涓被锛夛紝铏氭嫙鏈轰細鍏堝垵濮嬪寲杩欎釜涓荤被銆?/p>
褰撲娇鐢↗DK 7鏂板姞鍏ョ殑鍔ㄦ€佽瑷€鏀寔鏃讹紝濡傛灉涓€涓猨ava.lang.invoke.MethodHandle瀹炰緥鏈€鍚庣殑瑙f瀽缁撴灉涓篟EF_getStatic銆丷EF_putStatic銆丷EF_invokeStatic銆丷EF_newInvokeSpecial鍥涚绫诲瀷鐨勬柟娉曞彞鏌勶紝骞朵笖杩欎釜鏂规硶鍙ユ焺瀵瑰簲鐨勭被娌℃湁杩涜杩囧垵濮嬪寲锛屽垯闇€瑕佸厛瑙﹀彂鍏跺垵濮嬪寲銆?/p>
褰撲竴涓帴鍙d腑瀹氫箟浜咼DK 8鏂板姞鍏ョ殑榛樿鏂规硶锛堣default鍏抽敭瀛椾慨楗扮殑鎺ュ彛鏂规硶锛夋椂锛屽鏋滄湁杩欎釜鎺ュ彛鐨勫疄鐜扮被鍙戠敓浜嗗垵濮嬪寲锛岄偅璇ユ帴鍙h鍦ㄥ叾涔嬪墠琚垵濮嬪寲銆?/p>
涓嬮潰鍒嗘瀽鍑犱釜妗堜緥浠g爜锛岃鑰呬滑鍙互鍏堟€濊€冨悗鍐嶈繍琛屼唬鐮佺湅鐪嬪拰鑷繁鎯崇殑鏄惁涓€鏍枫€?/p>
妗堜緥涓€
鍏堝畾涔夊涓嬩袱涓被锛?/p>
public class SuperClazz {
static {
System.out.println("SuperClass init锛?);
}
public static int value=123;
public static final String HELLOWORLD="hello world";
public static final int WHAT = value;
}
public class SubClaszz extends SuperClazz {
static{
System.out.println("SubClass init锛?);
}
}
1
鐒跺悗杩涜涓嬮潰鐨勮皟鐢細
public class Initialization {
public static void main(String[]args){
Initialization initialization = new Initialization();
initialization.M1();
}
public void M1(){
System.out.println(SubClaszz.value);
}
}
1
绗竴涓渚嬫槸閫氳繃瀛愮被鍘诲紩鐢ㄧ埗绫讳腑鐨?span class="mq-223">闈欐€佸彉閲?/span>锛屼袱涓被閮戒細鍔犺浇鍜屽垵濮嬪寲涔堬紵鎵撳嵃缁撴灉鐪嬬湅锛?/p>
SuperClass init锛?br>123
鍙互鐪嬪埌鍙湁鐖剁被鍒濆鍖栦簡锛岄偅涔堢埗绫诲繀鐒舵槸鍔犺浇浜嗙殑锛岄棶棰樺氨鍦ㄤ簬瀛愮被鏈夋病鏈夎鍔犺浇鍛紵鍙互鍔犱笂鍙傛暟锛?XX:+TraceClassLoading鍐嶆墽琛岋紙璇ュ弬鏁扮殑浣滅敤灏辨槸鎵撳嵃琚姞杞戒簡鐨勭被锛夛紝鍙互鐪嬪埌瀛愮被鏄鍔犺浇浜嗙殑銆傛墍浠?span class="mq-230">閫氳繃瀛愮被寮曠敤鐖剁被闈欐€佸彉閲忥紝鐖跺瓙绫婚兘浼氳鍔犺浇锛屼絾鍙湁鐖剁被浼氳繘琛屽垵濮嬪寲銆?br class="mq-231">涓轰粈涔堝憿锛熷弽缂栬瘧鍚庡彲浠ョ湅鍒扮敓鎴愪簡濡備笅鎸囦护锛?/p>
0: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
3: getstatic #6 // Field ex7/init/SubClaszz.value:I
6: invokevirtual #7 // Method java/io/PrintStream.println:(I)V
9: return
鍏抽敭灏辨槸getstatic鎸囦护灏变細瑙﹀彂绫荤殑鍒濆鍖栵紝浣嗘槸涓轰粈涔堝瓙绫讳笉浼氬垵濮嬪寲鍛紵鍥犱负杩欎釜鍙橀噺鏄潵鑷簬鐖剁被鐨勶紝涓轰簡鎻愰珮鏁堢巼锛屾墍浠ヨ櫄鎷熸満杩涜浜嗕紭鍖栵紝杩欑鎯呭喌鍙渶瑕佸垵濮嬪寲鐖剁被灏辫浜嗐€?/p>
妗堜緥浜?/h3>
璋冪敤涓嬮潰鐨勬柟娉曪細
public void M2(){
SubClaszz[]sca = new SubClaszz[10];
}
1
鎵ц鍚庡彲浠ュ彂鐜帮紝浣跨敤鏁扮粍锛屼笉浼氳Е鍙戝垵濮嬪寲锛屼絾鐖跺瓙绫婚兘浼氳鍔犺浇銆?/p>
妗堜緥涓?/h3>
public void M3(){
System.out.println(SuperClazz.HELLOWORLD);
寮曠敤甯搁噺涓嶄細瑙﹀彂绫荤殑鍔犺浇鍜屽垵濮嬪寲锛屽洜涓哄父閲忓湪缂栬瘧鍚庡氨宸茬粡瀛樺湪褰撳墠class鐨勫父閲忔睜銆?/p>
妗堜緥鍥?/h3>
public void M4(){
System.out.println(SubClaszz.WHAT);
}
閫氳繃甯搁噺鍘诲紩鐢ㄥ叾瀹冪殑闈欐€佸彉閲忎細鍙戠敓浠€涔堝憿锛熻繖涓拰妗堜緥涓€缁撴灉鏄竴鏍风殑銆?/p>
绫诲姞杞藉櫒
绫诲姞杞藉櫒鍜屽弻浜插娲炬ā鍨?/h2>
鍦ㄦ垜浠钩鏃跺紑鍙戜腑锛岀‘瀹氫竴涓被闇€瑕侀€氳繃瀹屽叏闄愬畾鍚?/span>锛岃€屼笉鑳界畝鍗曠殑閫氳繃鍚嶅瓧锛屽洜涓哄湪涓嶅悓鐨勮矾寰勪笅鎴戜滑鏄彲浠ュ畾涔夊悓鍚嶇殑绫荤殑銆傞偅涔堝湪铏氭嫙鏈轰腑鍙堟槸鎬庝箞鍖哄垎绫荤殑鍛紵鍦ㄨ櫄鎷熸満涓渶瑕?span class="mq-319">绫诲姞杞藉櫒+瀹屽叏闄愬畾鍚?/span>涓€璧锋潵鎸囧畾涓€涓被鐨勫敮涓€鎬э紝鍗崇浉鍚岄檺瀹氬悕鐨勭被鑻ョ敱涓や釜涓嶅悓鐨勭被鍔犺浇鍣ㄥ姞杞斤紝閭h櫄鎷熸満灏变笉浼氭妸瀹冧滑褰撳仛涓€涓被銆備粠杩欓噷鎴戜滑鍙互鐪嬪嚭绫诲姞杞藉櫒涓€瀹氭槸鏈夊涓殑锛岄偅涔堜笉鍚岀殑绫诲姞杞藉櫒鏄€庝箞缁勭粐鐨勶紵瀹冧滑鍙堝垎鍒渶瑕佸姞杞藉摢浜涚被鍛紵
浠庤櫄鎷熻搴︾湅锛屽彧鏈変袱绉嶇被鍨嬬殑绫诲姞杞藉櫒锛?span class="mq-325">鍚姩绫诲姞杞藉櫒锛圔ootstrapClassLoader锛?span class="mq-326">鍜?/span>闈炲惎鍔ㄧ被鍔犺浇鍣?/span>銆傚墠鑰呮槸C++瀹炵幇锛屽睘浜庤櫄鎷熸満鐨勪竴閮ㄥ垎锛屽悗鑰呭垯鏄敱Java瀹炵幇鐨勶紝鐙珛浜庤櫄鎷熸満鐨勫閮紝骞朵笖鍏ㄩ儴缁ф壙鑷娊璞$被java.lang.ClassLoader銆?br class="mq-327">浣嗕粠Java鏈韩鏉ョ湅锛屼竴鐩翠繚鎸佺潃涓夊眰绫诲姞杞藉櫒銆?span class="mq-329">鍙屼翰濮旀淳鐨勭粨鏋勶紝褰撶劧闄や簡Java鏈韩鎻愪緵鐨勪笁灞傜被鍔犺浇鍣紝鎴戜滑杩樺彲浠ヨ嚜瀹氫箟瀹炵幇绫诲姞杞藉櫒銆傚涓婂浘锛屼笂闈笁涓氨鏄師鐢熺殑绫诲姞杞藉櫒锛屾瘡涓€涓兘鏄笅涓€涓被鍔犺浇鍣ㄧ殑鐖跺姞杞藉櫒锛屾敞鎰忚繖閲岄兘鏄噰鐢?span class="mq-331">缁勫悎鑰岄潪缁ф壙銆傚綋寮€濮嬪姞杞界被鏃讹紝棣栧厛浜ょ粰鐖跺姞杞藉櫒鍔犺浇锛岀埗鍔犺浇鍣ㄥ姞杞戒簡瀛愬姞杞藉櫒灏变笉鐢ㄥ啀鍔犺浇浜嗭紝鑰岃嫢鏄埗鍔犺浇鍣ㄥ姞杞戒笉浜嗭紝灏变細浜ょ粰瀛愬姞杞藉櫒鍔犺浇锛岃繖灏辨槸鍙屼翰濮旀淳鏈哄埗銆傝繖灏卞ソ姣斿伐浣滀腑閬囧埌浜嗘棤娉曞鐞嗙殑浜嬶紝浣犱細鍘昏绀虹洿鎺ラ瀵硷紝鐩存帴棰嗗澶勭悊涓嶄簡锛屽啀鎵句笂灞傞瀵硷紝鐒跺悗涓婂眰棰嗗瑙夊緱杩欐槸涓皬浜嬶紝涓嶇敤浠栦翰鑷姩鎵嬶紝灏辫浣犵殑鐩存帴棰嗗鍘诲仛锛屾帴鐫€浠栧張浜ょ粰浣犲幓鍋氱瓑绛夈€備笅闈㈡潵鐪嬬湅姣忎釜绫诲姞杞藉櫒鐨勫叿浣撲綔鐢細
BootstrapClassLoader锛氬惎鍔ㄧ被鍔犺浇鍣紝椤惧悕鎬濅箟锛岃繖涓被鍔犺浇鍣ㄤ富瑕佽礋璐e姞杞絁DK lib鍖咃紝浠ュ強-Xbootclasspath鍙傛暟鎸囧畾鐨勭洰褰曪紝骞朵笖铏氭嫙鏈哄鏂囦欢鍚嶈繘琛屼簡闄愬畾锛屼篃灏辨槸璇村嵆浣挎垜浠嚜宸卞啓涓猨ar鏀惧叆鍒颁笂杩扮洰褰曪紝涔熶笉浼氳鍔犺浇銆傜敱浜庤绫诲姞杞藉櫒鏄疌++浣跨敤锛屾墍浠ユ垜浠殑Java绋嬪簭涓棤娉曠洿鎺ュ紩鐢紝璋冪敤java.lang.ClassLoader.getClassLoader()鏂规硶鏃堕粯璁よ繑鍥炵殑鏄痭ull銆?/p>
ExtClassLoader锛氭墿灞曠被鍔犺浇鍣紝涓昏璐熻矗鍔犺浇JDK lib/ext鍖咃紝浠ュ強琚郴缁熷彉閲廽ava.ext.dirs鎸囧悜鐨勬墍鏈夌被搴擄紝杩欎釜绫诲簱鍙互瀛樻斁鎴戜滑鑷繁鍐欑殑閫氱敤jar銆?/p>
AppClassLoader锛氬簲鐢ㄧ▼搴忕被鍔犺浇鍣紝璐熻矗鍔犺浇鐢ㄦ埛classpath涓婄殑鎵€鏈夌被銆傚畠鏄痡ava.lang.ClassLoader.getSystemClassLoader()鐨勮繑鍥炲€硷紝涔熸槸鎴戜滑绋嬪簭鐨勯粯璁ょ被鍔犺浇鍣紙濡傛灉鎴戜滑娌℃湁鑷畾涔夌被鍔犺浇鍣ㄧ殑璇濓級銆?/p>
閫氳繃杩欎笁涓被鍔犺浇浠ュ強鍙屼翰濮旀淳鏈哄埗锛屼竴涓樉鑰屾槗瑙佺殑濂藉灏辨槸锛屼笉鍚岀殑绫婚殢瀹冪殑绫诲姞杞藉櫒澶╃劧鍏锋湁浜嗗姞杞戒紭鍏堢骇锛屽儚Object銆丼tring绛夌瓑杩欎簺鏍稿績绫诲簱鑷劧灏变細鍦ㄦ垜浠殑搴旂敤绋嬪簭绫讳箣鍓嶈鍔犺浇锛屼娇寰楃▼搴忔洿瀹夊叏锛屼笉浼氬嚭鐜伴敊璇紝Spring鐨勭埗瀛愬鍣ㄤ篃鏄繖鏍风殑涓€涓璁°€傞€氳繃涓嬮潰杩欐浠g爜鍙互鐪嬪埌姣忎釜绫绘墍瀵瑰簲鐨勭被鍔犺浇鍣細
public class ClassLoader {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader()); //鍚姩绫诲姞杞藉櫒
System.out.println(sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader());//鎷撳睍绫诲姞杞藉櫒
System.out.println(ClassLoader.class.getClassLoader());//搴旂敤绋嬪簭绫诲姞杞藉櫒
}
}
杈撳嚭锛?/p>
null
sun.misc.Launcher$ExtClassLoader@4b67cf4d
sun.misc.Launcher$AppClassLoader@14dad5dc
鐮村潖鍙屼翰濮旀淳妯″瀷
鍒氬垰鎴戜妇浜嗗伐浣滀腑鐨勪竴涓緥瀛愭潵璇存槑鍙屼翰濮旀淳鏈哄埗锛屼絾鐜板疄涓垜浠笉闇€瑕佷簨浜嬮兘鍘昏绀洪瀵硷紝鍚屾牱绫诲姞杞藉櫒涔熶笉鏄畬鍏ㄩ伒寰弻浜插娲炬満鍒讹紝鍦ㄥ繀瑕佺殑鏃跺€欐槸鍙互鎵撶牬杩欎釜瑙勫垯鐨勩€備笅闈㈠垪涓惧洓涓牬鍧忕殑鎯呭喌锛屽湪姝や箣鍓嶆垜浠渶瑕佸厛浜嗚В涓嬪弻浜?濮旀淳鐨勪唬鐮佸疄鐜板師鐞嗭紝鍦╦ava.lang.ClassLoader绫讳腑鏈変竴涓?span class="mq-415">loadClass浠ュ強findClass鏂规硶锛?/p>
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
浠庝笂闈㈠彲浠ョ湅鍒伴鍏堟槸璋冪敤parent鍘诲姞杞界被锛屾病鏈夊姞杞藉埌鎵嶈皟鐢ㄨ嚜韬殑findClass鏂规硶鍘诲姞杞姐€備篃灏辨槸璇寸敤鎴峰湪瀹炵幇鑷畾涔夌被鍔犺浇鍣ㄧ殑鏃跺€欓渶瑕佽鐩栫殑鏄?span class="mq-581">fiindClass鑰屼笉鏄?span class="mq-582">loadClass锛岃繖鏍锋墠鑳芥弧瓒?span class="mq-583">鍙屼翰濮旀淳妯″瀷銆?br class="mq-584">涓嬮潰鍏蜂綋鏉ョ湅鐪嬬牬鍧忓弻浜插娲剧殑鍑犱釜鍦烘櫙銆?/p>
绗竴娆?/h3>
绗竴娆$牬鍧忔槸鍦ㄥ弻浜插娲炬ā鍨嬪嚭鐜颁箣鍓嶏紝 鍥犱负璇ユā鍨嬫槸鍦↗DK1.2涔嬪悗鎵嶅紩鍏ョ殑锛岄偅涔堝湪姝や箣鍓嶏紝鎶借薄绫籮ava.lang.ClassLoader灏卞凡缁忓瓨鍦ㄤ簡锛岀敤鎴疯嚜瀹氫箟鐨勭被鍔犺浇鍣ㄩ兘浼氬幓瑕嗙洊璇ョ被涓殑loadClass鏂规硶锛屾墍浠ュ弻浜插娲炬ā鍨嬪嚭鐜板悗锛屽氨鏃犳硶閬垮厤鐢ㄦ埛瑕嗙洊璇ユ柟娉曪紝鍥犳鏂板浜?span class="mq-588">findClass寮曞鐢ㄦ埛鍘昏鐩栬鏂规硶瀹炵幇鑷繁鐨勭被鍔犺浇閫昏緫銆?/p>
SPI
绗簩娆$牬鍧忔槸鐢变簬杩欎釜妯″瀷鏈韩缂洪櫡瀵艰嚧鐨勶紝鍥犱负璇ユā鍨嬩繚璇佷簡绫荤殑鍔犺浇浼樺厛绾э紝浣嗘槸鏈変簺鎺ュ彛鏄疛ava瀹氫箟鍦ㄦ牳蹇冪被搴撲腑锛屼絾鍏蜂綋鐨勬湇鍔″疄鐜版槸鐢辩敤鎴锋彁渚涚殑锛岃繖鏃跺€欏氨涓嶅緱涓嶇牬鍧忚妯″瀷鎵嶈兘瀹炵幇锛屽吀鍨嬬殑灏辨槸Java涓殑SPI鏈哄埗锛堝SPI涓嶄簡瑙g殑璇昏€呭彲浠ョ炕闃呮垜涔嬪墠鐨勬枃绔犳垨鏄叾瀹冭祫鏂欙紝杩欓噷涓嶈繘琛岄槓杩帮級銆侸
DBC鐨勯┍鍔ㄥ姞杞藉氨鏄疭PI瀹炵幇鐨勶紝鎵€浠ョ洿鎺ョ湅鍒?span class="mq-592">java.sql.DriverManager绫伙紝璇ョ被涓湁涓€涓潤鎬佸垵濮嬪寲鍧楋細
static {
loadInitialDrivers();
println("JDBC DriverManager initialized");
}
private static void loadInitialDrivers() {
String drivers;
try {
drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("jdbc.drivers");
}
});
} catch (Exception ex) {
drivers = null;
}
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
Iterator<Driver> driversIterator = loadedDrivers.iterator();
try{
while(driversIterator.hasNext()) {
driversIterator.next();
}
} catch(Throwable t) {
// Do nothing
}
return null;
}
});
println("DriverManager.initialize: jdbc.drivers = " + drivers);
if (drivers == null || drivers.equals("")) {
return;
}
String[] driversList = drivers.split(":");
println("number of Drivers:" + driversList.length);
for (String aDriver : driversList) {
try {
println("DriverManager.Initialize: loading " + aDriver);
Class.forName(aDriver, true,
ClassLoader.getSystemClassLoader());
} catch (Exception ex) {
println("DriverManager.Initialize: load failed: " + ex);
}
}
}
51
涓昏鐪?span class="mq-800">ServiceLoader.load鏂规硶锛岃繖涓氨鏄€氳繃SPI鍘诲姞杞芥垜浠紩鍏?span class="mq-801">java.sql.Driver瀹炵幇绫伙紙姣斿寮曞叆mysql鐨勯┍鍔ㄥ寘灏辨槸com.mysql.cj.jdbc.Driver锛夛細
public static <S> ServiceLoader<S> load(Class<S> service) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return ServiceLoader.load(service, cl);
}
杩欎釜鏂规硶涓昏鏄粠褰撳墠绾跨▼涓幏鍙栫被鍔犺浇鍣紝鐒跺悗閫氳繃杩欎釜绫诲姞杞藉櫒鍘诲姞杞?span class="mq-840">椹卞姩瀹炵幇绫?/span>锛堣繖涓彨绾跨▼涓婁笅鏂囩被鍔犺浇鍣?/span>锛屾垜浠篃鍙互浣跨敤杩欎釜鎶€宸у幓鎵撶牬鍙屼翰濮旀淳锛夛紝閭h繖閲屼細鑾峰彇鍒板摢涓€涓被鍔犺浇鍣ㄥ憿锛熷叿浣撶殑璁剧疆鏄湪sun.misc.Launcher绫荤殑鏋勯€犲櫒涓細
public Launcher() {
Launcher.ExtClassLoader var1;
try {
var1 = Launcher.ExtClassLoader.getExtClassLoader();
} catch (IOException var10) {
throw new InternalError("Could not create extension class loader", var10);
}
try {
this.loader = Launcher.AppClassLoader.getAppClassLoader(var1);
} catch (IOException var9) {
throw new InternalError("Could not create application class loader", var9);
}
Thread.currentThread().setContextClassLoader(this.loader);
String var2 = System.getProperty("java.security.manager");
if (var2 != null) {
SecurityManager var3 = null;
if (!"".equals(var2) && !"default".equals(var2)) {
try {
var3 = (SecurityManager)this.loader.loadClass(var2).newInstance();
} catch (IllegalAccessException var5) {
} catch (InstantiationException var6) {
} catch (ClassNotFoundException var7) {
} catch (ClassCastException var8) {
}
} else {
var3 = new SecurityManager();
}
if (var3 == null) {
throw new InternalError("Could not create SecurityManager: " + var2);
}
System.setSecurityManager(var3);
}
鍙互鐪嬪埌璁剧疆鐨勫氨鏄?span class="mq-1018">AppClassLoader銆備綘鍙兘浼氭湁鐐圭枒鎯戯紝杩欎釜绫诲姞杞藉櫒鍔犺浇绫荤殑鏃跺€欎笉涔熸槸鍏堣皟鐢ㄧ埗绫诲姞杞藉櫒鍔犺浇涔堬紝鎬庝箞灏辨墦鐮村弻浜插娲句簡鍛紵鍏跺疄鎵撶牬鍙屼翰濮旀淳鎸囩殑灏辨槸绫荤殑灞傛缁撴瀯锛屽欢浼告剰鎬濆氨鏄被鐨勫姞杞戒紭鍏堢骇锛岃繖閲屾湰搴旇鏄湪鍔犺浇鏍稿績绫诲簱鐨勬椂鍊欏嵈鎻愬墠灏嗘垜浠簲鐢ㄧ▼搴忎腑鐨勭被搴撶粰鍔犺浇鍒拌櫄鎷熸満涓潵浜嗐€?/p>
Tomcat
涓婂浘鏄疶omcat绫诲姞杞界殑绫诲浘锛屽墠闈笁涓笉鐢ㄨ锛?span class="mq-1022">CommonClassLoader銆?span class="mq-1023">CatalinaClassLoader銆?span class="mq-1024">SharedClassLoader銆?span class="mq-1025">WebAppClassLoader銆?span class="mq-1026">JspClassLoader鍒欐槸Tomcat鑷繁瀹炵幇鐨勭被鍔犺浇鍣紝鍒嗗埆鍔犺浇common鍖?/span>銆?span class="mq-1028">server鍖?/span>銆?span class="mq-1029">shared鍖?/span>銆?span class="mq-1030">WebApp/WEB-INF/lib鍖?/span>浠ュ強JSP鏂囦欢锛屽墠闈笁涓湪tomcat 6涔嬪悗宸茬粡鍚堝苟鍒版牴鐩綍涓嬬殑lib鐩綍涓嬨€傝€?span class="mq-1032">WebAppClassLoader鍒欐槸姣忎竴涓簲鐢ㄧ▼搴忓搴斾竴涓紝JspClassLoader鏄瘡涓€涓狫SP鏂囦欢閮戒細瀵瑰簲涓€涓紝骞朵笖杩欎袱涓被鍔犺浇鍣ㄩ兘娌℃湁鐖剁被鍔犺浇鍣紝杩欎篃灏辫繚鑳屼簡鍙屼翰濮旀淳妯″瀷銆?br class="mq-1034">涓轰粈涔堟瘡涓簲鐢ㄧ▼搴忛渶瑕佸崟鐙殑WebAppClassLoader瀹炰緥锛熷洜涓烘瘡涓簲鐢ㄧ▼搴忛渶瑕佸郊姝ら殧绂伙紝鍋囧鍦ㄤ袱涓簲鐢ㄤ腑瀹氫箟浜嗕竴鏍风殑绫伙紙瀹屽叏闄愬畾鍚嶏級锛屽鏋滈伒寰弻浜插娲鹃偅灏卞彧浼氬瓨鍦ㄤ竴浠戒簡锛屽彟澶栦笉鍚岀殑搴旂敤杩樻湁鍙兘渚濊禆鍚屼竴涓被搴撶殑涓嶅悓鐗堟湰锛岃繖涔熼渶瑕侀殧绂伙紝鎵€浠ユ瘡涓€涓簲鐢ㄧ▼搴忛兘浼氬搴斾竴涓?span class="mq-1037">WebAppClassLoader锛屽畠浠叡浜殑绫诲簱鍙互璁?span class="mq-1038">SharedClassLoader鍔犺浇锛屽彟澶栬繖浜涚被鍔犺浇鍔犺浇鐨勭被瀵筎omcat鏈韩鏉ヨ涔熸槸闅旂鐨勶紙CatalinaClassLoader鍔犺浇鐨勶級銆?br class="mq-1039">涓轰粈涔堟瘡涓狫SP鏂囦欢闇€瑕佸搴斿崟鐙殑涓€涓?span class="mq-1040">JspClassLoader瀹炰緥锛熻繖鏄敱浜嶫SP鏄敮鎸佽繍琛屾椂淇敼鐨勶紝淇敼鍚庝細涓㈠純鎺変箣鍓嶇紪璇戠敓鎴愮殑class锛屽苟閲嶆柊鐢熸垚涓€涓?span class="mq-1041">JspClassLoader瀹炰緥鍘诲姞杞芥柊鐨刢lass銆?br class="mq-1042">浠ヤ笂灏辨槸Tomcat涓轰粈涔堣鎵撶牬鍙屼翰濮旀淳妯″瀷鐨勫師鍥犮€?/p>
OSGI
OSGI鏄敤浜庡疄鐜版ā鍧楃儹閮ㄧ讲锛屽儚Eclipse鐨勬彃浠剁郴缁熷氨鏄埄鐢∣SGI瀹炵幇鐨勶紝杩欎釜鎶€鏈潪甯稿鏉傚悓鏃朵娇鐢ㄧ殑涔熻秺鏉ヨ秺灏戜簡锛屾劅鍏磋叮鐨勮鑰呭彲鑷鏌ラ槄璧勬枡瀛︿範锛岃繖閲屼笉鍐嶈繘琛岄槓杩般€?/p>
鎬荤粨
绫诲姞杞界殑杩囩▼璁╂垜浠簡瑙e埌涓€涓被鏄浣曡鍔犺浇鍒板唴瀛樹腑锛岄渶瑕佺粡杩囧摢浜涢樁娈碉紱鑰岀被鍔犺浇鍣ㄥ拰鍙屼翰濮旀淳妯″瀷鍒欐槸鍛婅瘔鎴戜滑搴旇鎬庝箞鍘诲姞杞界被銆佺被鐨勫姞杞戒紭鍏堢骇鏄€庢牱鐨勶紝鍏朵腑鐨勮璁℃€濇兂鎴戜滑涔熷彲浠ュ涔犲€熼壌锛涙渶鍚庨渶瑕佹繁鍒荤悊瑙g殑鏄负浠€涔堥渶瑕佹墦鐮村弻浜插娲撅紝鍦ㄩ亣鍒扮浉搴旂殑鍦烘櫙鏃跺簲璇ユ€庝箞鍋氥€?/p>
end
骞茶揣鍒嗕韩
鍥炲銆?01銆戣幏鍙?nbsp; 銆婂叏鏍堟灦鏋勫笀銆嬭绋?nbsp;
鍥炲銆?02銆戣幏鍙?銆婇珮绾у疄鎴樻灦鏋勫笀璇剧▼銆嬭绋?nbsp;
鍥炲銆?03銆戣幏鍙?銆婇珮绾ф灦鏋勫笀鍏ㄥ鏁欑▼銆嬭绋?nbsp;
鍥炲銆?05銆戣幏鍙?nbsp;銆婂ぇ鏁版嵁涔婬ive瑙嗛銆嬭绋?nbsp;
鍥炲銆?08銆戣幏鍙?nbsp;銆妚3 RabbitMQ娑堟伅涓棿浠舵妧鏈簿璁层€嬭绋?nbsp;
鍥炲銆?09銆戣幏鍙?nbsp; 銆婂ぇ鏁版嵁涔媄ookeeper瑙嗛銆嬭绋?nbsp;
鏇村瀛︿範璧勬枡璇疯仈绯绘垜鍝︼紒锛侊紒
鏈夌儹闂ㄦ帹鑽?/span>馃憞
1.
2.
3.
4.
鎶€鏈€丣VM銆丣ava骞跺彂銆丼SM銆佸井鏈嶅姟銆佹暟鎹簱銆佹暟鎹粨鏋勭瓑绛夈€?/span>
鍠滄鐨勭殑璇濓紝鐐逛釜鈥?/strong>鍦ㄧ湅鈥?/strong>榧撳姳涓€涓嬪惂
璋㈣阿 (*^__^*锛?/strong>
以上是关于娣卞叆鎺㈢┒JVM涔嬬被鍔犺浇涓庡弻浜插娲炬満鍒?/h1>
Posted 鐮佸啘寮€鑺?/a> 娣卞叆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了娣卞叆鎺㈢┒JVM涔嬬被鍔犺浇涓庡弻浜插娲炬満鍒?/h1>
鎶€鏈共璐?馃憠 蹇呭畾濂変笂
鑳屾櫙浠嬬粛
鏂囩珷鐩綍
鍓嶈█
绫荤殑鐢熷懡鍛ㄦ湡
鍔犺浇
楠岃瘉
鍑嗗
瑙f瀽
鍒濆鍖?/p>
妗堜緥涓€
妗堜緥浜?/p>
妗堜緥涓?/p>
妗堜緥鍥?/p>
绫诲姞杞藉櫒
绫诲姞杞藉櫒鍜屽弻浜插娲炬ā鍨?/p>
鐮村潖鍙屼翰濮旀淳妯″瀷
绗竴娆?/p>
SPI
Tomcat
OSGI
鎬荤粨
鍓嶈█
鍓嶉潰瀛︿範浜嗚櫄鎷熸満鐨勫唴瀛樼粨鏋勩€佸璞$殑鍒嗛厤鍜屽垱寤猴紝浣嗗璞℃墍瀵瑰簲鐨勭被鏄€庝箞鍔犺浇鍒拌櫄鎷熸満涓潵鐨勫憿锛熷姞杞借繃绋嬩腑闇€瑕佸仛浜涗粈涔堬紵浠€涔堟槸鍙屼翰濮旀淳鏈哄埗浠ュ強涓轰粈涔堣鎵撶牬鍙屼翰濮旀淳鏈哄埗锛?/p>
绫荤殑鐢熷懡鍛ㄦ湡
绫荤殑鐢熷懡鍛ㄦ湡鍖呭惈浜嗗涓婄殑7涓樁娈碉紝鍏朵腑楠岃瘉銆?span class="mq-48">鍑嗗銆?span class="mq-49">瑙f瀽缁熺О涓?span class="mq-50">杩炴帴 锛岀被鐨勫姞杞戒富瑕佹槸鍓嶄簲涓樁娈碉紝姣忎釜闃舵鍩烘湰涓婁繚鎸佸涓婇『搴?/span>寮€濮?/span>锛堜粎浠呮槸寮€濮嬶紝瀹為檯涓婃墽琛屾槸浜ゅ弶娣峰悎鐨勶級锛屽彧鏈?span class="mq-52">瑙f瀽闃舵涓嶄竴瀹氾紝鍦?span class="mq-53">鍒濆鍖栧悗涔熸湁鍙兘鎵嶅紑濮嬫墽琛岃В鏋愶紝杩欐槸涓轰簡鏀寔鍔ㄦ€佽瑷€銆?/p>
鍔犺浇
鍔犺浇灏辨槸灏嗗瓧鑺傜爜鐨勪簩杩涘埗娴佽浆鍖栦负鏂规硶鍖?/span>鐨勮繍琛屾椂鏁版嵁缁撴瀯锛屽苟鐢熸垚绫绘墍瀵硅薄鐨凜lass瀵硅薄锛屽瓧鑺傜爜浜岃繘鍒舵祦鍙互鏄垜浠紪璇戝悗鐨刢lass鏂囦欢锛屼篃鍙互浠庣綉缁滀腑鑾峰彇锛屾垨鑰呰繍琛屾椂鍔ㄦ€佺敓鎴愶紙鍔ㄦ€佷唬鐞嗭級绛夌瓑銆?br class="mq-57">閭d粈涔堟椂鍊欎細瑙﹀彂绫诲姞杞藉憿锛熻繖涓湪铏氭嫙鏈鸿鑼冧腑娌℃湁鏄庣‘瀹氫箟锛屽彧鏄瀹氫簡浣曟椂闇€瑕佹墽琛屽垵濮嬪寲锛堢◢鍚庤缁嗗垎鏋愶級銆?/p>
杩欎釜闃舵寰堝ソ鐞嗚В锛屽氨鏄繘琛屽繀瑕佺殑鏍¢獙锛岀‘淇濆姞杞藉埌鍐呭瓨涓殑瀛楄妭鐮佹槸绗﹀悎瑕佹眰鐨勶紝涓昏鍖呭惈浠ヤ笅鍥涗釜鏍¢獙姝ラ锛堜簡瑙e嵆鍙級锛?/p>
鏂囦欢鏍煎紡鏍¢獙锛氳繖涓樁娈佃鏍¢獙鐨勪笢瑗块潪甯稿锛屼富瑕佺殑鏈変笅闈㈣繖浜涳紙瀹為檯涓婅繙杩滀笉姝級 鏄惁浠ラ瓟鏁?xCAFEBABE寮€澶淬€?/p> 涓汇€佹鐗堟湰鍙锋槸鍚﹀湪褰撳墠Java铏氭嫙鏈烘帴鍙楄寖鍥翠箣鍐呫€?/p> 甯搁噺姹犵殑甯搁噺涓槸鍚︽湁涓嶈鏀寔鐨勫父閲忕被鍨嬶紙妫€鏌ュ父閲弔ag鏍囧織锛夈€?/p> 鎸囧悜甯搁噺鐨勫悇绉嶇储寮曞€间腑鏄惁鏈夋寚鍚戜笉瀛樺湪鐨勫父閲忔垨涓嶇鍚堢被鍨嬬殑甯搁噺銆?/p> CONSTANT_Utf8_info鍨嬬殑甯搁噺涓槸鍚︽湁涓嶇鍚圲TF-8缂栫爜鐨勬暟鎹€?/p> Class鏂囦欢涓悇涓儴鍒嗗強鏂囦欢鏈韩鏄惁鏈夎鍒犻櫎鐨勬垨闄勫姞鐨勫叾浠栦俊鎭€?/p> 銆傘€傘€傘€傘€傘€?/p> 鍏冩暟鎹牎楠岋細瀵瑰瓧鑺傜爜鎻忚堪淇℃伅杩涜璇箟鍒嗘瀽銆?/p> 杩欎釜绫绘槸鍚︽湁鐖剁被锛堥櫎浜唈ava.lang.Object涔嬪锛屾墍鏈夌殑绫婚兘搴斿綋鏈夌埗绫伙級銆?/p> 杩欎釜绫荤殑鐖剁被鏄惁缁ф壙浜嗕笉鍏佽琚户鎵跨殑绫伙紙琚玣inal淇グ鐨勭被锛夈€?/p> 濡傛灉杩欎釜绫讳笉鏄娊璞$被锛屾槸鍚﹀疄鐜颁簡鍏剁埗绫绘垨鎺ュ彛涔嬩腑瑕佹眰瀹炵幇鐨勬墍鏈夋柟娉曘€?/p> 绫讳腑鐨勫瓧娈点€佹柟娉曟槸鍚︿笌鐖剁被浜х敓鐭涚浘锛堜緥濡傝鐩栦簡鐖剁被鐨刦inal瀛楁锛屾垨鑰呭嚭鐜颁笉绗﹀悎瑙勫垯鐨勬柟娉曢噸杞斤紝渚嬪鏂规硶鍙傛暟閮戒竴鑷达紝浣嗚繑鍥炲€肩被鍨嬪嵈涓嶅悓绛夛級銆?/p> 銆傘€傘€傘€傘€傘€?/p> 瀛楄妭鐮佹牎楠岋細纭繚绋嬪簭娌℃湁璇硶鍜岄€昏緫閿欒锛岃繖鏄暣涓獙璇侀樁娈垫渶澶嶆潅鐨勪竴涓楠ゃ€?/p> 淇濊瘉浠绘剰鏃跺埢鎿嶄綔鏁版爤鐨勬暟鎹被鍨嬩笌鎸囦护浠g爜搴忓垪閮借兘閰嶅悎宸ヤ綔锛屼緥濡備笉浼氬嚭鐜扮被浼间簬鈥滃湪鎿嶄綔鏍堟斁缃簡涓€涓?int 绫诲瀷鐨勬暟鎹紝浣跨敤鏃跺嵈鎸?long 绫诲瀷鏉ュ姞杞藉叆鏈湴鍙橀噺琛ㄤ腑鈥濊繖鏍风殑鎯呭喌銆?/p> 淇濊瘉浠讳綍璺宠浆鎸囦护閮戒笉浼氳烦杞埌鏂规硶浣撲互澶栫殑瀛楄妭鐮佹寚浠や笂銆?/p> 淇濊瘉鏂规硶浣撲腑鐨勭被鍨嬭浆鎹㈡€绘槸鏈夋晥鐨勶紝渚嬪鍙互鎶?涓瓙绫诲璞¤祴鍊肩粰鐖剁被鏁版嵁绫诲瀷锛岃繖鏄畨鍏ㄧ殑锛屼絾鏄妸鐖剁被瀵硅薄璧嬪€肩粰瀛愮被鏁版嵁绫诲瀷锛岀敋鑷虫妸瀵硅薄璧嬪€肩粰涓庡畠姣棤缁ф壙鍏崇郴銆佸畬鍏ㄤ笉鐩稿共鐨勪竴涓暟鎹被鍨嬶紝鍒欐槸鍗遍櫓鍜屼笉鍚堟硶鐨勩€?/p> 銆傘€傘€傘€傘€傘€?/p> 绗﹀彿寮曠敤楠岃瘉锛氳繖涓樁娈靛彂鐢熷湪绗﹀彿寮曠敤杞负鐩存帴寮曠敤鐨勬椂鍊欙紝鍗冲疄闄呬笂鏄湪瑙f瀽闃舵涓繘琛岀殑銆?/p> 绗﹀彿寮曠敤涓€氳繃瀛楃涓叉弿杩扮殑鍏ㄩ檺瀹氬悕鏄惁鑳芥壘鍒板搴旂殑绫汇€?/p> 鍦ㄦ寚瀹氱被涓槸鍚﹀瓨鍦ㄧ鍚堟柟娉曠殑瀛楁鎻忚堪绗﹀強绠€鍗曞悕绉版墍鎻忚堪鐨勬柟娉曞拰瀛楁銆?/p> 绗﹀彿寮曠敤涓殑绫汇€佸瓧娈点€佹柟娉曠殑鍙闂€? private銆?protected. public銆?)銆?/p> 鏄惁鍙褰撳墠绫昏闂€?/p> 銆傘€傘€傘€傘€傘€?/p> 璇ラ樁娈垫槸涓?span class="mq-93">绫诲彉閲?/span>锛坰tatic锛夊垎閰嶅唴瀛樺苟璁剧疆闆跺€硷紝鍗崇被鍙缁忚繃鍑嗗闃舵鍏朵腑鐨勯潤鎬佸彉閲忓氨鏄彲浣跨敤鐨勪簡锛屼絾姝ゆ椂绫诲彉閲忕殑鍊艰繕涓嶆槸鎴戜滑鎯宠鐨勫€硷紝闇€瑕佺粡杩囧垵濮嬪寲闃舵鎵嶄細灏嗘垜浠笇鏈涚殑鍊艰祴鍊肩粰瀵瑰簲鐨勯潤鎬佸彉閲忋€?/p>
java.lang.NoSuchFieldError锛氭壘涓嶅埌瀛楁 java.lang.IllegalAccessError锛氫笉鍏锋湁璁块棶鏉冮檺 java.lang.NoSuchMethodError锛氭壘涓嶅埌鏂规硶 杩欐槸绫诲姞杞借繃绋嬩腑鐨勬渶鍚庝竴涓楠わ紝涓昏鏄敹闆嗙被鐨?span class="mq-101">闈欐€佸彉閲忕殑璧嬪€煎姩浣?/span>鍜?span class="mq-102">static鍧椾腑鐨勮鍙?/span>鍚堟垚<cinit>鏂规硶锛岄€氳繃璇ユ柟娉曟牴鎹垜浠殑鎰忔効涓?span class="mq-103">闈欐€佸彉閲忚祴鍊?/span>浠ュ強鎵цstatic鍧?/span>锛岃鏂规硶浼氳鍔犻攣锛岀‘淇濆绾跨▼鎯呭喌涓嬪彧鏈変竴涓嚎绋嬭兘鍒濆鍖栨垚鍔燂紝鍒╃敤璇ョ壒鎬у彲浠ュ疄鐜?span class="mq-105">鍗曚緥妯″紡銆傝櫄鎷熸満瑙勫畾浜?span class="mq-106">鏈変笖鍙湁楠岃瘉
鍑嗗
瑙f瀽
鍒濆鍖?/h2>
閬囧埌new銆乬etstatic銆乸utstatic鎴杋nvokestatic杩欏洓鏉″瓧鑺傜爜鎸囦护鏃躲€傝兘澶熺敓鎴愯繖鍥涙潯鎸囦护鐨勫吀鍨婮ava浠g爜鍦烘櫙鏈夛細
浣跨敤new鍏抽敭瀛楀疄渚嬪寲瀵硅薄鐨勬椂鍊欍€?/p>
璇诲彇鎴栬缃竴涓被鍨嬬殑闈欐€佸瓧娈碉紙琚玣inal淇グ銆佸凡鍦ㄧ紪璇戞湡鎶婄粨鏋滄斁鍏ュ父閲忔睜鐨勯潤鎬佸瓧娈甸櫎澶栵級鐨勬椂鍊欍€?/p>
璋冪敤涓€涓被鍨嬬殑闈欐€佹柟娉曠殑鏃跺€欍€?/p>
鍙嶅皠璋冪敤绫绘椂銆?/p>
褰撳垵濮嬪寲绫荤殑鏃跺€欙紝濡傛灉鍙戠幇鍏剁埗绫昏繕娌℃湁杩涜杩囧垵濮嬪寲锛屽垯闇€瑕佸厛瑙﹀彂鍏剁埗绫荤殑鍒濆鍖栥€?/p>
褰撹櫄鎷熸満鍚姩鏃讹紝鐢ㄦ埛闇€瑕佹寚瀹氫竴涓鎵ц鐨勪富绫伙紙鍖呭惈main()鏂规硶鐨勯偅涓被锛夛紝铏氭嫙鏈轰細鍏堝垵濮嬪寲杩欎釜涓荤被銆?/p>
褰撲娇鐢↗DK 7鏂板姞鍏ョ殑鍔ㄦ€佽瑷€鏀寔鏃讹紝濡傛灉涓€涓猨ava.lang.invoke.MethodHandle瀹炰緥鏈€鍚庣殑瑙f瀽缁撴灉涓篟EF_getStatic銆丷EF_putStatic銆丷EF_invokeStatic銆丷EF_newInvokeSpecial鍥涚绫诲瀷鐨勬柟娉曞彞鏌勶紝骞朵笖杩欎釜鏂规硶鍙ユ焺瀵瑰簲鐨勭被娌℃湁杩涜杩囧垵濮嬪寲锛屽垯闇€瑕佸厛瑙﹀彂鍏跺垵濮嬪寲銆?/p>
褰撲竴涓帴鍙d腑瀹氫箟浜咼DK 8鏂板姞鍏ョ殑榛樿鏂规硶锛堣default鍏抽敭瀛椾慨楗扮殑鎺ュ彛鏂规硶锛夋椂锛屽鏋滄湁杩欎釜鎺ュ彛鐨勫疄鐜扮被鍙戠敓浜嗗垵濮嬪寲锛岄偅璇ユ帴鍙h鍦ㄥ叾涔嬪墠琚垵濮嬪寲銆?/p>
涓嬮潰鍒嗘瀽鍑犱釜妗堜緥浠g爜锛岃鑰呬滑鍙互鍏堟€濊€冨悗鍐嶈繍琛屼唬鐮佺湅鐪嬪拰鑷繁鎯崇殑鏄惁涓€鏍枫€?/p>
妗堜緥涓€
鍏堝畾涔夊涓嬩袱涓被锛?/p>
public class SuperClazz {
static {
System.out.println("SuperClass init锛?);
}
public static int value=123;
public static final String HELLOWORLD="hello world";
public static final int WHAT = value;
}
public class SubClaszz extends SuperClazz {
static{
System.out.println("SubClass init锛?);
}
}
1
鐒跺悗杩涜涓嬮潰鐨勮皟鐢細
public class Initialization {
public static void main(String[]args){
Initialization initialization = new Initialization();
initialization.M1();
}
public void M1(){
System.out.println(SubClaszz.value);
}
}
1
绗竴涓渚嬫槸閫氳繃瀛愮被鍘诲紩鐢ㄧ埗绫讳腑鐨?span class="mq-223">闈欐€佸彉閲?/span>锛屼袱涓被閮戒細鍔犺浇鍜屽垵濮嬪寲涔堬紵鎵撳嵃缁撴灉鐪嬬湅锛?/p>
SuperClass init锛?br>123
鍙互鐪嬪埌鍙湁鐖剁被鍒濆鍖栦簡锛岄偅涔堢埗绫诲繀鐒舵槸鍔犺浇浜嗙殑锛岄棶棰樺氨鍦ㄤ簬瀛愮被鏈夋病鏈夎鍔犺浇鍛紵鍙互鍔犱笂鍙傛暟锛?XX:+TraceClassLoading鍐嶆墽琛岋紙璇ュ弬鏁扮殑浣滅敤灏辨槸鎵撳嵃琚姞杞戒簡鐨勭被锛夛紝鍙互鐪嬪埌瀛愮被鏄鍔犺浇浜嗙殑銆傛墍浠?span class="mq-230">閫氳繃瀛愮被寮曠敤鐖剁被闈欐€佸彉閲忥紝鐖跺瓙绫婚兘浼氳鍔犺浇锛屼絾鍙湁鐖剁被浼氳繘琛屽垵濮嬪寲銆?br class="mq-231">涓轰粈涔堝憿锛熷弽缂栬瘧鍚庡彲浠ョ湅鍒扮敓鎴愪簡濡備笅鎸囦护锛?/p>
0: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
3: getstatic #6 // Field ex7/init/SubClaszz.value:I
6: invokevirtual #7 // Method java/io/PrintStream.println:(I)V
9: return
鍏抽敭灏辨槸getstatic鎸囦护灏变細瑙﹀彂绫荤殑鍒濆鍖栵紝浣嗘槸涓轰粈涔堝瓙绫讳笉浼氬垵濮嬪寲鍛紵鍥犱负杩欎釜鍙橀噺鏄潵鑷簬鐖剁被鐨勶紝涓轰簡鎻愰珮鏁堢巼锛屾墍浠ヨ櫄鎷熸満杩涜浜嗕紭鍖栵紝杩欑鎯呭喌鍙渶瑕佸垵濮嬪寲鐖剁被灏辫浜嗐€?/p>
妗堜緥浜?/h3>
璋冪敤涓嬮潰鐨勬柟娉曪細
public void M2(){
SubClaszz[]sca = new SubClaszz[10];
}
1
鎵ц鍚庡彲浠ュ彂鐜帮紝浣跨敤鏁扮粍锛屼笉浼氳Е鍙戝垵濮嬪寲锛屼絾鐖跺瓙绫婚兘浼氳鍔犺浇銆?/p>
妗堜緥涓?/h3>
public void M3(){
System.out.println(SuperClazz.HELLOWORLD);
public void M3(){
System.out.println(SuperClazz.HELLOWORLD);
寮曠敤甯搁噺涓嶄細瑙﹀彂绫荤殑鍔犺浇鍜屽垵濮嬪寲锛屽洜涓哄父閲忓湪缂栬瘧鍚庡氨宸茬粡瀛樺湪褰撳墠class鐨勫父閲忔睜銆?/p>
妗堜緥鍥?/h3>
public void M4(){
System.out.println(SubClaszz.WHAT);
}
public void M4(){
System.out.println(SubClaszz.WHAT);
}
閫氳繃甯搁噺鍘诲紩鐢ㄥ叾瀹冪殑闈欐€佸彉閲忎細鍙戠敓浠€涔堝憿锛熻繖涓拰妗堜緥涓€缁撴灉鏄竴鏍风殑銆?/p>
绫诲姞杞藉櫒
绫诲姞杞藉櫒鍜屽弻浜插娲炬ā鍨?/h2>
鍦ㄦ垜浠钩鏃跺紑鍙戜腑锛岀‘瀹氫竴涓被闇€瑕侀€氳繃瀹屽叏闄愬畾鍚?/span>锛岃€屼笉鑳界畝鍗曠殑閫氳繃鍚嶅瓧锛屽洜涓哄湪涓嶅悓鐨勮矾寰勪笅鎴戜滑鏄彲浠ュ畾涔夊悓鍚嶇殑绫荤殑銆傞偅涔堝湪铏氭嫙鏈轰腑鍙堟槸鎬庝箞鍖哄垎绫荤殑鍛紵鍦ㄨ櫄鎷熸満涓渶瑕?span class="mq-319">绫诲姞杞藉櫒+瀹屽叏闄愬畾鍚?/span>涓€璧锋潵鎸囧畾涓€涓被鐨勫敮涓€鎬э紝鍗崇浉鍚岄檺瀹氬悕鐨勭被鑻ョ敱涓や釜涓嶅悓鐨勭被鍔犺浇鍣ㄥ姞杞斤紝閭h櫄鎷熸満灏变笉浼氭妸瀹冧滑褰撳仛涓€涓被銆備粠杩欓噷鎴戜滑鍙互鐪嬪嚭绫诲姞杞藉櫒涓€瀹氭槸鏈夊涓殑锛岄偅涔堜笉鍚岀殑绫诲姞杞藉櫒鏄€庝箞缁勭粐鐨勶紵瀹冧滑鍙堝垎鍒渶瑕佸姞杞藉摢浜涚被鍛紵
浠庤櫄鎷熻搴︾湅锛屽彧鏈変袱绉嶇被鍨嬬殑绫诲姞杞藉櫒锛?span class="mq-325">鍚姩绫诲姞杞藉櫒锛圔ootstrapClassLoader锛?span class="mq-326">鍜?/span>闈炲惎鍔ㄧ被鍔犺浇鍣?/span>銆傚墠鑰呮槸C++瀹炵幇锛屽睘浜庤櫄鎷熸満鐨勪竴閮ㄥ垎锛屽悗鑰呭垯鏄敱Java瀹炵幇鐨勶紝鐙珛浜庤櫄鎷熸満鐨勫閮紝骞朵笖鍏ㄩ儴缁ф壙鑷娊璞$被java.lang.ClassLoader銆?br class="mq-327">浣嗕粠Java鏈韩鏉ョ湅锛屼竴鐩翠繚鎸佺潃涓夊眰绫诲姞杞藉櫒銆?span class="mq-329">鍙屼翰濮旀淳鐨勭粨鏋勶紝褰撶劧闄や簡Java鏈韩鎻愪緵鐨勪笁灞傜被鍔犺浇鍣紝鎴戜滑杩樺彲浠ヨ嚜瀹氫箟瀹炵幇绫诲姞杞藉櫒銆傚涓婂浘锛屼笂闈笁涓氨鏄師鐢熺殑绫诲姞杞藉櫒锛屾瘡涓€涓兘鏄笅涓€涓被鍔犺浇鍣ㄧ殑鐖跺姞杞藉櫒锛屾敞鎰忚繖閲岄兘鏄噰鐢?span class="mq-331">缁勫悎鑰岄潪缁ф壙銆傚綋寮€濮嬪姞杞界被鏃讹紝棣栧厛浜ょ粰鐖跺姞杞藉櫒鍔犺浇锛岀埗鍔犺浇鍣ㄥ姞杞戒簡瀛愬姞杞藉櫒灏变笉鐢ㄥ啀鍔犺浇浜嗭紝鑰岃嫢鏄埗鍔犺浇鍣ㄥ姞杞戒笉浜嗭紝灏变細浜ょ粰瀛愬姞杞藉櫒鍔犺浇锛岃繖灏辨槸鍙屼翰濮旀淳鏈哄埗銆傝繖灏卞ソ姣斿伐浣滀腑閬囧埌浜嗘棤娉曞鐞嗙殑浜嬶紝浣犱細鍘昏绀虹洿鎺ラ瀵硷紝鐩存帴棰嗗澶勭悊涓嶄簡锛屽啀鎵句笂灞傞瀵硷紝鐒跺悗涓婂眰棰嗗瑙夊緱杩欐槸涓皬浜嬶紝涓嶇敤浠栦翰鑷姩鎵嬶紝灏辫浣犵殑鐩存帴棰嗗鍘诲仛锛屾帴鐫€浠栧張浜ょ粰浣犲幓鍋氱瓑绛夈€備笅闈㈡潵鐪嬬湅姣忎釜绫诲姞杞藉櫒鐨勫叿浣撲綔鐢細
BootstrapClassLoader锛氬惎鍔ㄧ被鍔犺浇鍣紝椤惧悕鎬濅箟锛岃繖涓被鍔犺浇鍣ㄤ富瑕佽礋璐e姞杞絁DK lib鍖咃紝浠ュ強-Xbootclasspath鍙傛暟鎸囧畾鐨勭洰褰曪紝骞朵笖铏氭嫙鏈哄鏂囦欢鍚嶈繘琛屼簡闄愬畾锛屼篃灏辨槸璇村嵆浣挎垜浠嚜宸卞啓涓猨ar鏀惧叆鍒颁笂杩扮洰褰曪紝涔熶笉浼氳鍔犺浇銆傜敱浜庤绫诲姞杞藉櫒鏄疌++浣跨敤锛屾墍浠ユ垜浠殑Java绋嬪簭涓棤娉曠洿鎺ュ紩鐢紝璋冪敤java.lang.ClassLoader.getClassLoader()鏂规硶鏃堕粯璁よ繑鍥炵殑鏄痭ull銆?/p>
ExtClassLoader锛氭墿灞曠被鍔犺浇鍣紝涓昏璐熻矗鍔犺浇JDK lib/ext鍖咃紝浠ュ強琚郴缁熷彉閲廽ava.ext.dirs鎸囧悜鐨勬墍鏈夌被搴擄紝杩欎釜绫诲簱鍙互瀛樻斁鎴戜滑鑷繁鍐欑殑閫氱敤jar銆?/p>
AppClassLoader锛氬簲鐢ㄧ▼搴忕被鍔犺浇鍣紝璐熻矗鍔犺浇鐢ㄦ埛classpath涓婄殑鎵€鏈夌被銆傚畠鏄痡ava.lang.ClassLoader.getSystemClassLoader()鐨勮繑鍥炲€硷紝涔熸槸鎴戜滑绋嬪簭鐨勯粯璁ょ被鍔犺浇鍣紙濡傛灉鎴戜滑娌℃湁鑷畾涔夌被鍔犺浇鍣ㄧ殑璇濓級銆?/p>
閫氳繃杩欎笁涓被鍔犺浇浠ュ強鍙屼翰濮旀淳鏈哄埗锛屼竴涓樉鑰屾槗瑙佺殑濂藉灏辨槸锛屼笉鍚岀殑绫婚殢瀹冪殑绫诲姞杞藉櫒澶╃劧鍏锋湁浜嗗姞杞戒紭鍏堢骇锛屽儚Object銆丼tring绛夌瓑杩欎簺鏍稿績绫诲簱鑷劧灏变細鍦ㄦ垜浠殑搴旂敤绋嬪簭绫讳箣鍓嶈鍔犺浇锛屼娇寰楃▼搴忔洿瀹夊叏锛屼笉浼氬嚭鐜伴敊璇紝Spring鐨勭埗瀛愬鍣ㄤ篃鏄繖鏍风殑涓€涓璁°€傞€氳繃涓嬮潰杩欐浠g爜鍙互鐪嬪埌姣忎釜绫绘墍瀵瑰簲鐨勭被鍔犺浇鍣細
public class ClassLoader {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader()); //鍚姩绫诲姞杞藉櫒
System.out.println(sun.net.spi.nameservice.dns.DNSNameService.class.getClassLoader());//鎷撳睍绫诲姞杞藉櫒
System.out.println(ClassLoader.class.getClassLoader());//搴旂敤绋嬪簭绫诲姞杞藉櫒
}
}
杈撳嚭锛?/p>
null
sun.misc.Launcher$ExtClassLoader@4b67cf4d
sun.misc.Launcher$AppClassLoader@14dad5dc
鐮村潖鍙屼翰濮旀淳妯″瀷
鍒氬垰鎴戜妇浜嗗伐浣滀腑鐨勪竴涓緥瀛愭潵璇存槑鍙屼翰濮旀淳鏈哄埗锛屼絾鐜板疄涓垜浠笉闇€瑕佷簨浜嬮兘鍘昏绀洪瀵硷紝鍚屾牱绫诲姞杞藉櫒涔熶笉鏄畬鍏ㄩ伒寰弻浜插娲炬満鍒讹紝鍦ㄥ繀瑕佺殑鏃跺€欐槸鍙互鎵撶牬杩欎釜瑙勫垯鐨勩€備笅闈㈠垪涓惧洓涓牬鍧忕殑鎯呭喌锛屽湪姝や箣鍓嶆垜浠渶瑕佸厛浜嗚В涓嬪弻浜?濮旀淳鐨勪唬鐮佸疄鐜板師鐞嗭紝鍦╦ava.lang.ClassLoader绫讳腑鏈変竴涓?span class="mq-415">loadClass浠ュ強findClass鏂规硶锛?/p>
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}
浠庝笂闈㈠彲浠ョ湅鍒伴鍏堟槸璋冪敤parent鍘诲姞杞界被锛屾病鏈夊姞杞藉埌鎵嶈皟鐢ㄨ嚜韬殑findClass鏂规硶鍘诲姞杞姐€備篃灏辨槸璇寸敤鎴峰湪瀹炵幇鑷畾涔夌被鍔犺浇鍣ㄧ殑鏃跺€欓渶瑕佽鐩栫殑鏄?span class="mq-581">fiindClass鑰屼笉鏄?span class="mq-582">loadClass锛岃繖鏍锋墠鑳芥弧瓒?span class="mq-583">鍙屼翰濮旀淳妯″瀷銆?br class="mq-584">涓嬮潰鍏蜂綋鏉ョ湅鐪嬬牬鍧忓弻浜插娲剧殑鍑犱釜鍦烘櫙銆?/p>
绗竴娆?/h3>
绗竴娆$牬鍧忔槸鍦ㄥ弻浜插娲炬ā鍨嬪嚭鐜颁箣鍓嶏紝 鍥犱负璇ユā鍨嬫槸鍦↗DK1.2涔嬪悗鎵嶅紩鍏ョ殑锛岄偅涔堝湪姝や箣鍓嶏紝鎶借薄绫籮ava.lang.ClassLoader灏卞凡缁忓瓨鍦ㄤ簡锛岀敤鎴疯嚜瀹氫箟鐨勭被鍔犺浇鍣ㄩ兘浼氬幓瑕嗙洊璇ョ被涓殑loadClass鏂规硶锛屾墍浠ュ弻浜插娲炬ā鍨嬪嚭鐜板悗锛屽氨鏃犳硶閬垮厤鐢ㄦ埛瑕嗙洊璇ユ柟娉曪紝鍥犳鏂板浜?span class="mq-588">findClass寮曞鐢ㄦ埛鍘昏鐩栬鏂规硶瀹炵幇鑷繁鐨勭被鍔犺浇閫昏緫銆?/p>
SPI
绗簩娆$牬鍧忔槸鐢变簬杩欎釜妯″瀷鏈韩缂洪櫡瀵艰嚧鐨勶紝鍥犱负璇ユā鍨嬩繚璇佷簡绫荤殑鍔犺浇浼樺厛绾э紝浣嗘槸鏈変簺鎺ュ彛鏄疛ava瀹氫箟鍦ㄦ牳蹇冪被搴撲腑锛屼絾鍏蜂綋鐨勬湇鍔″疄鐜版槸鐢辩敤鎴锋彁渚涚殑锛岃繖鏃跺€欏氨涓嶅緱涓嶇牬鍧忚妯″瀷鎵嶈兘瀹炵幇锛屽吀鍨嬬殑灏辨槸Java涓殑SPI鏈哄埗锛堝SPI涓嶄簡瑙g殑璇昏€呭彲浠ョ炕闃呮垜涔嬪墠鐨勬枃绔犳垨鏄叾瀹冭祫鏂欙紝杩欓噷涓嶈繘琛岄槓杩帮級銆侸
DBC鐨勯┍鍔ㄥ姞杞藉氨鏄疭PI瀹炵幇鐨勶紝鎵€浠ョ洿鎺ョ湅鍒?span class="mq-592">java.sql.DriverManager绫伙紝璇ョ被涓湁涓€涓潤鎬佸垵濮嬪寲鍧楋細
static {
loadInitialDrivers();
println("JDBC DriverManager initialized");
}
private static void loadInitialDrivers() {
String drivers;
try {
drivers = AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("jdbc.drivers");
}
});
} catch (Exception ex) {
drivers = null;
}
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
Iterator<Driver> driversIterator = loadedDrivers.iterator();
try{
while(driversIterator.hasNext()) {
driversIterator.next();
}
} catch(Throwable t) {
// Do nothing
}
return null;
}
});
println("DriverManager.initialize: jdbc.drivers = " + drivers);
if (drivers == null || drivers.equals("")) {
return;
}
String[] driversList = drivers.split(":");
println("number of Drivers:" + driversList.length);
for (String aDriver : driversList) {
try {
println("DriverManager.Initialize: loading " + aDriver);
Class.forName(aDriver, true,
ClassLoader.getSystemClassLoader());
} catch (Exception ex) {
println("DriverManager.Initialize: load failed: " + ex);
}
}
}
51
涓昏鐪?span class="mq-800">ServiceLoader.load鏂规硶锛岃繖涓氨鏄€氳繃SPI鍘诲姞杞芥垜浠紩鍏?span class="mq-801">java.sql.Driver瀹炵幇绫伙紙姣斿寮曞叆mysql鐨勯┍鍔ㄥ寘灏辨槸com.mysql.cj.jdbc.Driver锛夛細
public static <S> ServiceLoader<S> load(Class<S> service) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return ServiceLoader.load(service, cl);
}
杩欎釜鏂规硶涓昏鏄粠褰撳墠绾跨▼涓幏鍙栫被鍔犺浇鍣紝鐒跺悗閫氳繃杩欎釜绫诲姞杞藉櫒鍘诲姞杞?span class="mq-840">椹卞姩瀹炵幇绫?/span>锛堣繖涓彨绾跨▼涓婁笅鏂囩被鍔犺浇鍣?/span>锛屾垜浠篃鍙互浣跨敤杩欎釜鎶€宸у幓鎵撶牬鍙屼翰濮旀淳锛夛紝閭h繖閲屼細鑾峰彇鍒板摢涓€涓被鍔犺浇鍣ㄥ憿锛熷叿浣撶殑璁剧疆鏄湪sun.misc.Launcher绫荤殑鏋勯€犲櫒涓細
public Launcher() {
Launcher.ExtClassLoader var1;
try {
var1 = Launcher.ExtClassLoader.getExtClassLoader();
} catch (IOException var10) {
throw new InternalError("Could not create extension class loader", var10);
}
try {
this.loader = Launcher.AppClassLoader.getAppClassLoader(var1);
} catch (IOException var9) {
throw new InternalError("Could not create application class loader", var9);
}
Thread.currentThread().setContextClassLoader(this.loader);
String var2 = System.getProperty("java.security.manager");
if (var2 != null) {
SecurityManager var3 = null;
if (!"".equals(var2) && !"default".equals(var2)) {
try {
var3 = (SecurityManager)this.loader.loadClass(var2).newInstance();
} catch (IllegalAccessException var5) {
} catch (InstantiationException var6) {
} catch (ClassNotFoundException var7) {
} catch (ClassCastException var8) {
}
} else {
var3 = new SecurityManager();
}
if (var3 == null) {
throw new InternalError("Could not create SecurityManager: " + var2);
}
System.setSecurityManager(var3);
}
鍙互鐪嬪埌璁剧疆鐨勫氨鏄?span class="mq-1018">AppClassLoader銆備綘鍙兘浼氭湁鐐圭枒鎯戯紝杩欎釜绫诲姞杞藉櫒鍔犺浇绫荤殑鏃跺€欎笉涔熸槸鍏堣皟鐢ㄧ埗绫诲姞杞藉櫒鍔犺浇涔堬紝鎬庝箞灏辨墦鐮村弻浜插娲句簡鍛紵鍏跺疄鎵撶牬鍙屼翰濮旀淳鎸囩殑灏辨槸绫荤殑灞傛缁撴瀯锛屽欢浼告剰鎬濆氨鏄被鐨勫姞杞戒紭鍏堢骇锛岃繖閲屾湰搴旇鏄湪鍔犺浇鏍稿績绫诲簱鐨勬椂鍊欏嵈鎻愬墠灏嗘垜浠簲鐢ㄧ▼搴忎腑鐨勭被搴撶粰鍔犺浇鍒拌櫄鎷熸満涓潵浜嗐€?/p>
Tomcat
涓婂浘鏄疶omcat绫诲姞杞界殑绫诲浘锛屽墠闈笁涓笉鐢ㄨ锛?span class="mq-1022">CommonClassLoader銆?span class="mq-1023">CatalinaClassLoader銆?span class="mq-1024">SharedClassLoader銆?span class="mq-1025">WebAppClassLoader銆?span class="mq-1026">JspClassLoader鍒欐槸Tomcat鑷繁瀹炵幇鐨勭被鍔犺浇鍣紝鍒嗗埆鍔犺浇common鍖?/span>銆?span class="mq-1028">server鍖?/span>銆?span class="mq-1029">shared鍖?/span>銆?span class="mq-1030">WebApp/WEB-INF/lib鍖?/span>浠ュ強JSP鏂囦欢锛屽墠闈笁涓湪tomcat 6涔嬪悗宸茬粡鍚堝苟鍒版牴鐩綍涓嬬殑lib鐩綍涓嬨€傝€?span class="mq-1032">WebAppClassLoader鍒欐槸姣忎竴涓簲鐢ㄧ▼搴忓搴斾竴涓紝JspClassLoader鏄瘡涓€涓狫SP鏂囦欢閮戒細瀵瑰簲涓€涓紝骞朵笖杩欎袱涓被鍔犺浇鍣ㄩ兘娌℃湁鐖剁被鍔犺浇鍣紝杩欎篃灏辫繚鑳屼簡鍙屼翰濮旀淳妯″瀷銆?br class="mq-1034">涓轰粈涔堟瘡涓簲鐢ㄧ▼搴忛渶瑕佸崟鐙殑WebAppClassLoader瀹炰緥锛熷洜涓烘瘡涓簲鐢ㄧ▼搴忛渶瑕佸郊姝ら殧绂伙紝鍋囧鍦ㄤ袱涓簲鐢ㄤ腑瀹氫箟浜嗕竴鏍风殑绫伙紙瀹屽叏闄愬畾鍚嶏級锛屽鏋滈伒寰弻浜插娲鹃偅灏卞彧浼氬瓨鍦ㄤ竴浠戒簡锛屽彟澶栦笉鍚岀殑搴旂敤杩樻湁鍙兘渚濊禆鍚屼竴涓被搴撶殑涓嶅悓鐗堟湰锛岃繖涔熼渶瑕侀殧绂伙紝鎵€浠ユ瘡涓€涓簲鐢ㄧ▼搴忛兘浼氬搴斾竴涓?span class="mq-1037">WebAppClassLoader锛屽畠浠叡浜殑绫诲簱鍙互璁?span class="mq-1038">SharedClassLoader鍔犺浇锛屽彟澶栬繖浜涚被鍔犺浇鍔犺浇鐨勭被瀵筎omcat鏈韩鏉ヨ涔熸槸闅旂鐨勶紙CatalinaClassLoader鍔犺浇鐨勶級銆?br class="mq-1039">涓轰粈涔堟瘡涓狫SP鏂囦欢闇€瑕佸搴斿崟鐙殑涓€涓?span class="mq-1040">JspClassLoader瀹炰緥锛熻繖鏄敱浜嶫SP鏄敮鎸佽繍琛屾椂淇敼鐨勶紝淇敼鍚庝細涓㈠純鎺変箣鍓嶇紪璇戠敓鎴愮殑class锛屽苟閲嶆柊鐢熸垚涓€涓?span class="mq-1041">JspClassLoader瀹炰緥鍘诲姞杞芥柊鐨刢lass銆?br class="mq-1042">浠ヤ笂灏辨槸Tomcat涓轰粈涔堣鎵撶牬鍙屼翰濮旀淳妯″瀷鐨勫師鍥犮€?/p>
OSGI
OSGI鏄敤浜庡疄鐜版ā鍧楃儹閮ㄧ讲锛屽儚Eclipse鐨勬彃浠剁郴缁熷氨鏄埄鐢∣SGI瀹炵幇鐨勶紝杩欎釜鎶€鏈潪甯稿鏉傚悓鏃朵娇鐢ㄧ殑涔熻秺鏉ヨ秺灏戜簡锛屾劅鍏磋叮鐨勮鑰呭彲鑷鏌ラ槄璧勬枡瀛︿範锛岃繖閲屼笉鍐嶈繘琛岄槓杩般€?/p>
鎬荤粨
绫诲姞杞界殑杩囩▼璁╂垜浠簡瑙e埌涓€涓被鏄浣曡鍔犺浇鍒板唴瀛樹腑锛岄渶瑕佺粡杩囧摢浜涢樁娈碉紱鑰岀被鍔犺浇鍣ㄥ拰鍙屼翰濮旀淳妯″瀷鍒欐槸鍛婅瘔鎴戜滑搴旇鎬庝箞鍘诲姞杞界被銆佺被鐨勫姞杞戒紭鍏堢骇鏄€庢牱鐨勶紝鍏朵腑鐨勮璁℃€濇兂鎴戜滑涔熷彲浠ュ涔犲€熼壌锛涙渶鍚庨渶瑕佹繁鍒荤悊瑙g殑鏄负浠€涔堥渶瑕佹墦鐮村弻浜插娲撅紝鍦ㄩ亣鍒扮浉搴旂殑鍦烘櫙鏃跺簲璇ユ€庝箞鍋氥€?/p>
end
骞茶揣鍒嗕韩
鍥炲銆?01銆戣幏鍙?nbsp; 銆婂叏鏍堟灦鏋勫笀銆嬭绋?nbsp;
鍥炲銆?02銆戣幏鍙?銆婇珮绾у疄鎴樻灦鏋勫笀璇剧▼銆嬭绋?nbsp;
鍥炲銆?03銆戣幏鍙?銆婇珮绾ф灦鏋勫笀鍏ㄥ鏁欑▼銆嬭绋?nbsp;
鍥炲銆?05銆戣幏鍙?nbsp;銆婂ぇ鏁版嵁涔婬ive瑙嗛銆嬭绋?nbsp;
鍥炲銆?08銆戣幏鍙?nbsp;銆妚3 RabbitMQ娑堟伅涓棿浠舵妧鏈簿璁层€嬭绋?nbsp;
鍥炲銆?09銆戣幏鍙?nbsp; 銆婂ぇ鏁版嵁涔媄ookeeper瑙嗛銆嬭绋?nbsp;
鏇村瀛︿範璧勬枡璇疯仈绯绘垜鍝︼紒锛侊紒
鏈夌儹闂ㄦ帹鑽?/span>馃憞
1.
2.
3.
4.
鎶€鏈€丣VM銆丣ava骞跺彂銆丼SM銆佸井鏈嶅姟銆佹暟鎹簱銆佹暟鎹粨鏋勭瓑绛夈€?/span>
鍠滄鐨勭殑璇濓紝鐐逛釜鈥?/strong>鍦ㄧ湅鈥?/strong>榧撳姳涓€涓嬪惂
璋㈣阿 (*^__^*锛?/strong>
以上是关于娣卞叆鎺㈢┒JVM涔嬬被鍔犺浇涓庡弻浜插娲炬満鍒?/h1>