https://bugstack.cn娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/blockquote>涓€銆佸墠瑷€浣犲啓鐨勪唬鐮侊紝鑳芥帴鐨勪"/>

鎵嬫捀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瀵硅薄的主要内容,如果未能解决你的问题,请参考以下文章

璇嶆硶鍒嗘瀽绋嬪簭鐨勮璁′笌瀹炵幇

寰蒋鍜岃胺姝屽垎鍒紑婧愬垎甯冨紡娣卞害瀛︿範妗嗘灦锛屽悇鑷帀瀹冲湪鍝紵

GeoGebra閲嶅鎵嬫瀹炵幇

鎵嬫満QQ 寮€婧愬熀浜嶧lutter 鐨勫姩鎬佸寲妗嗘灦 MXFlutter 锛岀墰閫硷紒

绉嬪悗涓€棰楃硸锛屾暀浣犱娇鐢╯ekiro jsrpc