CRUD鎼爾涓や笁骞翠簡锛屾€庝箞闃呰Spring婧愮爜锛?/a>
Posted 灏忓倕鍝?,
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CRUD鎼爾涓や笁骞翠簡锛屾€庝箞闃呰Spring婧愮爜锛?/a>相关的知识,希望对你有一定的参考价值。
浣滆€咃細灏忓倕鍝?br>鍗氬锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fbugstack.cn" rel="nofollow">https://bugstack.cn
娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/blockquote>馃懆鈥嶐煉昏繛璇诲悓浜嬪啓鐨勪唬鐮侀兘璐瑰姴锛岃繕璇籗pring锛?/strong>
鍜嬬殑锛孲pring 寰堥毦璇伙紒
杩欎釜涓庢垜浠爜鍐滄湞澶曠浉澶勭殑 Spring锛屽氨鍍忕潯鍦ㄤ綘韬竟鐨勫濡囷紝浣犵煡閬撴壘濂硅鍚冦€佽鍠濄€佽闆惰姳閽便€佽涔扮毊鑲ゃ€備絾浣犱笉鐭ラ亾濂圭殑浠撳簱鍏辨湁澶氬皯瀛樼伯銆佷篃涓嶇煡閬撳ス鏄拱浜嗙悊璐㈣繕鏄瓨浜嗛摱琛屻€?em>馃崙寮€涓帺绗戯紝鎺ヤ笅鏉ユ垜瑕佹缁忎簡锛?/em>
涓€銆佷负浠€涔圫pring闅捐鎳傦紵
涓轰粈涔?Spring 澶╁ぉ鐢紝浣嗚鎯冲幓璇讳竴璇绘簮鐮侊紝鎬庝箞灏遍偅涔堥毦锛佸洜涓虹敱
Java鍜孞2EE寮€鍙戦鍩熺殑涓撳
Rod Johnson 浜?2002 骞存彁鍑哄苟闅忓悗鍒涘缓鐨?Spring 妗嗘灦锛岄殢鐫€ JDK 鐗堟湰鍜屽競鍦洪渶瑕佸彂灞曡嚦浠婏紝鑷充粖瀹冨凡缁忚秺鏉ヨ秺澶т簡锛?/p>褰撲綘闃呰瀹冪殑婧愮爜浣犱細鎰熻锛?/p>
- 鎬庝箞杩欎唬鐮佽烦鏉ヨ烦鍘荤殑锛屾牴鏈笉鏄儚鑷繁鍐欎唬鐮佷竴鏍烽偅涔?code>鍗曠函
- 涓轰粈涔堥偅涔堝鐨勬帴鍙e拰鎺ュ彛缁ф壙锛岀被A缁ф壙鐨勭被B杩樺疄鐜颁簡绫籄瀹炵幇鐨勬帴鍙
- 绠€鍗曞伐鍘傘€佸伐鍘傛柟娉曘€佷唬鐞嗘ā寮忋€佽瀵熻€呮ā寮忥紝鎬庝箞鐢ㄤ簡浼氭湁杩欐牱澶氱殑璁捐妯″紡浣跨敤
- 鍙堟槸璧勬簮鍔犺浇銆佸張鏄簲鐢ㄤ笂涓嬫枃銆佸張鏄疘OC銆佸張鏄疉OP銆佽疮绌跨殑杩樻湁 Bean 鐨勫0鏄庡懆鏈燂紝涓€鐗囦竴鐗囩殑浠g爜浠庡摢涓嬫墜
鎬庢牱锛岃繖灏辨槸浣犲湪闃呰 Spring 閬囧埌鐨勪竴浜涘垪闂鍚э紵鍏跺疄涓嶆浣犵敋鑷冲彲浠ヨ鍙鏄粠浜嬭繖涓涓氱殑鐮佸啘锛屾兂璇?Spring 婧愮爜閮戒細鏈夌涓嶇煡閬撲粠鍝笅鎵嬬殑鎰熻銆傛墍浠ユ垜鎯充簡涓姙娉曪紝鏃㈢劧 Spring 澶ぇ涓嶅ソ浜嗚В锛岄偅涔堟垜灏卞皾璇曚粠涓€涓皬鐨?Spring 寮€濮嬶紝鎵嬫捀 瀹炵幇涓€涓?Spring 鏄笉鍙互鐞嗚В鐨勬洿濂斤紝鍒鏁堟灉杩樼湡涓嶉敊锛?code>鍦ㄨ姳浜嗗皢杩?涓湀鐨勬椂闂达紝瀹炵幇涓€涓畝鍗曠増鏈殑 Spring 鍚?/code> 鐜板湪瀵?Spring 鐨勭悊瑙o紝鏈変簡寰堝ぇ鐨勬彁鍗囷紝涔熻兘璇绘噦 Spring 鐨勬簮鐮佷簡銆?/p>
浜屻€佸垎浜墜鎾?Spring
閫氳繃杩欐牱鎵嬪啓绠€鍖栫増 Spring 妗嗘灦锛屼簡瑙?Spring 鏍稿績鍘熺悊銆傚湪鎵嬪啓鐨勮繃绋嬩腑浼氱畝鍖?Spring 婧愮爜锛屾憳鍙栨暣浣撴鏋朵腑鐨勬牳蹇冮€昏緫锛岀畝鍖栦唬鐮佸疄鐜拌繃绋嬶紝淇濈暀鏍稿績鍔熻兘锛屼緥濡傦細IOC銆丄OP銆丅ean鐢熷懡鍛ㄦ湡銆佷笂涓嬫枃銆佷綔鐢ㄥ煙銆佽祫婧愬鐞嗙瓑鍐呭瀹炵幇銆?/p>
婧愮爜锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgithub.com%2Ffuzhengwei%2Fsmall-spring" rel="nofollow">https://github.com/fuzhengwei/small-spring
1. 瀹炵幇涓€涓畝鍗曠殑Bean瀹瑰櫒
鍑℃槸鍙互瀛樻斁鏁版嵁鐨勫叿浣撴暟鎹粨鏋勫疄鐜帮紝閮藉彲浠ョО涔嬩负瀹瑰櫒銆備緥濡傦細ArrayList銆丩inkedList銆丠ashSet绛夛紝浣嗗湪 Spring Bean 瀹瑰櫒鐨勫満鏅笅锛屾垜浠渶瑕佷竴绉嶅彲浠ョ敤浜庡瓨鏀惧拰鍚嶇О绱㈠紩寮忕殑鏁版嵁缁撴瀯锛屾墍浠ラ€夋嫨 HashMap 鏄渶鍚堥€備笉杩囩殑銆?/p>
杩欓噷绠€鍗曚粙缁嶄竴涓?HashMap锛孒ashMap 鏄竴绉嶅熀浜庢壈鍔ㄥ嚱鏁般€佽礋杞藉洜瀛愩€佺孩榛戞爲杞崲绛夋妧鏈唴瀹癸紝褰㈡垚鐨勬媺閾惧鍧€鐨勬暟鎹粨鏋勶紝瀹冭兘璁╂暟鎹洿鍔犳暎鍒楃殑鍒嗗竷鍦ㄥ搱甯屾《浠ュ強纰版挒鏃跺舰鎴愮殑閾捐〃鍜岀孩榛戞爲涓娿€傚畠鐨勬暟鎹粨鏋勪細灏藉彲鑳芥渶澶ч檺搴︾殑璁╂暣涓暟鎹鍙栫殑澶嶆潅搴﹀湪 O(1) ~ O(Logn) ~O(n)涔嬮棿锛屽綋鐒跺湪鏋佺鎯呭喌涓嬩篃浼氭湁 O(n) 閾捐〃鏌ユ壘鏁版嵁杈冨鐨勬儏鍐点€備笉杩囨垜浠粡杩?0涓囨暟鎹殑鎵板姩鍑芥暟鍐嶅鍧€楠岃瘉娴嬭瘯锛屾暟鎹細鍧囧寑鐨勬暎鍒楀湪鍚勪釜鍝堝笇妗剁储寮曚笂锛屾墍浠?HashMap 闈炲父閫傚悎鐢ㄥ湪 Spring Bean 鐨勫鍣ㄥ疄鐜颁笂銆?/p>
鍙﹀涓€涓畝鍗曠殑 Spring Bean 瀹瑰櫒瀹炵幇锛岃繕闇€ Bean 鐨勫畾涔夈€佹敞鍐屻€佽幏鍙栦笁涓熀鏈楠わ紝绠€鍖栬璁″涓嬶紱
- 瀹氫箟锛欱eanDefinition锛屽彲鑳借繖鏄綘鍦ㄦ煡闃?Spring 婧愮爜鏃剁粡甯哥湅鍒扮殑涓€涓被锛屼緥濡傚畠浼氬寘鎷?singleton銆乸rototype銆丅eanClassName 绛夈€備絾鐩墠鎴戜滑鍒濇瀹炵幇浼氭洿鍔犵畝鍗曠殑澶勭悊锛屽彧瀹氫箟涓€涓?Object 绫诲瀷鐢ㄤ簬瀛樻斁瀵硅薄銆?/li>
- 娉ㄥ唽锛氳繖涓繃绋嬪氨鐩稿綋浜庢垜浠妸鏁版嵁瀛樻斁鍒?HashMap 涓紝鍙笉杩囩幇鍦?HashMap 瀛樻斁鐨勬槸瀹氫箟浜嗙殑 Bean 鐨勫璞′俊鎭€?/li>
- 鑾峰彇锛氭渶鍚庡氨鏄幏鍙栧璞★紝Bean 鐨勫悕瀛楀氨鏄痥ey锛孲pring 瀹瑰櫒鍒濆鍖栧ソ Bean 浠ュ悗锛屽氨鍙互鐩存帴鑾峰彇浜嗐€?/li>
2. 杩愮敤璁捐妯″紡锛屽疄鐜?Bean 鐨勫畾涔夈€佹敞鍐屻€佽幏鍙?/h3>
灏?Spring Bean 瀹瑰櫒瀹屽杽璧锋潵锛岄鍏堥潪甯搁噸瑕佺殑涓€鐐规槸鍦?Bean 娉ㄥ唽鐨勬椂鍊欏彧娉ㄥ唽涓€涓被淇℃伅锛岃€屼笉浼氱洿鎺ユ妸瀹炰緥鍖栦俊鎭敞鍐屽埌 Spring 瀹瑰櫒涓€傞偅涔堝氨闇€瑕佷慨鏀?BeanDefinition 涓殑灞炴€?Object 涓?Class锛屾帴涓嬫潵鍦ㄩ渶瑕佸仛鐨勫氨鏄湪鑾峰彇 Bean 瀵硅薄鏃堕渶瑕佸鐞?Bean 瀵硅薄鐨勫疄渚嬪寲鎿嶄綔浠ュ強鍒ゆ柇褰撳墠鍗曚緥瀵硅薄鍦ㄥ鍣ㄤ腑鏄惁宸茬粡缂撳瓨璧锋潵浜嗐€傛暣浣撹璁″鍥?3-1
- 棣栧厛鎴戜滑闇€瑕佸畾涔?BeanFactory 杩欐牱涓€涓?Bean 宸ュ巶锛屾彁渚?Bean 鐨勮幏鍙栨柟娉?
getBean(String name)
锛屼箣鍚庤繖涓?Bean 宸ュ巶鎺ュ彛鐢辨娊璞$被 AbstractBeanFactory 瀹炵幇銆傝繖鏍蜂娇鐢?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fbugstack.cn%2Fitstack-demo-design%2F2020%2F07%2F07%2F%25E9%2587%258D%25E5%25AD%25A6-Java-%25E8%25AE%25BE%25E8%25AE%25A1%25E6%25A8%25A1%25E5%25BC%258F-%25E5%25AE%259E%25E6%2588%2598%25E6%25A8%25A1%25E6%259D%25BF%25E6%25A8%25A1%25E5%25BC%258F.html" rel="nofollow">妯℃澘妯″紡鐨勮璁℃柟寮忥紝鍙互缁熶竴鏀跺彛閫氱敤鏍稿績鏂规硶鐨勮皟鐢ㄩ€昏緫鍜屾爣鍑嗗畾涔夛紝涔熷氨寰堝ソ鐨勬帶鍒朵簡鍚庣画鐨勫疄鐜拌€呬笉鐢ㄥ叧蹇冭皟鐢ㄩ€昏緫锛屾寜鐓х粺涓€鏂瑰紡鎵ц銆傞偅涔堢被鐨勭户鎵胯€呭彧闇€瑕佸叧蹇冨叿浣撴柟娉曠殑閫昏緫瀹炵幇鍗冲彲銆?/li>- 閭d箞鍦ㄧ户鎵挎娊璞$被 AbstractBeanFactory 鍚庣殑 AbstractAutowireCapableBeanFactory 灏卞彲浠ュ疄鐜扮浉搴旂殑鎶借薄鏂规硶浜嗭紝鍥犱负 AbstractAutowireCapableBeanFactory 鏈韩涔熸槸涓€涓娊璞$被锛屾墍浠ュ畠鍙細瀹炵幇灞炰簬鑷繁鐨勬娊璞℃柟娉曪紝鍏朵粬鎶借薄鏂规硶鐢辩户鎵?AbstractAutowireCapableBeanFactory 鐨勭被瀹炵幇銆傝繖閲屽氨浣撶幇浜嗙被瀹炵幇杩囩▼涓殑鍚勫徃鍏惰亴锛屼綘鍙渶瑕佸叧蹇冨睘浜庝綘鐨勫唴瀹癸紝涓嶆槸浣犵殑鍐呭锛屼笉瑕佸弬涓庛€?/li>
- 鍙﹀杩欓噷杩樻湁鍧楅潪甯搁噸瑕佺殑鐭ヨ瘑鐐癸紝灏辨槸鍏充簬鍗曚緥 SingletonBeanRegistry 鐨勬帴鍙e畾涔夊疄鐜帮紝鑰?DefaultSingletonBeanRegistry 瀵规帴鍙e疄鐜板悗锛屼細琚娊璞$被 AbstractBeanFactory 缁ф壙銆傜幇鍦?AbstractBeanFactory 灏辨槸涓€涓潪甯稿畬鏁翠笖寮哄ぇ鐨勬娊璞$被浜嗭紝涔熻兘闈炲父濂界殑浣撶幇鍑哄畠瀵规ā鏉挎ā寮忕殑鎶借薄瀹氫箟銆?/li>
3. 鍩轰簬Cglib瀹炵幇鍚瀯閫犲嚱鏁扮殑绫诲疄渚嬪寲绛栫暐
濉钩杩欎釜鍧戠殑鎶€鏈璁′富瑕佽€冭檻涓ら儴鍒嗭紝涓€涓槸涓叉祦绋嬩粠鍝悎鐞嗙殑鎶婃瀯閫犲嚱鏁扮殑鍏ュ弬淇℃伅浼犻€掑埌瀹炰緥鍖栨搷浣滈噷锛屽彟澶栦竴涓槸鎬庝箞鍘诲疄渚嬪寲鍚湁鏋勯€犲嚱鏁扮殑瀵硅薄銆?/p>
- 鍙傝€?Spring Bean 瀹瑰櫒婧愮爜鐨勫疄鐜版柟寮忥紝鍦?BeanFactory 涓坊鍔?
Object getBean(String name, Object... args)
鎺ュ彛锛岃繖鏍峰氨鍙互鍦ㄨ幏鍙?Bean 鏃舵妸鏋勯€犲嚱鏁扮殑鍏ュ弬淇℃伅浼犻€掕繘鍘讳簡銆?/li>- 鍙﹀涓€涓牳蹇冪殑鍐呭鏄娇鐢ㄤ粈涔堟柟寮忔潵鍒涘缓鍚湁鏋勯€犲嚱鏁扮殑 Bean 瀵硅薄鍛紵杩欓噷鏈変袱绉嶆柟寮忓彲浠ラ€夋嫨锛屼竴涓槸鍩轰簬 Java 鏈韩鑷甫鐨勬柟娉?
DeclaredConstructor
锛屽彟澶栦竴涓槸浣跨敤 Cglib 鏉ュ姩鎬佸垱寤?Bean 瀵硅薄銆?em>Cglib 鏄熀浜庡瓧鑺傜爜妗嗘灦 ASM 瀹炵幇锛屾墍浠ヤ綘涔熷彲浠ョ洿鎺ラ€氳繃 ASM 鎿嶄綔鎸囦护鐮佹潵鍒涘缓瀵硅薄4. 涓築ean瀵硅薄娉ㄥ叆灞炴€у拰渚濊禆Bean鐨勫姛鑳藉疄鐜?/h3>
閴翠簬灞炴€у~鍏呮槸鍦?Bean 浣跨敤
newInstance
鎴栬€?Cglib
鍒涘缓鍚庯紝寮€濮嬭ˉ鍏ㄥ睘鎬т俊鎭紝閭d箞灏卞彲浠ュ湪绫?AbstractAutowireCapableBeanFactory
鐨?createBean 鏂规硶涓坊鍔犺ˉ鍏ㄥ睘鎬ф柟娉曘€?em>杩欓儴鍒嗗ぇ瀹跺湪瀹炰範鐨勮繃绋嬩腑涔熷彲浠ュ鐓pring婧愮爜瀛︿範锛岃繖閲岀殑瀹炵幇涔熸槸Spring鐨勭畝鍖栫増锛屽悗缁鐓у涔犱細鏇村姞鏄撲簬鐞嗚В
- 灞炴€у~鍏呰鍦ㄧ被瀹炰緥鍖栧垱寤轰箣鍚庯紝涔熷氨鏄渶瑕佸湪
AbstractAutowireCapableBeanFactory
鐨?createBean 鏂规硶涓坊鍔?applyPropertyValues
鎿嶄綔銆?/li>- 鐢变簬鎴戜滑闇€瑕佸湪鍒涘缓Bean鏃跺€欏~鍏呭睘鎬ф搷浣滐紝閭d箞灏遍渶瑕佸湪 bean 瀹氫箟 BeanDefinition 绫讳腑锛屾坊鍔?PropertyValues 淇℃伅銆?/li>
- 鍙﹀鏄~鍏呭睘鎬т俊鎭繕鍖呮嫭浜?Bean 鐨勫璞$被鍨嬶紝涔熷氨鏄渶瑕佸啀瀹氫箟涓€涓?BeanReference锛岄噷闈㈠叾瀹炲氨鏄竴涓畝鍗曠殑 Bean 鍚嶇О锛屽湪鍏蜂綋鐨勫疄渚嬪寲鎿嶄綔鏃惰繘琛岄€掑綊鍒涘缓鍜屽~鍏咃紝涓?Spring 婧愮爜瀹炵幇涓€鏍枫€?em>Spring 婧愮爜涓?BeanReference 鏄竴涓帴鍙?/em>
5. 璁捐涓庡疄鐜拌祫婧愬姞杞藉櫒锛屼粠Spring.xml瑙f瀽鍜屾敞鍐孊ean瀵硅薄
渚濈収鏈珷鑺傜殑闇€姹傝儗鏅紝鎴戜滑闇€瑕佸湪鐜版湁鐨?Spring 妗嗘灦闆忓舰涓坊鍔犱竴涓祫婧愯В鏋愬櫒锛屼篃灏辨槸鑳借鍙朿lasspath銆佹湰鍦版枃浠跺拰浜戞枃浠剁殑閰嶇疆鍐呭銆傝繖浜涢厤缃唴瀹瑰氨鏄儚浣跨敤 Spring 鏃堕厤缃殑 Spring.xml 涓€鏍凤紝閲岄潰浼氬寘鎷?Bean 瀵硅薄鐨勬弿杩板拰灞炴€т俊鎭€?鍦ㄨ鍙栭厤缃枃浠朵俊鎭悗锛屾帴涓嬫潵灏辨槸瀵归厤缃枃浠朵腑鐨?Bean 鎻忚堪淇℃伅瑙f瀽鍚庤繘琛屾敞鍐屾搷浣滐紝鎶?Bean 瀵硅薄娉ㄥ唽鍒?Spring 瀹瑰櫒涓€傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 璧勬簮鍔犺浇鍣ㄥ睘浜庣浉瀵圭嫭绔嬬殑閮ㄥ垎锛屽畠浣嶄簬 Spring 妗嗘灦鏍稿績鍖呬笅鐨処O瀹炵幇鍐呭锛屼富瑕佺敤浜庡鐞咰lass銆佹湰鍦板拰浜戠幆澧冧腑鐨勬枃浠朵俊鎭€?/li>
- 褰撹祫婧愬彲浠ュ姞杞藉悗锛屾帴涓嬫潵灏辨槸瑙f瀽鍜屾敞鍐?Bean 鍒?Spring 涓殑鎿嶄綔锛岃繖閮ㄥ垎瀹炵幇闇€瑕佸拰 DefaultListableBeanFactory 鏍稿績绫荤粨鍚堣捣鏉ワ紝鍥犱负浣犳墍鏈夌殑瑙f瀽鍚庣殑娉ㄥ唽鍔ㄤ綔锛岄兘浼氭妸 Bean 瀹氫箟淇℃伅鏀惧叆鍒拌繖涓被涓€?/li>
- 閭d箞鍦ㄥ疄鐜扮殑鏃跺€欏氨璁捐濂芥帴鍙g殑瀹炵幇灞傜骇鍏崇郴锛屽寘鎷垜浠渶瑕佸畾涔夊嚭 Bean 瀹氫箟鐨勮鍙栨帴鍙?
BeanDefinitionReader
浠ュ強鍋氬ソ瀵瑰簲鐨勫疄鐜扮被锛屽湪瀹炵幇绫讳腑瀹屾垚瀵?Bean 瀵硅薄鐨勮В鏋愬拰娉ㄥ唽銆?/li>6. 璁捐涓庡疄鐜拌祫婧愬姞杞藉櫒锛屼粠Spring.xml瑙f瀽鍜屾敞鍐孊ean瀵硅薄
涓轰簡鑳芥弧瓒充簬鍦?Bean 瀵硅薄浠庢敞鍐屽埌瀹炰緥鍖栫殑杩囩▼涓墽琛岀敤鎴风殑鑷畾涔夋搷浣滐紝灏遍渶瑕佸湪 Bean 鐨勫畾涔夊拰鍒濆鍖栬繃绋嬩腑鎻掑叆鎺ュ彛绫伙紝杩欎釜鎺ュ彛鍐嶆湁澶栭儴鍘诲疄鐜拌嚜宸遍渶瑕佺殑鏈嶅姟銆傞偅涔堝湪缁撳悎瀵?Spring 妗嗘灦涓婁笅鏂囩殑澶勭悊鑳藉姏锛屽氨鍙互婊¤冻鎴戜滑鐨勭洰鏍囬渶姹備簡銆傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 婊¤冻浜庡 Bean 瀵硅薄鎵╁睍鐨勪袱涓帴鍙o紝鍏跺疄涔熸槸 Spring 妗嗘灦涓潪甯稿叿鏈夐噸閲忕骇鐨勪袱涓帴鍙o細
BeanFactoryPostProcess
鍜?BeanPostProcessor
锛屼篃鍑犱箮鏄ぇ瀹跺湪浣跨敤 Spring 妗嗘灦棰濆鏂板寮€鍙戣嚜宸辩粍寤洪渶姹傜殑涓や釜蹇呭鎺ュ彛銆?/li>- BeanFactoryPostProcessor锛屾槸鐢?Spring 妗嗘灦缁勫缓鎻愪緵鐨勫鍣ㄦ墿灞曟満鍒讹紝鍏佽鍦?Bean 瀵硅薄娉ㄥ唽鍚庝絾鏈疄渚嬪寲涔嬪墠锛屽 Bean 鐨勫畾涔変俊鎭?
BeanDefinition
鎵ц淇敼鎿嶄綔銆?/li>- BeanPostProcessor锛屼篃鏄?Spring 鎻愪緵鐨勬墿灞曟満鍒讹紝涓嶈繃 BeanPostProcessor 鏄湪 Bean 瀵硅薄瀹炰緥鍖栦箣鍚庝慨鏀?Bean 瀵硅薄锛屼篃鍙互鏇挎崲 Bean 瀵硅薄銆傝繖閮ㄥ垎涓庡悗闈㈣瀹炵幇鐨?AOP 鏈夌潃瀵嗗垏鐨勫叧绯汇€?/li>
- 鍚屾椂濡傛灉鍙槸娣诲姞杩欎袱涓帴鍙o紝涓嶅仛浠讳綍鍖呰锛岄偅涔堝浜庝娇鐢ㄨ€呮潵璇磋繕鏄潪甯搁夯鐑︾殑銆傛垜浠笇鏈涗簬寮€鍙?Spring 鐨勪笂涓嬫枃鎿嶄綔绫伙紝鎶婄浉搴旂殑 XML 鍔犺浇 銆佹敞鍐屻€佸疄渚嬪寲浠ュ強鏂板鐨勪慨鏀瑰拰鎵╁睍閮借瀺鍚堣繘鍘伙紝璁?Spring 鍙互鑷姩鎵弿鍒版垜浠殑鏂板鏈嶅姟锛屼究浜庣敤鎴蜂娇鐢ㄣ€?/li>
7. 瀹炵幇搴旂敤涓婁笅鏂囷紝鑷姩璇嗗埆銆佽祫婧愬姞杞姐€佹墿灞曟満鍒?/h3>
鍙兘闈㈠鍍?Spring 杩欐牱搴炲ぇ鐨勬鏋讹紝瀵瑰鏆撮湶鐨勬帴鍙e畾涔変娇鐢ㄦ垨鑰厁ml閰嶇疆锛屽畬鎴愮殑涓€绯诲垪鎵╁睍鎬ф搷浣滐紝閮借 Spring 妗嗘灦鐪嬩笂鍘诲緢绁炵銆傚叾瀹炲浜庤繖鏍峰湪 Bean 瀹瑰櫒鍒濆鍖栬繃绋嬩腑棰濆娣诲姞鐨勫鐞嗘搷浣滐紝鏃犻潪灏辨槸棰勫厛鎵ц浜嗕竴涓畾涔夊ソ鐨勬帴鍙f柟娉曟垨鑰呮槸鍙嶅皠璋冪敤绫讳腑xml涓厤缃殑鏂规硶锛屾渶缁堜綘鍙鎸夌収鎺ュ彛瀹氫箟瀹炵幇锛屽氨浼氭湁 Spring 瀹瑰櫒鍦ㄥ鐞嗙殑杩囩▼涓繘琛岃皟鐢ㄨ€屽凡銆傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 鍦?spring.xml 閰嶇疆涓坊鍔?
init-method銆乨estroy-method
涓や釜娉ㄨВ锛屽湪閰嶇疆鏂囦欢鍔犺浇鐨勮繃绋嬩腑锛屾妸娉ㄨВ閰嶇疆涓€骞跺畾涔夊埌 BeanDefinition 鐨勫睘鎬у綋涓€傝繖鏍峰湪 initializeBean 鍒濆鍖栨搷浣滅殑宸ョ▼涓紝灏卞彲浠ラ€氳繃鍙嶅皠鐨勬柟寮忔潵璋冪敤閰嶇疆鍦?Bean 瀹氫箟灞炴€у綋涓殑鏂规硶淇℃伅浜嗐€傚彟澶栧鏋滄槸鎺ュ彛瀹炵幇鐨勬柟寮忥紝閭d箞鐩存帴鍙互閫氳繃 Bean 瀵硅薄璋冪敤瀵瑰簲鎺ュ彛瀹氫箟鐨勬柟娉曞嵆鍙紝((InitializingBean) bean).afterPropertiesSet()
锛屼袱绉嶆柟寮忚揪鍒扮殑鏁堟灉鏄竴鏍风殑銆?/li>- 闄や簡鍦ㄥ垵濮嬪寲鍋氱殑鎿嶄綔澶栵紝
destroy-method
鍜?DisposableBean
鎺ュ彛鐨勫畾涔夛紝閮戒細鍦?Bean 瀵硅薄鍒濆鍖栧畬鎴愰樁娈碉紝鎵ц娉ㄥ唽閿€姣佹柟娉曠殑淇℃伅鍒?DefaultSingletonBeanRegistry 绫讳腑鐨?disposableBeans 灞炴€ч噷锛岃繖鏄负浜嗗悗缁粺涓€杩涜鎿嶄綔銆?em>杩欓噷杩樻湁涓€娈甸€傞厤鍣ㄧ殑浣跨敤锛屽洜涓哄弽灏勮皟鐢ㄥ拰鎺ュ彛鐩存帴璋冪敤锛屾槸涓ょ鏂瑰紡銆傛墍浠ラ渶瑕佷娇鐢ㄩ€傞厤鍣ㄨ繘琛屽寘瑁咃紝涓嬫枃浠g爜璁茶В涓弬鑰?DisposableBeanAdapter 鐨勫叿浣撳疄鐜?/em>
-鍏充簬閿€姣佹柟娉曢渶瑕佸湪铏氭嫙鏈烘墽琛屽叧闂箣鍓嶈繘琛屾搷浣滐紝鎵€浠ヨ繖閲岄渶瑕佺敤鍒颁竴涓敞鍐岄挬瀛愮殑鎿嶄綔锛屽锛?code>Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("close锛?))); 杩欐浠g爜浣犲彲浠ユ墽琛屾祴璇?/em>锛屽彟澶栦綘鍙互浣跨敤鎵嬪姩璋冪敤 ApplicationContext.close 鏂规硶鍏抽棴瀹瑰櫒銆?/li>8. 鍚戣櫄鎷熸満娉ㄥ唽閽╁瓙锛屽疄鐜癇ean瀵硅薄鐨勫垵濮嬪寲鍜岄攢姣佹柟娉?/h3>
鍙兘闈㈠鍍?Spring 杩欐牱搴炲ぇ鐨勬鏋讹紝瀵瑰鏆撮湶鐨勬帴鍙e畾涔変娇鐢ㄦ垨鑰厁ml閰嶇疆锛屽畬鎴愮殑涓€绯诲垪鎵╁睍鎬ф搷浣滐紝閮借 Spring 妗嗘灦鐪嬩笂鍘诲緢绁炵銆傚叾瀹炲浜庤繖鏍峰湪 Bean 瀹瑰櫒鍒濆鍖栬繃绋嬩腑棰濆娣诲姞鐨勫鐞嗘搷浣滐紝鏃犻潪灏辨槸棰勫厛鎵ц浜嗕竴涓畾涔夊ソ鐨勬帴鍙f柟娉曟垨鑰呮槸鍙嶅皠璋冪敤绫讳腑xml涓厤缃殑鏂规硶锛屾渶缁堜綘鍙鎸夌収鎺ュ彛瀹氫箟瀹炵幇锛屽氨浼氭湁 Spring 瀹瑰櫒鍦ㄥ鐞嗙殑杩囩▼涓繘琛岃皟鐢ㄨ€屽凡銆傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 鍦?spring.xml 閰嶇疆涓坊鍔?
init-method銆乨estroy-method
涓や釜娉ㄨВ锛屽湪閰嶇疆鏂囦欢鍔犺浇鐨勮繃绋嬩腑锛屾妸娉ㄨВ閰嶇疆涓€骞跺畾涔夊埌 BeanDefinition 鐨勫睘鎬у綋涓€傝繖鏍峰湪 initializeBean 鍒濆鍖栨搷浣滅殑宸ョ▼涓紝灏卞彲浠ラ€氳繃鍙嶅皠鐨勬柟寮忔潵璋冪敤閰嶇疆鍦?Bean 瀹氫箟灞炴€у綋涓殑鏂规硶淇℃伅浜嗐€傚彟澶栧鏋滄槸鎺ュ彛瀹炵幇鐨勬柟寮忥紝閭d箞鐩存帴鍙互閫氳繃 Bean 瀵硅薄璋冪敤瀵瑰簲鎺ュ彛瀹氫箟鐨勬柟娉曞嵆鍙紝((InitializingBean) bean).afterPropertiesSet()
锛屼袱绉嶆柟寮忚揪鍒扮殑鏁堟灉鏄竴鏍风殑銆?/li>- 闄や簡鍦ㄥ垵濮嬪寲鍋氱殑鎿嶄綔澶栵紝
destroy-method
鍜?DisposableBean
鎺ュ彛鐨勫畾涔夛紝閮戒細鍦?Bean 瀵硅薄鍒濆鍖栧畬鎴愰樁娈碉紝鎵ц娉ㄥ唽閿€姣佹柟娉曠殑淇℃伅鍒?DefaultSingletonBeanRegistry 绫讳腑鐨?disposableBeans 灞炴€ч噷锛岃繖鏄负浜嗗悗缁粺涓€杩涜鎿嶄綔銆?em>杩欓噷杩樻湁涓€娈甸€傞厤鍣ㄧ殑浣跨敤锛屽洜涓哄弽灏勮皟鐢ㄥ拰鎺ュ彛鐩存帴璋冪敤锛屾槸涓ょ鏂瑰紡銆傛墍浠ラ渶瑕佷娇鐢ㄩ€傞厤鍣ㄨ繘琛屽寘瑁咃紝涓嬫枃浠g爜璁茶В涓弬鑰?DisposableBeanAdapter 鐨勫叿浣撳疄鐜?/em>
-鍏充簬閿€姣佹柟娉曢渶瑕佸湪铏氭嫙鏈烘墽琛屽叧闂箣鍓嶈繘琛屾搷浣滐紝鎵€浠ヨ繖閲岄渶瑕佺敤鍒颁竴涓敞鍐岄挬瀛愮殑鎿嶄綔锛屽锛?code>Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("close锛?))); 杩欐浠g爜浣犲彲浠ユ墽琛屾祴璇?/em>锛屽彟澶栦綘鍙互浣跨敤鎵嬪姩璋冪敤 ApplicationContext.close 鏂规硶鍏抽棴瀹瑰櫒銆?/li>9. 瀹氫箟鏍囪绫诲瀷Aware鎺ュ彛锛屽疄鐜版劅鐭ュ鍣ㄥ璞?/h3>
濡傛灉璇存垜甯屾湜鎷垮埌 Spring 妗嗘灦涓竴浜涙彁渚涚殑璧勬簮锛岄偅涔堥鍏堥渶瑕佽€冭檻浠ヤ竴涓粈涔堟柟寮忓幓鑾峰彇锛屼箣鍚庝綘瀹氫箟鍑烘潵鐨勮幏鍙栨柟寮忥紝鍦?Spring 妗嗘灦涓鎬庝箞鍘绘壙鎺ワ紝瀹炵幇浜嗚繖涓ら」鍐呭锛屽氨鍙互鎵╁睍鍑轰綘闇€瑕佺殑涓€浜涘睘浜?Spring 妗嗘灦鏈韩鐨勮兘鍔涗簡銆?/p>
鍦ㄥ叧浜?Bean 瀵硅薄瀹炰緥鍖栭樁娈垫垜浠搷浣滆繃涓€浜涢澶栧畾涔夈€佸睘鎬с€佸垵濮嬪寲鍜岄攢姣佺殑鎿嶄綔锛屽叾瀹炴垜浠鏋滃儚鑾峰彇 Spring 涓€浜涘 BeanFactory銆丄pplicationContext 鏃讹紝涔熷彲浠ラ€氳繃姝ょ被鏂瑰紡杩涜瀹炵幇銆傞偅涔堟垜浠渶瑕佸畾涔変竴涓爣璁版€х殑鎺ュ彛锛岃繖涓帴鍙d笉闇€瑕佹湁鏂规硶锛屽畠鍙捣鍒版爣璁颁綔鐢ㄥ氨鍙互锛岃€屽叿浣撶殑鍔熻兘鐢辩户鎵挎鎺ュ彛鐨勫叾浠栧姛鑳芥€ф帴鍙e畾涔夊叿浣撴柟娉曪紝鏈€缁堣繖涓帴鍙e氨鍙互閫氳繃
instanceof
杩涜鍒ゆ柇鍜岃皟鐢ㄤ簡銆傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 瀹氫箟鎺ュ彛 Aware锛屽湪 Spring 妗嗘灦涓畠鏄竴绉嶆劅鐭ユ爣璁版€ф帴鍙o紝鍏蜂綋鐨勫瓙绫诲畾涔夊拰瀹炵幇鑳芥劅鐭ュ鍣ㄤ腑鐨勭浉鍏冲璞°€?em>涔熷氨鏄€氳繃杩欎釜妗ユ锛屽悜鍏蜂綋鐨勫疄鐜扮被涓彁渚涘鍣ㄦ湇鍔?/em>
- 缁ф壙 Aware 鐨勬帴鍙e寘鎷細BeanFactoryAware銆丅eanClassLoaderAware銆丅eanNameAware鍜孉pplicationContextAware锛屽綋鐒跺湪 Spring 婧愮爜涓繕鏈変竴浜涘叾浠栧叧浜庢敞瑙g殑锛屼笉杩囩洰鍓嶆垜浠繕鏄敤涓嶅埌銆?/li>
- 鍦ㄥ叿浣撶殑鎺ュ彛瀹炵幇杩囩▼涓綘鍙互鐪嬪埌锛屼竴閮ㄥ垎(BeanFactoryAware銆丅eanClassLoaderAware銆丅eanNameAware)鍦?factory 鐨?support 鏂囦欢澶逛笅锛屽彟澶?ApplicationContextAware 鏄湪 context 鐨?support 涓紝杩欐槸鍥犱负涓嶅悓鐨勫唴瀹硅幏鍙栭渶瑕佸湪涓嶅悓鐨勫寘涓嬫彁渚涖€傛墍浠ワ紝鍦?AbstractApplicationContext 鐨勫叿浣撳疄鐜颁腑浼氱敤鍒板悜 beanFactory 娣诲姞 BeanPostProcessor 鍐呭鐨?
ApplicationContextAwareProcessor
鎿嶄綔锛屾渶鍚庣敱 AbstractAutowireCapableBeanFactory 鍒涘缓 createBean 鏃跺鐞嗙浉搴旂殑璋冪敤鎿嶄綔銆?em>鍏充簬 applyBeanPostProcessorsBeforeInitialization 宸茬粡鍦ㄥ墠闈㈢珷鑺備腑瀹炵幇杩囷紝濡傛灉蹇樿鍙互寰€鍓嶇炕缈?/em>10. 鍏充簬Bean瀵硅薄浣滅敤鍩熶互鍙奆actoryBean鐨勫疄鐜板拰浣跨敤
鍏充簬鎻愪緵涓€涓兘璁╀娇鐢ㄨ€呭畾涔夊鏉傜殑 Bean 瀵硅薄锛屽姛鑳界偣闈炲父涓嶉敊锛屾剰涔変篃闈炲父澶э紝鍥犱负杩欐牱鍋氫簡涔嬪悗 Spring 鐨勭敓鎬佺瀛愬鍖栫灏辨鎻愪緵浜嗭紝璋佸鐨勬鏋堕兘鍙互鍦ㄦ鏍囧噯涓婂畬鎴愯嚜宸辨湇鍔$殑鎺ュ叆銆?/p>
浣嗚繖鏍风殑鍔熻兘閫昏緫璁捐涓婂苟涓嶅鏉傦紝鍥犱负鏁翠釜 Spring 妗嗘灦鍦ㄥ紑鍙戠殑杩囩▼涓氨宸茬粡鎻愪緵浜嗗悇椤规墿灞曡兘鍔涚殑
鎺ヨ尙
锛屼綘鍙渶瑕佸湪鍚堥€傜殑浣嶇疆鎻愪緵涓€涓帴鑼殑澶勭悊鎺ュ彛璋冪敤鍜岀浉搴旂殑鍔熻兘閫昏緫瀹炵幇鍗冲彲锛屽儚杩欓噷鐨勭洰鏍囧疄鐜板氨鏄澶栨彁渚涗竴涓彲浠ヤ簩娆′粠 FactoryBean 鐨?getObject 鏂规硶涓幏鍙栧璞$殑鍔熻兘鍗冲彲锛岃繖鏍锋墍鏈夊疄鐜版鎺ュ彛鐨勫璞$被锛屽氨鍙互鎵╁厖鑷繁鐨勫璞″姛鑳戒簡銆?em>MyBatis 灏辨槸瀹炵幇浜嗕竴涓?MapperFactoryBean 绫伙紝鍦?getObject 鏂规硶涓彁渚?SqlSession 瀵规墽琛?CRUD 鏂规硶鐨勬搷浣?/em> 鏁翠綋璁捐缁撴瀯濡備笅鍥撅細
- 鏁翠釜鐨勫疄鐜拌繃绋嬪寘鎷簡涓ら儴鍒嗭紝涓€涓В鍐冲崟渚嬭繕鏄師鍨嬪璞★紝鍙﹀涓€涓鐞?FactoryBean 绫诲瀷瀵硅薄鍒涘缓杩囩▼涓叧浜庤幏鍙栧叿浣撹皟鐢ㄥ璞$殑
getObject
鎿嶄綔銆?/li>SCOPE_SINGLETON
銆?code>SCOPE_PROTOTYPE锛屽璞$被鍨嬬殑鍒涘缓鑾峰彇鏂瑰紡锛屼富瑕佸尯鍒嗗湪浜?AbstractAutowireCapableBeanFactory#createBean
鍒涘缓瀹屾垚瀵硅薄鍚庢槸鍚︽斁鍏ュ埌鍐呭瓨涓紝濡傛灉涓嶆斁鍏ュ垯姣忔鑾峰彇閮戒細閲嶆柊鍒涘缓銆?/li>- createBean 鎵ц瀵硅薄鍒涘缓銆佸睘鎬у~鍏呫€佷緷璧栧姞杞姐€佸墠缃悗缃鐞嗐€佸垵濮嬪寲绛夋搷浣滃悗锛屽氨瑕佸紑濮嬪仛鎵ц鍒ゆ柇鏁翠釜瀵硅薄鏄惁鏄竴涓?FactoryBean 瀵硅薄锛屽鏋滄槸杩欐牱鐨勫璞★紝灏遍渶瑕佸啀缁х画鎵ц鑾峰彇 FactoryBean 鍏蜂綋瀵硅薄涓殑
getObject
瀵硅薄浜嗐€傛暣涓?getBean 杩囩▼涓兘浼氭柊澧炰竴涓崟渚嬬被鍨嬬殑鍒ゆ柇factory.isSingleton()
锛岀敤浜庡喅瀹氭槸鍚︿娇鐢ㄥ唴瀛樺瓨鏀惧璞′俊鎭€?/li>11. 鍩轰簬瑙傚療鑰呭疄鐜帮紝瀹瑰櫒浜嬩欢鍜屼簨浠剁洃鍚櫒
鍏跺疄浜嬩欢鐨勮璁℃湰韬氨鏄竴绉嶈瀵熻€呮ā寮忕殑瀹炵幇锛屽畠鎵€瑕佽В鍐崇殑灏辨槸涓€涓璞$姸鎬佹敼鍙樼粰鍏朵粬瀵硅薄閫氱煡鐨勯棶棰橈紝鑰屼笖瑕佽€冭檻鍒版槗鐢ㄥ拰浣庤€﹀悎锛屼繚璇侀珮搴︾殑鍗忎綔銆?/p>
鍦ㄥ姛鑳藉疄鐜颁笂鎴戜滑闇€瑕佸畾涔夊嚭浜嬩欢绫汇€佷簨浠剁洃鍚€佷簨浠跺彂甯冿紝鑰岃繖浜涚被鐨勫姛鑳介渶瑕佺粨鍚堝埌 Spring 鐨?AbstractApplicationContext#refresh()锛屼互渚夸簬澶勭悊浜嬩欢鍒濆鍖栧拰娉ㄥ唽浜嬩欢鐩戝惉鍣ㄧ殑鎿嶄綔銆傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 鍦ㄦ暣涓姛鑳藉疄鐜拌繃绋嬩腑锛屼粛鐒堕渶瑕佸湪闈㈠悜鐢ㄦ埛鐨勫簲鐢ㄤ笂涓嬫枃
AbstractApplicationContext
涓坊鍔犵浉鍏充簨浠跺唴瀹癸紝鍖呮嫭锛氬垵濮嬪寲浜嬩欢鍙戝竷鑰呫€佹敞鍐屼簨浠剁洃鍚櫒銆佸彂甯冨鍣ㄥ埛鏂板畬鎴愪簨浠躲€?/li>- 浣跨敤瑙傚療鑰呮ā寮忓畾涔変簨浠剁被銆佺洃鍚被銆佸彂甯冪被锛屽悓鏃惰繕闇€瑕佸畬鎴愪竴涓箍鎾櫒鐨勫姛鑳斤紝鎺ユ敹鍒颁簨浠舵帹閫佹椂杩涜鍒嗘瀽澶勭悊绗﹀悎鐩戝惉浜嬩欢鎺ュ彈鑰呮劅鍏磋叮鐨勪簨浠讹紝涔熷氨鏄娇鐢?isAssignableFrom 杩涜鍒ゆ柇銆?/li>
- isAssignableFrom 鍜?instanceof 鐩镐技锛屼笉杩?isAssignableFrom 鏄敤鏉ュ垽鏂瓙绫诲拰鐖剁被鐨勫叧绯荤殑锛屾垨鑰呮帴鍙g殑瀹炵幇绫诲拰鎺ュ彛鐨勫叧绯荤殑锛岄粯璁ゆ墍鏈夌殑绫荤殑缁堟瀬鐖剁被閮芥槸Object銆傚鏋淎.isAssignableFrom(B)缁撴灉鏄痶rue锛岃瘉鏄嶣鍙互杞崲鎴愪负A,涔熷氨鏄疉鍙互鐢盉杞崲鑰屾潵銆?/li>
12. 鍩轰簬JDK鍜孋glib鍔ㄦ€佷唬鐞嗭紝瀹炵幇AOP鏍稿績鍔熻兘
鍦ㄦ妸 AOP 鏁翠釜鍒囬潰璁捐铻嶅悎鍒?Spring 鍓嶏紝鎴戜滑闇€瑕佽В鍐充袱涓棶棰橈紝鍖呮嫭锛?code>濡備綍缁欑鍚堣鍒欑殑鏂规硶鍋氫唬鐞?/code>锛?code>浠ュ強鍋氬畬浠g悊鏂规硶鐨勬渚嬪悗锛屾妸绫荤殑鑱岃矗鎷嗗垎鍑烘潵銆傝€岃繖涓や釜鍔熻兘鐐圭殑瀹炵幇锛岄兘鏄互鍒囬潰鐨勬€濇兂杩涜璁捐鍜屽紑鍙戙€傚鏋滀笉鏄緢娓呮 AOP 鏄暐锛屼綘鍙互鎶婂垏闈㈢悊瑙d负鐢ㄥ垁鍒囬煭鑿滐紝涓€鏍逛竴鏍瑰垏鎬绘槸鏈夌偣鎱紝閭d箞鐢ㄦ墜(
浠g悊
)鎶婇煭鑿滄崗鎴愪竴鎶婏紝鐢ㄨ彍鍒€鎴栬€呮枾澶磋繖鏍蜂笉鍚岀殑鎷︽埅鎿嶄綔鏉ュ鐞嗐€傝€岀▼搴忎腑鍏跺疄涔熸槸涓€鏍凤紝鍙笉杩囬煭鑿滃彉鎴愪簡鏂规硶锛岃彍鍒€鍙樻垚浜嗘嫤鎴柟娉曘€傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 灏卞儚浣犲湪浣跨敤 Spring 鐨?AOP 涓€鏍凤紝鍙鐞嗕竴浜涢渶瑕佽鎷︽埅鐨勬柟娉曘€傚湪鎷︽埅鏂规硶鍚庯紝鎵ц浣犲鏂规硶鐨勬墿灞曟搷浣溿€?/li>
- 閭d箞鎴戜滑灏遍渶瑕佸厛鏉ュ疄鐜颁竴涓彲浠ヤ唬鐞嗘柟娉曠殑 Proxy锛屽叾瀹炰唬鐞嗘柟娉曚富瑕佹槸浣跨敤鍒版柟娉曟嫤鎴櫒绫诲鐞嗘柟娉曠殑璋冪敤
MethodInterceptor#invoke
锛岃€屼笉鏄洿鎺ヤ娇鐢?invoke 鏂规硶涓殑鍏ュ弬 Method method 杩涜method.invoke(targetObj, args)
杩欏潡鏄暣涓娇鐢ㄦ椂鐨勫樊寮傘€?/li>- 闄や簡浠ヤ笂鐨勬牳蹇冨姛鑳藉疄鐜帮紝杩橀渶瑕佷娇鐢ㄥ埌
org.aspectj.weaver.tools.PointcutParser
澶勭悊鎷︽埅琛ㄨ揪寮?"execution(* cn.bugstack.springframework.test.bean.IUserService.*(..))"
锛屾湁浜嗘柟娉曚唬鐞嗗拰澶勭悊鎷︽埅锛屾垜浠氨鍙互瀹屾垚璁捐鍑轰竴涓?AOP 鐨勯洀褰簡銆?/li>13. 鎶夾OP鍔ㄦ€佷唬鐞嗭紝铻嶅叆鍒癇ean鐨勭敓鍛藉懆鏈?/h3>
鍏跺疄鍦ㄦ湁浜咥OP鐨勬牳蹇冨姛鑳藉疄鐜板悗锛屾妸杩欓儴鍒嗗姛鑳芥湇鍔¤瀺鍏ュ埌 Spring 鍏跺疄涔熶笉闅撅紝鍙笉杩囪瑙e喅鍑犱釜闂锛屽寘鎷細鎬庝箞鍊熺潃 BeanPostProcessor 鎶婂姩鎬佷唬鐞嗚瀺鍏ュ埌 Bean 鐨勭敓鍛藉懆鏈熶腑锛屼互鍙婂浣曠粍瑁呭悇椤瑰垏鐐广€佹嫤鎴€佸墠缃殑鍔熻兘鍜岄€傞厤瀵瑰簲鐨勪唬鐞嗗櫒銆傛暣浣撹璁$粨鏋勫涓嬪浘锛?/p>
- 涓轰簡鍙互璁╁璞″垱寤鸿繃绋嬩腑锛岃兘鎶妜ml涓厤缃殑浠g悊瀵硅薄涔熷氨鏄垏闈㈢殑涓€浜涚被瀵硅薄瀹炰緥鍖栵紝灏遍渶瑕佺敤鍒?BeanPostProcessor 鎻愪緵鐨勬柟娉曪紝鍥犱负杩欎釜绫荤殑涓殑鏂规硶鍙互鍒嗗埆浣滅敤涓?Bean 瀵硅薄鎵ц鍒濆鍖栧墠鍚庝慨鏀?Bean 鐨勫璞$殑鎵╁睍淇℃伅銆備絾杩欓噷闇€瑕侀泦鍚堜簬 BeanPostProcessor 瀹炵幇鏂扮殑鎺ュ彛鍜屽疄鐜扮被锛岃繖鏍锋墠鑳藉畾鍚戣幏鍙栧搴旂殑绫讳俊鎭€?/li>
- 浣嗗洜涓哄垱寤虹殑鏄唬鐞嗗璞′笉鏄箣鍓嶆祦绋嬮噷鐨勬櫘閫氬璞★紝鎵€浠ユ垜浠渶瑕佸墠缃簬鍏朵粬瀵硅薄鐨勫垱寤猴紝鎵€浠ュ湪瀹為檯寮€鍙戠殑杩囩▼涓紝闇€瑕佸湪 AbstractAutowireCapableBeanFactory#createBean 浼樺厛瀹屾垚 Bean 瀵硅薄鐨勫垽鏂紝鏄惁闇€瑕佷唬鐞嗭紝鏈夊垯鐩存帴杩斿洖浠g悊瀵硅薄銆?em>鍦⊿pring鐨勬簮鐮佷腑浼氭湁 createBean 鍜?doCreateBean 鐨勬柟娉曟媶鍒?/em>
- 杩欓噷杩樺寘鎷瑙e喅鏂规硶鎷︽埅鍣ㄧ殑鍏蜂綋鍔熻兘锛屾彁渚涗竴浜?BeforeAdvice銆丄fterAdvice 鐨勫疄鐜帮紝璁╃敤鎴峰彲浠ユ洿绠€鍖栫殑浣跨敤鍒囬潰鍔熻兘銆傞櫎姝や箣澶栬繕鍖呮嫭闇€瑕佸寘瑁呭垏闈㈣〃杈惧紡浠ュ強鎷︽埅鏂规硶鐨勬暣鍚堬紝浠ュ強鎻愪緵涓嶅悓绫诲瀷鐨勪唬鐞嗘柟寮忕殑浠g悊宸ュ巶锛屾潵鍖呰鎴戜滑鐨勫垏闈㈡湇鍔°€?/li>
涓夈€?瀛︿範璇存槑
鏈唬鐮佷粨搴?https://github.com/fuzhengwei/small-spring 浠?Spring 婧愮爜瀛︿範涓虹洰鐨勶紝閫氳繃鎵嬪啓绠€鍖栫増 Spring 妗嗘灦锛屼簡瑙?Spring 鏍稿績鍘熺悊銆?/p>
鍦ㄦ墜鍐欑殑杩囩▼涓細绠€鍖?Spring 婧愮爜锛屾憳鍙栨暣浣撴鏋朵腑鐨勬牳蹇冮€昏緫锛岀畝鍖栦唬鐮佸疄鐜拌繃绋嬶紝淇濈暀鏍稿績鍔熻兘锛屼緥濡傦細IOC銆丄OP銆丅ean鐢熷懡鍛ㄦ湡銆佷笂涓嬫枃銆佷綔鐢ㄥ煙銆佽祫婧愬鐞嗙瓑鍐呭瀹炵幇銆?/p>
- 姝や笓鏍忎负瀹炴垬缂栫爜绫昏祫鏂欙紝鍦ㄥ涔犵殑杩囩▼涓渶瑕佺粨鍚堟枃涓瘡涓珷鑺傞噷锛岃瑙e喅鐨?strong>鐩爣锛岃繘琛岀殑鎬濊矾璁捐锛屽甫鍏ュ埌缂栫爜瀹炴搷杩囩▼銆傚湪瀛︿範缂栫爜鐨勫悓鏃朵篃鏈€濂界悊瑙e叧浜庤繖閮ㄥ垎鍐呭涓轰粈涔堣繖鏍风殑瀹炵幇锛屽畠鐢ㄥ埌浜嗗摢鏍风殑璁捐妯″紡锛岄噰鐢ㄤ簡浠€涔堟墜娈靛仛浜嗕粈涔堟牱鐨勮亴璐e垎绂汇€傚彧鏈夐€氳繃杩欐牱鐨勫涔犳墠鑳芥洿濂界殑鐞嗚В鍜屾帉鎻?Spring 婧愮爜鐨勫疄鐜拌繃绋嬶紝涔熻兘甯姪浣犲湪浠ュ悗鐨勬繁鍏ュ涔犲拰瀹炶返搴旂敤鐨勮繃绋嬩腑鎵撲笅涓€涓墡瀹炵殑鍩虹銆?/li>
- 鍙﹀姝や笓鏍忓唴瀹圭殑瀛︿範涓婄粨鍚堜簡璁捐妯″紡锛屼笅瀵瑰簲浜?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fjuejin.cn%2Fbook%2F6940996508632219689" rel="nofollow">SpringBoot 涓棿浠惰璁″拰寮€鍙?/a>锛屾墍浠ヨ鑰呭湪瀛︿範鐨勮繃绋嬩腑濡傛灉閬囧埌涓嶇悊瑙g殑璁捐妯″紡鍙互缈婚槄鐩稿簲鐨勮祫鏂欙紝鍦ㄥ涔犲畬 Spring 鍚庤繕鍙互缁撳悎涓棿浠剁殑鍐呭杩涜缁冧範銆?/li>
- 婧愮爜锛氭涓撴爮娑夊強鍒扮殑婧愮爜宸茬粡鍏ㄩ儴鏁村悎鍒板綋鍓嶅伐绋嬩笅锛屽彲浠ヤ笌绔犺妭涓搴旂殑妗堜緥婧愮爜涓€涓€鍖归厤涓娿€傚ぇ瀹舵嬁鍒版暣濂楀伐绋嬪彲浠ョ洿鎺ヨ繍琛岋紝涔熷彲浠ユ妸姣忎釜绔犺妭瀵瑰簲鐨勬簮鐮佸伐绋嬪崟鐙墦寮€杩愯銆?/li>
- 濡傛灉浣犲湪瀛︿範鐨勮繃绋嬩腑閬囧埌浠€涔堥棶棰橈紝鍖呮嫭锛氫笉鑳借繍琛屻€佷紭鍖栨剰瑙併€佹枃瀛楅敊璇瓑浠讳綍闂閮藉彲浠ユ彁浜ssue
- 鍦ㄤ笓鏍忕殑鍐呭缂栧啓涓紝姣忎竴涓珷鑺傞兘鎻愪緵浜嗘竻鏅扮殑璁捐鍥剧鍜屽搴旂殑绫诲浘锛屾墍浠ュ涔犺繃绋嬩腑涓€瀹氫笉瑕佸彧鏄湪涔庝唬鐮佹槸鎬庝箞缂栧啓鐨勶紝鏇撮噸瑕佺殑鏄悊瑙h繖浜涜璁$殑鍐呭鏄浣曟潵鐨勩€?/li>
馃榿 濂藉槥锛屽笇鏈涗綘鍙互瀛︾殑鎰夊揩锛?/p>
以上是关于CRUD鎼爾涓や笁骞翠簡锛屾€庝箞闃呰Spring婧愮爜锛?/a>的主要内容,如果未能解决你的问题,请参考以下文章
棰樺璇濓細2020 骞磋鎬庝箞鐢?Intellij IDEA 鎼缓 Haskell 鐜