鎵嬫捀Spring妗嗘灦锛岃璁′笌瀹炵幇璧勬簮鍔犺浇鍣紝浠嶴pring.xml瑙f瀽鍜屾敞鍐孊ean瀵硅薄
Posted 灏忓倕鍝?,
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎵嬫捀Spring妗嗘灦锛岃璁′笌瀹炵幇璧勬簮鍔犺浇鍣紝浠嶴pring.xml瑙f瀽鍜屾敞鍐孊ean瀵硅薄相关的知识,希望对你有一定的参考价值。
浣滆€咃細灏忓倕鍝?br><br/>鍗氬锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fbugstack.cn" rel="nofollow">https://bugstack.cn
娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/blockquote>涓€銆佸墠瑷€
浣犲啓鐨勪唬鐮侊紝鑳芥帴鐨勪綇浜у搧鍔犻渶姹傚悧锛?/code>
鎺ワ紝鏄兘鎺ョ殑锛屾帴鍑犳涔熻锛屽摢鎬曞氨涓€涓被涓€鐗囩殑 if...else 涔熷彲浠ワ紒浣嗘帴瀹屾垚浠€涔堟牱鍙氨涓嶄竴瀹氫簡锛屼細涓嶄細鍑轰簨鏁呬篃涓嶆槸鑳芥帶鍒朵綇鐨勩€?/p>
閭e嚭浜嬫晠鏃讹紝浣犺鍥犱负鎴戝啓 if...else 澶氫簡瀵艰嚧浠g爜鐑備簡锛屼絾鍙槸浣犲厛鍔ㄧ殑鎵嬪晩锛?code>浣犺鐨勯渶姹傝繕寰楀姞銆?code>浣犺鐨勮€佹澘璁╀笂绾?/code>銆?code>浣犺鐨勫悎鍚岄兘绛句簡锛屾惉鐮栫爜鍐滅殑鎴戞病鍔炴硶锛屾墠浠ュ爢浠g爜骞抽渶姹傦紝闇€姹傚お澶氫笉濂芥悶锛屾垜鎵嶄互鎼爾骞抽渶姹傦紒璇镐警涓嶆湇锛屾垜鎵嶄互鍏垫湇璇镐警锛屼綘涓嶆湇锛屾垜灏辨墦鍒颁綘鏈嶏紒
浣嗕唬鐮佺儌浜嗘湁鏃跺€欏苟涓嶆槸鍥犱负闇€姹傚姞鐨勫揩銆佷篃涓嶆槸鐫€鎬ヤ笂绾裤€傚洜涓哄線寰€鍦ㄦ壙鎺ヤ骇鍝侀渶姹傜殑鍓嶅嚑娆★紝涓€涓姛鑳介€昏緫鐨勮璁″苟涓嶄細澶鏉傦紝涔熶笉浼氭湁澶氭€ヨ揩锛岀敋鑷充細鐣欏嚭璁╀綘鍋氳璁°€佸仛璇勫銆佸仛寮€鍙戠殑鏃堕棿锛屽鏋滆繖涓椂鍊欎粛涓嶈兘鎶婁互鍚庡彲鑳戒細鍙戠敓鐨勪簨鎯呰瘎浼板埌闇€姹傞噷锛岄偅涔堝鑷翠唬鐮佺殑娣蜂贡浠庝竴寮€濮嬪氨宸茬粡鍩嬩笅浜嗭紝浠ュ悗鍙兘瓒婃潵瓒婁贡锛?/p>
鎵挎帴闇€姹傚苟鑳芥妸瀹冨仛濂斤紝杩欐潵鑷簬瀵归渶姹傜殑鐞嗚В锛屼骇鍝佸満鏅紑鍙戠殑缁忛獙浠ュ強瀵逛唬鐮佸疄璺佃惤鍦扮殑鎶婃帶鑳藉姏绛夌患鍚堝鏂归潰鍥犵礌鐨勭粨鏋溿€傚氨鍍忎綘鐜板湪鍋氱殑寮€鍙戜腑锛屼綘鐨勪唬鐮佹湁鍝簺鏄粡甯稿彉鍖栫殑锛屾湁鍝簺鏄浐瀹氶€氱敤鐨勶紝鏈夊摢浜涙槸璐熻矗閫昏緫鎷艰鐨勩€佹湁鍝簺鏄潵鍋氭牳蹇冨疄鐜扮殑銆傞偅涔堢幇鍦ㄥ鏋滀綘鐨勬牳蹇冨叡鐢ㄥ眰鍋氫簡棰戠箒鍙樺寲鐨勪笟鍔″眰鍖呰锛岄偅涔堣偗瀹氱殑璇达紝浣犵殑浠g爜鍗冲皢瓒婃潵瓒婁贡锛岀敋鑷冲彲鑳藉煁涓嬩簨鏁呯殑椋庨櫓锛?/p>
鍦ㄦ垜浠疄鐜扮殑 Spring 妗嗘灦涓紝姣忎竴涓珷鑺傞兘浼氱粨鍚堜笂涓€绔犺妭缁х画鎵╁睍鍔熻兘锛屽氨鍍忔瘡涓€娆′骇鍝侀兘鍦ㄥ姞闇€姹備竴鏍凤紝閭d箞鍦ㄥ涔犵殑杩囩▼涓彲浠ユ壙涓婂惎涓嬬殑瀵圭収鍜屽弬鑰冿紝鐪嬬湅姣忎竴涓ā鍧楃殑娣诲姞閮芥槸鐢ㄤ粈涔堥€昏緫鍜屾妧鏈粏鑺傚疄鐜扮殑銆傝繖浜涘唴瀹圭殑瀛︿範锛屼細闈炲父鏈夊埄浜庝綘浠ュ悗鍦ㄨ璁″拰瀹炵幇锛岃嚜宸辨壙鎺ヤ骇鍝侀渶姹傛椂鍋氱殑鍏蜂綋寮€鍙戯紝浠g爜鐨勮川閲忎篃浼氳秺鏉ヨ秺楂橈紝瓒婃潵瓒婃湁鎵╁睍鎬у拰鍙淮鎶ゆ€с€?/p>
浜屻€佺洰鏍?/h2>
鍦ㄥ畬鎴?Spring 鐨勬鏋堕洀褰㈠悗锛岀幇鍦ㄦ垜浠彲浠ラ€氳繃鍗曞厓娴嬭瘯杩涜鎵嬪姩鎿嶄綔 Bean 瀵硅薄鐨勫畾涔夈€佹敞鍐屽拰灞炴€у~鍏咃紝浠ュ強鏈€缁堣幏鍙栧璞¤皟鐢ㄦ柟娉曘€備絾杩欓噷浼氭湁涓€涓棶棰橈紝灏辨槸濡傛灉瀹為檯浣跨敤杩欎釜 Spring 妗嗘灦锛屾槸涓嶅お鍙兘璁╃敤鎴烽€氳繃鎵嬪姩鏂瑰紡鍒涘缓鐨勶紝鑰屾槸鏈€濂借兘閫氳繃閰嶇疆鏂囦欢鐨勬柟寮忕畝鍖栧垱寤鸿繃绋嬨€傞渶瑕佸畬鎴愬涓嬫搷浣滐細
- 濡傚浘涓垜浠渶瑕佹妸姝ラ锛?銆?銆?鏁村悎鍒癝pring妗嗘灦涓紝閫氳繃 Spring 閰嶇疆鏂囦欢鐨勬柟寮忓皢 Bean 瀵硅薄瀹炰緥鍖栥€?/li>
- 鎺ヤ笅鏉ユ垜浠氨闇€瑕佸湪鐜版湁鐨?Spring 妗嗘灦涓紝娣诲姞鑳借В鍐?Spring 閰嶇疆鐨勮鍙栥€佽В鏋愩€佹敞鍐孊ean鐨勬搷浣溿€?/li>
涓夈€佽璁?/h2>
渚濈収鏈珷鑺傜殑闇€姹傝儗鏅紝鎴戜滑闇€瑕佸湪鐜版湁鐨?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>鍥涖€佸疄鐜?/h2>
1. 宸ョ▼缁撴瀯
small-spring-step-05 鈹斺攢鈹€ src 鈹溾攢鈹€ main 鈹? 鈹斺攢鈹€ java 鈹? 鈹斺攢鈹€ cn.bugstack.springframework 鈹? 鈹溾攢鈹€ beans 鈹? 鈹? 鈹溾攢鈹€ factory 鈹? 鈹? 鈹? 鈹溾攢鈹€ factory 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ AutowireCapableBeanFactory.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ BeanDefinition.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ BeanReference.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ ConfigurableBeanFactory.java 鈹? 鈹? 鈹? 鈹? 鈹斺攢鈹€ SingletonBeanRegistry.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ support 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ AbstractAutowireCapableBeanFactory.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ AbstractBeanDefinitionReader.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ AbstractBeanFactory.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ BeanDefinitionReader.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ BeanDefinitionRegistry.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ CglibSubclassingInstantiationStrategy.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ DefaultListableBeanFactory.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ DefaultSingletonBeanRegistry.java 鈹? 鈹? 鈹? 鈹? 鈹溾攢鈹€ InstantiationStrategy.java 鈹? 鈹? 鈹? 鈹? 鈹斺攢鈹€ SimpleInstantiationStrategy.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ support 鈹? 鈹? 鈹? 鈹? 鈹斺攢鈹€ XmlBeanDefinitionReader.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ BeanFactory.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ ConfigurableListableBeanFactory.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ HierarchicalBeanFactory.java 鈹? 鈹? 鈹? 鈹斺攢鈹€ ListableBeanFactory.java 鈹? 鈹? 鈹溾攢鈹€ BeansException.java 鈹? 鈹? 鈹溾攢鈹€ PropertyValue.java 鈹? 鈹? 鈹斺攢鈹€ PropertyValues.java 鈹? 鈹溾攢鈹€ core.io 鈹? 鈹? 鈹溾攢鈹€ ClassPathResource.java 鈹? 鈹? 鈹溾攢鈹€ DefaultResourceLoader.java 鈹? 鈹? 鈹溾攢鈹€ FileSystemResource.java 鈹? 鈹? 鈹溾攢鈹€ Resource.java 鈹? 鈹? 鈹溾攢鈹€ ResourceLoader.java 鈹? 鈹? 鈹斺攢鈹€ UrlResource.java 鈹? 鈹斺攢鈹€ utils 鈹? 鈹斺攢鈹€ ClassUtils.java 鈹斺攢鈹€ test 鈹斺攢鈹€ java 鈹斺攢鈹€ cn.bugstack.springframework.test 鈹溾攢鈹€ bean 鈹? 鈹溾攢鈹€ UserDao.java 鈹? 鈹斺攢鈹€ UserService.java 鈹斺攢鈹€ ApiTest.java
宸ョ▼婧愮爜锛?code>鍏紬鍙枫€宐ugstack铏礊鏍堛€嶏紝鍥炲锛歋pring 涓撴爮锛岃幏鍙栧畬鏁存簮鐮?/code>
Spring Bean 瀹瑰櫒璧勬簮鍔犺浇鍜屼娇鐢ㄧ被鍏崇郴锛屽鍥?6-3
- 鏈珷鑺備负浜嗚兘鎶?Bean 鐨勫畾涔夈€佹敞鍐屽拰鍒濆鍖栦氦缁?Spring.xml 閰嶇疆鍖栧鐞嗭紝閭d箞灏遍渶瑕佸疄鐜颁袱澶у潡鍐呭锛屽垎鍒槸锛氳祫婧愬姞杞藉櫒銆亁ml璧勬簮澶勭悊绫伙紝瀹炵幇杩囩▼涓昏浠ュ鎺ュ彛
Resource
銆?code>ResourceLoader 鐨勫疄鐜帮紝鑰屽彟澶?BeanDefinitionReader
鎺ュ彛鍒欐槸瀵硅祫婧愮殑鍏蜂綋浣跨敤锛屽皢閰嶇疆淇℃伅娉ㄥ唽鍒?Spring 瀹瑰櫒涓幓銆?/li>- 鍦?Resource 鐨勮祫婧愬姞杞藉櫒鐨勫疄鐜颁腑鍖呮嫭浜嗭紝ClassPath銆佺郴缁熸枃浠躲€佷簯閰嶇疆鏂囦欢锛岃繖涓夐儴鍒嗕笌 Spring 婧愮爜涓殑璁捐鍜屽疄鐜颁繚鎸佷竴鑷达紝鏈€缁堝湪 DefaultResourceLoader 涓仛鍏蜂綋鐨勮皟鐢ㄣ€?/li>
- 鎺ュ彛锛欱eanDefinitionReader銆佹娊璞$被锛欰bstractBeanDefinitionReader銆佸疄鐜扮被锛歑mlBeanDefinitionReader锛岃繖涓夐儴鍒嗗唴瀹逛富瑕佹槸鍚堢悊娓呮櫚鐨勫鐞嗕簡璧勬簮璇诲彇鍚庣殑娉ㄥ唽 Bean 瀹瑰櫒鎿嶄綔銆?em>鎺ュ彛绠″畾涔夛紝鎶借薄绫诲鐞嗛潪鎺ュ彛鍔熻兘澶栫殑娉ㄥ唽Bean缁勪欢濉厖锛屾渶缁堝疄鐜扮被鍗冲彲鍙叧蹇冨叿浣撶殑涓氬姟瀹炵幇
鍙﹀鏈珷鑺傝繕鍙傝€?Spring 婧愮爜锛屽仛浜嗙浉搴旀帴鍙g殑闆嗘垚鍜屽疄鐜扮殑鍏崇郴锛岃櫧鐒惰繖浜涙帴鍙g洰鍓嶈繕骞舵病鏈夊お澶х殑浣滅敤锛屼絾闅忕潃妗嗘灦鐨勯€愭瀹屽杽锛屽畠浠篃浼氬彂鎸ヤ綔鐢ㄣ€傚鍥?6-4
- BeanFactory锛屽凡缁忓瓨鍦ㄧ殑 Bean 宸ュ巶鎺ュ彛鐢ㄤ簬鑾峰彇 Bean 瀵硅薄锛岃繖娆℃柊澧炲姞浜嗘寜鐓х被鍨嬭幏鍙?Bean 鐨勬柟娉曪細
<T> T getBean(String name, Class<T> requiredType)
- ListableBeanFactory锛屾槸涓€涓墿灞?Bean 宸ュ巶鎺ュ彛鐨勬帴鍙o紝鏂板鍔犱簡
getBeansOfType
銆?code>getBeanDefinitionNames() 鏂规硶锛屽湪 Spring 婧愮爜涓繕鏈夊叾浠栨墿灞曟柟娉曘€?/li>- HierarchicalBeanFactory锛屽湪 Spring 婧愮爜涓畠鎻愪緵浜嗗彲浠ヨ幏鍙栫埗绫?BeanFactory 鏂规硶锛屽睘浜庢槸涓€绉嶆墿灞曞伐鍘傜殑灞傛瀛愭帴鍙c€?em>Sub-interface implemented by bean factories that can be part of a hierarchy.
- AutowireCapableBeanFactory锛屾槸涓€涓嚜鍔ㄥ寲澶勭悊Bean宸ュ巶閰嶇疆鐨勬帴鍙o紝鐩墠妗堜緥宸ョ▼涓繕娌℃湁鍋氱浉搴旂殑瀹炵幇锛屽悗缁€愭瀹屽杽銆?/li>
- ConfigurableBeanFactory锛屽彲鑾峰彇 BeanPostProcessor銆丅eanClassLoader绛夌殑涓€涓厤缃寲鎺ュ彛銆?/li>
- ConfigurableListableBeanFactory锛屾彁渚涘垎鏋愬拰淇敼Bean浠ュ強棰勫厛瀹炰緥鍖栫殑鎿嶄綔鎺ュ彛锛屼笉杩囩洰鍓嶅彧鏈変竴涓?getBeanDefinition 鏂规硶銆?/li>
2. 璧勬簮鍔犺浇鎺ュ彛瀹氫箟鍜屽疄鐜?/h3>
cn.bugstack.springframework.core.io.Resource
public interface Resource { InputStream getInputStream() throws IOException; }
- 鍦?Spring 妗嗘灦涓嬪垱寤?core.io 鏍稿績鍖咃紝鍦ㄨ繖涓寘涓富瑕佺敤浜庡鐞嗚祫婧愬姞杞芥祦銆?/li>
- 瀹氫箟 Resource 鎺ュ彛锛屾彁渚涜幏鍙?InputStream 娴佺殑鏂规硶锛屾帴涓嬫潵鍐嶅垎鍒疄鐜颁笁绉嶄笉鍚岀殑娴佹枃浠舵搷浣滐細classPath銆丗ileSystem銆乁RL
ClassPath锛歝n.bugstack.springframework.core.io.ClassPathResource
public class ClassPathResource implements Resource { private final String path; private ClassLoader classLoader; public ClassPathResource(String path) { this(path, (ClassLoader) null); } public ClassPathResource(String path, ClassLoader classLoader) { Assert.notNull(path, "Path must not be null"); this.path = path; this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); } @Override public InputStream getInputStream() throws IOException { InputStream is = classLoader.getResourceAsStream(path); if (is == null) { throw new FileNotFoundException( this.path + " cannot be opened because it does not exist"); } return is; } }
- 杩欎竴閮ㄥ垎鐨勫疄鐜版槸鐢ㄤ簬閫氳繃
ClassLoader
璇诲彇ClassPath
涓嬬殑鏂囦欢淇℃伅锛屽叿浣撶殑璇诲彇杩囩▼涓昏鏄細classLoader.getResourceAsStream(path)
FileSystem锛歝n.bugstack.springframework.core.io.FileSystemResource
public class FileSystemResource implements Resource { private final File file; private final String path; public FileSystemResource(File file) { this.file = file; this.path = file.getPath(); } public FileSystemResource(String path) { this.file = new File(path); this.path = path; } @Override public InputStream getInputStream() throws IOException { return new FileInputStream(this.file); } public final String getPath() { return this.path; } }
- 閫氳繃鎸囧畾鏂囦欢璺緞鐨勬柟寮忚鍙栨枃浠朵俊鎭紝杩欓儴鍒嗗ぇ瀹惰偗瀹氳繕鏄潪甯哥啛鎮夌殑锛岀粡甯镐細璇诲彇涓€浜泃xt銆乪xcel鏂囦欢杈撳嚭鍒版帶鍒跺彴銆?/li>
Url锛歝n.bugstack.springframework.core.io.UrlResource
public class UrlResource implements Resource{ private final URL url; public UrlResource(URL url) { Assert.notNull(url,"URL must not be null"); this.url = url; } @Override public InputStream getInputStream() throws IOException { URLConnection con = this.url.openConnection(); try { return con.getInputStream(); } catch (IOException ex){ if (con instanceof HttpURLConnection){ ((HttpURLConnection) con).disconnect(); } throw ex; } } }
- 閫氳繃 HTTP 鐨勬柟寮忚鍙栦簯鏈嶅姟鐨勬枃浠讹紝鎴戜滑涔熷彲浠ユ妸閰嶇疆鏂囦欢鏀惧埌 GitHub 鎴栬€?Gitee 涓娿€?/li>
3. 鍖呰璧勬簮鍔犺浇鍣?/h3>
鎸夌収璧勬簮鍔犺浇鐨勪笉鍚屾柟寮忥紝璧勬簮鍔犺浇鍣ㄥ彲浠ユ妸杩欎簺鏂瑰紡闆嗕腑鍒扮粺涓€鐨勭被鏈嶅姟涓嬭繘琛屽鐞嗭紝澶栭儴鐢ㄦ埛鍙渶瑕佷紶閫掕祫婧愬湴鍧€鍗冲彲锛岀畝鍖栦娇鐢ㄣ€?/p>
瀹氫箟鎺ュ彛锛歝n.bugstack.springframework.core.io.ResourceLoader
public interface ResourceLoader { /** * Pseudo URL prefix for loading from the class path: "classpath:" */ String CLASSPATH_URL_PREFIX = "classpath:"; Resource getResource(String location); }
- 瀹氫箟鑾峰彇璧勬簮鎺ュ彛锛岄噷闈紶閫?location 鍦板潃鍗冲彲銆?/li>
瀹炵幇鎺ュ彛锛歝n.bugstack.springframework.core.io.DefaultResourceLoader
public class DefaultResourceLoader implements ResourceLoader { @Override public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); if (location.startsWith(CLASSPATH_URL_PREFIX)) { return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length())); } else { try { URL url = new URL(location); return new UrlResource(url); } catch (MalformedURLException e) { return new FileSystemResource(location); } } } }
- 鍦ㄨ幏鍙栬祫婧愮殑瀹炵幇涓紝涓昏鏄妸涓夌涓嶅悓绫诲瀷鐨勮祫婧愬鐞嗘柟寮忚繘琛屼簡鍖呰锛屽垎涓猴細鍒ゆ柇鏄惁涓篊lassPath銆乁RL浠ュ強鏂囦欢銆?/li>
- 铏界劧 DefaultResourceLoader 绫诲疄鐜扮殑杩囩▼绠€鍗曪紝浣嗚繖涔熸槸璁捐妯″紡绾﹀畾鐨勫叿浣撶粨鏋滐紝鍍忔槸杩欓噷涓嶄細璁╁閮ㄨ皟鐢ㄦ斁鐭ラ亾杩囧鐨勭粏鑺傦紝鑰屾槸浠呭叧蹇冨叿浣撹皟鐢ㄧ粨鏋滃嵆鍙€?/li>
4. Bean瀹氫箟璇诲彇鎺ュ彛
cn.bugstack.springframework.beans.factory.support.BeanDefinitionReader
public interface BeanDefinitionReader { BeanDefinitionRegistry getRegistry(); ResourceLoader getResourceLoader(); void loadBeanDefinitions(Resource resource) throws BeansException; void loadBeanDefinitions(Resource... resources) throws BeansException; void loadBeanDefinitions(String location) throws BeansException; }
- 杩欐槸涓€涓?Simple interface for bean definition readers. 鍏跺疄閲岄潰鏃犻潪瀹氫箟浜嗗嚑涓柟娉曪紝鍖呮嫭锛歡etRegistry()銆乬etResourceLoader()锛屼互鍙婁笁涓姞杞紹ean瀹氫箟鐨勬柟娉曘€?/li>
- 杩欓噷闇€瑕佹敞鎰?getRegistry()銆乬etResourceLoader()锛岄兘鏄敤浜庢彁渚涚粰鍚庨潰涓変釜鏂规硶鐨勫伐鍏凤紝鍔犺浇鍜屾敞鍐岋紝杩欎袱涓柟娉曠殑瀹炵幇浼氬寘瑁呭埌鎶借薄绫讳腑锛屼互鍏嶆薄鏌撳叿浣撶殑鎺ュ彛瀹炵幇鏂规硶銆?/li>
5. Bean瀹氫箟鎶借薄绫诲疄鐜?/h3>
cn.bugstack.springframework.beans.factory.support.AbstractBeanDefinitionReader
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader { private final BeanDefinitionRegistry registry; private ResourceLoader resourceLoader; protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) { this(registry, new DefaultResourceLoader()); } public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) { this.registry = registry; this.resourceLoader = resourceLoader; } @Override public BeanDefinitionRegistry getRegistry() { return registry; } @Override public ResourceLoader getResourceLoader() { return resourceLoader; } }
- 鎶借薄绫绘妸 BeanDefinitionReader 鎺ュ彛鐨勫墠涓や釜鏂规硶鍏ㄩ儴瀹炵幇瀹屼簡锛屽苟鎻愪緵浜嗘瀯閫犲嚱鏁帮紝璁╁閮ㄧ殑璋冪敤浣跨敤鏂癸紝鎶夿ean瀹氫箟娉ㄥ叆绫伙紝浼犻€掕繘鏉ャ€?/li>
- 杩欐牱鍦ㄦ帴鍙?BeanDefinitionReader 鐨勫叿浣撳疄鐜扮被涓紝灏卞彲浠ユ妸瑙f瀽鍚庣殑 XML 鏂囦欢涓殑 Bean 淇℃伅锛屾敞鍐屽埌 Spring 瀹瑰櫒鍘讳簡銆?em>浠ュ墠鎴戜滑鏄€氳繃鍗曞厓娴嬭瘯浣跨敤锛岃皟鐢?BeanDefinitionRegistry 瀹屾垚Bean鐨勬敞鍐岋紝鐜板湪鍙互鏀惧埌 XMl 涓搷浣滀簡
6. 瑙f瀽XML澶勭悊Bean娉ㄥ唽
cn.bugstack.springframework.beans.factory.xml.XmlBeanDefinitionReader
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) { super(registry); } public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) { super(registry, resourceLoader); } @Override public void loadBeanDefinitions(Resource resource) throws BeansException { try { try (InputStream inputStream = resource.getInputStream()) { doLoadBeanDefinitions(inputStream); } } catch (IOException | ClassNotFoundException e) { throw new BeansException("IOException parsing XML document from " + resource, e); } } @Override public void loadBeanDefinitions(Resource... resources) throws BeansException { for (Resource resource : resources) { loadBeanDefinitions(resource); } } @Override public void loadBeanDefinitions(String location) throws BeansException { ResourceLoader resourceLoader = getResourceLoader(); Resource resource = resourceLoader.getResource(location); loadBeanDefinitions(resource); } protected void doLoadBeanDefinitions(InputStream inputStream) throws ClassNotFoundException { Document doc = XmlUtil.readXML(inputStream); Element root = doc.getDocumentElement(); NodeList childNodes = root.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { // 鍒ゆ柇鍏冪礌 if (!(childNodes.item(i) instanceof Element)) continue; // 鍒ゆ柇瀵硅薄 if (!"bean".equals(childNodes.item(i).getNodeName())) continue; // 瑙f瀽鏍囩 Element bean = (Element) childNodes.item(i); String id = bean.getAttribute("id"); String name = bean.getAttribute("name"); String className = bean.getAttribute("class"); // 鑾峰彇 Class锛屾柟渚胯幏鍙栫被涓殑鍚嶇О Class<?> clazz = Class.forName(className); // 浼樺厛绾?id > name String beanName = StrUtil.isNotEmpty(id) ? id : name; if (StrUtil.isEmpty(beanName)) { beanName = StrUtil.lowerFirst(clazz.getSimpleName()); } // 瀹氫箟Bean BeanDefinition beanDefinition = new BeanDefinition(clazz); // 璇诲彇灞炴€у苟濉厖 for (int j = 0; j < bean.getChildNodes().getLength(); j++) { if (!(bean.getChildNodes().item(j) instanceof Element)) continue; if (!"property".equals(bean.getChildNodes().item(j).getNodeName())) continue; // 瑙f瀽鏍囩锛歱roperty Element property = (Element) bean.getChildNodes().item(j); String attrName = property.getAttribute("name"); String attrValue = property.getAttribute("value"); String attrRef = property.getAttribute("ref"); // 鑾峰彇灞炴€у€硷細寮曞叆瀵硅薄銆佸€煎璞? Object value = StrUtil.isNotEmpty(attrRef) ? new BeanReference(attrRef) : attrValue; // 鍒涘缓灞炴€т俊鎭? PropertyValue propertyValue = new PropertyValue(attrName, value); beanDefinition.getPropertyValues().addPropertyValue(propertyValue); } if (getRegistry().containsBeanDefinition(beanName)) { throw new BeansException("Duplicate beanName[" + beanName + "] is not allowed"); } // 娉ㄥ唽 BeanDefinition getRegistry().registerBeanDefinition(beanName, beanDefinition); } } }
XmlBeanDefinitionReader 绫绘渶鏍稿績鐨勫唴瀹瑰氨鏄 XML 鏂囦欢鐨勮В鏋愶紝鎶婃垜浠湰鏉ュ湪浠g爜涓殑鎿嶄綔鏀惧埌浜嗛€氳繃瑙f瀽 XML 鑷姩娉ㄥ唽鐨勬柟寮忋€?/p>
- loadBeanDefinitions 鏂规硶锛屽鐞嗚祫婧愬姞杞斤紝杩欓噷鏂板鍔犱簡涓€涓唴閮ㄦ柟娉曪細
doLoadBeanDefinitions
锛屽畠涓昏璐熻矗瑙f瀽 xml- 鍦?doLoadBeanDefinitions 鏂规硶涓紝涓昏鏄xml鐨勮鍙?
XmlUtil.readXML(inputStream)
鍜屽厓绱?Element 瑙f瀽銆傚湪瑙f瀽鐨勮繃绋嬩腑閫氳繃寰幆鎿嶄綔锛屼互姝よ幏鍙?Bean 閰嶇疆浠ュ強閰嶇疆涓殑 id銆乶ame銆乧lass銆乿alue銆乺ef 淇℃伅銆?/li>- 鏈€缁堟妸璇诲彇鍑烘潵鐨勯厤缃俊鎭紝鍒涘缓鎴?BeanDefinition 浠ュ強 PropertyValue锛屾渶缁堟妸瀹屾暣鐨?Bean 瀹氫箟鍐呭娉ㄥ唽鍒?Bean 瀹瑰櫒锛?code>getRegistry().registerBeanDefinition(beanName, beanDefinition)
浜斻€佹祴璇?/h2>
1. 浜嬪厛鍑嗗
cn.bugstack.springframework.test.bean.UserDao
public class UserDao { private static Map<String, String> hashMap = new HashMap<>(); static { hashMap.put("10001", "灏忓倕鍝?); hashMap.put("10002", "鍏澂姘?); hashMap.put("10003", "闃挎瘺"); } public String queryUserName(String uId) { return hashMap.get(uId); } }
cn.bugstack.springframework.test.bean.UserService
public class UserService { private String uId; private UserDao userDao; public void queryUserInfo() { return userDao.queryUserName(uId); } // ...get/set }
- Dao銆丼ervice锛屾槸鎴戜滑骞冲父寮€鍙戠粡甯镐娇鐢ㄧ殑鍦烘櫙銆傚湪 UserService 涓敞鍏?UserDao锛岃繖鏍峰氨鑳戒綋鐜板嚭Bean灞炴€х殑渚濊禆浜嗐€?/li>
2. 閰嶇疆鏂囦欢
important.properties
# Config File system.key=OLpj9823dZ
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="userDao" class="cn.bugstack.springframework.test.bean.UserDao"/> <bean id="userService" class="cn.bugstack.springframework.test.bean.UserService"> <property name="uId" value="10001"/> <property name="userDao" ref="userDao"/> </bean> </beans>
- 杩欓噷鏈変袱浠介厤缃枃浠讹紝涓€浠界敤浜庢祴璇曡祫婧愬姞杞藉櫒锛屽彟澶?spring.xml 鐢ㄤ簬娴嬭瘯鏁翠綋鐨?Bean 娉ㄥ唽鍔熻兘銆?/li>
3. 鍗曞厓娴嬭瘯(璧勬簮鍔犺浇)
妗堜緥
private DefaultResourceLoader resourceLoader; @Before public void init() { resourceLoader = new DefaultResourceLoader(); } @Test public void test_classpath() throws IOException { Resource resource = resourceLoader.getResource("classpath:important.properties"); InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content); } @Test public void test_file() throws IOException { Resource resource = resourceLoader.getResource("src/test/resources/important.properties"); InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content); } @Test public void test_url() throws IOException { Resource resource = resourceLoader.getResource("https://github.com/fuzhengwei/small-spring/important.properties" InputStream inputStream = resource.getInputStream(); String content = IoUtil.readUtf8(inputStream); System.out.println(content); }
娴嬭瘯缁撴灉
# Config File system.key=OLpj9823dZ Process finished with exit code 0
- 杩欎笁涓柟娉曪細test_classpath銆乼est_file銆乼est_url锛屽垎鍒敤浜庢祴璇曞姞杞?ClassPath銆丗ileSystem銆乁rl 鏂囦欢锛?em>URL鏂囦欢鍦℅ithub锛屽彲鑳藉姞杞芥椂浼氭參
4. 鍗曞厓娴嬭瘯(閰嶇疆鏂囦欢娉ㄥ唽Bean)
妗堜緥
@Test public void test_xml() { // 1.鍒濆鍖?BeanFactory DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // 2. 璇诲彇閰嶇疆鏂囦欢&娉ㄥ唽Bean XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions("classpath:spring.xml"); // 3. 鑾峰彇Bean瀵硅薄璋冪敤鏂规硶 UserService userService = beanFactory.getBean("userService", UserService.class); String result = userService.queryUserInfo(); System.out.println("娴嬭瘯缁撴灉锛? + result); }
娴嬭瘯缁撴灉
娴嬭瘯缁撴灉锛氬皬鍌呭摜 Process finished with exit code 0
- 鍦ㄤ笂闈㈢殑娴嬭瘯妗堜緥涓彲浠ョ湅鍒帮紝鎴戜滑鎶婁互鍓嶉€氳繃鎵嬪姩娉ㄥ唽 Bean 浠ュ強閰嶇疆灞炴€т俊鎭殑鍐呭锛屼氦缁欎簡
new XmlBeanDefinitionReader(beanFactory)
绫昏鍙?Spring.xml 鐨勬柟寮忔潵澶勭悊锛屽苟閫氳繃浜嗘祴璇曢獙璇併€?/li>鍏€佹€荤粨
- 姝ゆ椂鐨勫伐绋嬬粨鏋勫凡缁忚秺鏉ヨ秺鏈?Spring 妗嗘灦鐨勫懗閬撲簡锛屼互閰嶇疆鏂囦欢涓哄叆鍙hВ鏋愬拰娉ㄥ唽 Bean 淇℃伅锛屾渶缁堝啀閫氳繃 Bean 宸ュ巶鑾峰彇 Bean 浠ュ強鍋氱浉搴旂殑璋冪敤鎿嶄綔銆?/li>
- 鍏充簬妗堜緥涓瘡涓€涓楠ょ殑瀹炵幇灏忓倕鍝ヨ繖閲岄兘浼氬敖鍙兘鍙傜収 Spring 婧愮爜鐨勬帴鍙e畾涔夈€佹娊璞$被瀹炵幇銆佸悕绉拌鑼冦€佷唬鐮佺粨鏋勭瓑锛屽仛鐩稿簲鐨勭畝鍖栧鐞嗐€傝繖鏍峰ぇ瀹跺湪瀛︿範鐨勮繃绋嬩腑涔熷彲浠ラ€氳繃绫诲悕鎴栬€呮帴鍙e拰鏁翠釜缁撴瀯浣撳涔?Spring 婧愮爜锛岃繖鏍峰涔犺捣鏉ュ氨瀹规槗澶氫簡銆?/li>
- 鐪嬪畬缁濆涓嶇瓑浜庝細锛屼綘鍙湁鍔ㄨ捣鎵嬫潵浠庝竴涓皬灏忕殑宸ョ▼妗嗘灦缁撴瀯锛屾暡鍒扮幇鍦ㄤ互鍙婁互鍚庝笉鏂殑鍙樺ぇ銆佸彉澶氥€佸彉寮烘椂锛屾墠鑳界湡鐨勬帉鎻¤繖閲岄潰鐨勭煡璇嗐€傚彟澶栨瘡涓€涓珷鑺傜殑鍔熻兘瀹炵幇閮戒細娑夊強鍒板緢澶氱殑浠g爜璁捐鎬濊矾锛岃璁ょ湡鍘婚鎮熴€?em>褰撶劧瀹炶返璧锋潵鏄渶濂界殑棰嗘偀鏂瑰紡锛?/em>
涓冦€佺郴鍒楁帹鑽?/h2>
- 缁欎綘涓€鍙版湇鍔″櫒锛屼綘鑳芥妸浣犲啓鐨勪唬鐮侀儴缃插埌绾夸笂鍚楋紵
- 姣曚笟鍓嶅啓浜?0涓囪浠g爜锛岃鎴戜粠鎴愪负鍚屽鐪奸噷鐨勯潰闇革紒
- 鏁板锛岀涓€涓▼搴忓憳鏈夊杩戯紵
- 涓€娆′唬鐮佽瘎瀹★紝宸偣杩囦笉浜嗚瘯鐢ㄦ湡锛?/a>
- 浠ユ暟瀛︾煡璇嗙偣寮€濮嬶紝娣卞叆璁茶В Java 鐨勬牳蹇冩妧鏈?00椤礘ava闈㈢粡
以上是关于鎵嬫捀Spring妗嗘灦锛岃璁′笌瀹炵幇璧勬簮鍔犺浇鍣紝浠嶴pring.xml瑙f瀽鍜屾敞鍐孊ean瀵硅薄的主要内容,如果未能解决你的问题,请参考以下文章
寰蒋鍜岃胺姝屽垎鍒紑婧愬垎甯冨紡娣卞害瀛︿範妗嗘灦锛屽悇鑷帀瀹冲湪鍝紵