Spring缁忓吀楂橀闈㈣瘯棰橈紝鍘熸潵鏄暱杩欎釜鏍峰瓙

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring缁忓吀楂橀闈㈣瘯棰橈紝鍘熸潵鏄暱杩欎釜鏍峰瓙相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%87%92%e5%8a%a0%e8%bd%bd' title='鎳掑姞杞?>鎳掑姞杞?/a>   main   world   web妗嗘灦   sel   isp   weakref   

Spring缁忓吀楂橀闈㈣瘯棰橈紝鍘熸潵鏄暱杩欎釜鏍峰瓙

鐗堟潈澹版槑锛氭湰鏂囦负鍗氫富鍘熷垱鏂囩珷锛岄伒寰?a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"> CC 4.0 BY-SA 鐗堟潈鍗忚锛岃浆杞借闄勪笂鍘熸枃鍑哄閾炬帴鍜屾湰澹版槑銆?/span>
鏈枃閾炬帴锛?a href="https://blog.csdn.net/broadview2006/article/details/100037214">https://blog.csdn.net/broadview2006/article/details/100037214

鏈枃閫夎嚜銆奡pring 5鏍稿績鍘熺悊涓?0涓被鎵嬪啓瀹炴垬銆嬩竴涔︼紝鏂囨湯鍙備笌浜掑姩鏈夋満浼氳耽鍙栨湰涔︺€傛湰鏂囬鐩洰褰曪細

1  浠€涔堟槸Spring妗嗘灦锛孲pring妗嗘灦鏈夊摢浜涗富瑕佹ā鍧?/p>

2  浣跨敤Spring妗嗘灦鑳藉甫鏉ュ摢浜涘ソ澶?/p>

3  浠€涔堟槸鎺у埗鍙嶈浆锛圛oC锛夛紝浠€涔堟槸渚濊禆娉ㄥ叆

4  鍦↗ava涓緷璧栨敞鍏ユ湁鍝簺鏂瑰紡

5  BeanFactory鍜孉pplicationContext鏈変粈涔堝尯鍒?/p>

6  Spring鎻愪緵鍑犵閰嶇疆鏂瑰紡鏉ヨ缃厓鏁版嵁

7  濡備綍浣跨敤XML閰嶇疆鏂瑰紡閰嶇疆Spring

8  Spring鎻愪緵鍝簺閰嶇疆褰㈠紡

9  鎬庢牱鐢ㄦ敞瑙g殑鏂瑰紡閰嶇疆Spring

10  璇疯В閲奡pring Bean鐨勭敓鍛藉懆鏈?/p>

11  鏇村缁忓吀楂橀闈㈣瘯棰?/p>

鎶€鏈浘鐗? class=

1  浠€涔堟槸Spring妗嗘灦锛孲pring妗嗘灦鏈夊摢浜涗富瑕佹ā鍧?/p>

Spring妗嗘灦鏄竴涓负Java搴旂敤绋嬪簭寮€鍙戞彁渚涚患鍚堛€佸箍娉涚殑鍩虹鎬ф敮鎸佺殑Java骞冲彴銆係pring甯姪寮€鍙戣€呰В鍐充簡寮€鍙戜腑鍩虹鎬х殑闂锛屼娇寰楀紑鍙戜汉鍛樺彲浠ヤ笓娉ㄤ簬搴旂敤绋嬪簭鐨勫紑鍙戙€係pring妗嗘灦鏈韩涔熸槸鎸夌収璁捐妯″紡绮惧績鎵撻€犵殑锛岃繖浣垮緱鎴戜滑鍙互鍦ㄥ紑鍙戠幆澧冧腑瀹夊績鍦伴泦鎴怱pring妗嗘灦锛屼笉蹇呮媴蹇僑pring鏄浣曞湪鍚庡彴宸ヤ綔鐨勩€?/p>

鎶€鏈浘鐗? class=

 

2  浣跨敤Spring妗嗘灦鑳藉甫鏉ュ摢浜涘ソ澶?/p>

涓嬮潰鍒椾妇浜嗕竴浜涗娇鐢⊿pring妗嗘灦甯︽潵鐨勪富瑕佸ソ澶勩€?/p>

锛?锛塂ependency Injection锛圖I锛変娇寰楁瀯閫犲櫒鍜孞avaBean properties鏂囦欢涓殑渚濊禆鍏崇郴涓€鐩簡鐒躲€?/p>

锛?锛変笌EJB瀹瑰櫒鐩告瘮杈冿紝IoC瀹瑰櫒鏇村姞瓒嬪悜浜庤交閲忕骇銆傝繖鏍蜂竴鏉ヤ娇鐢↖oC瀹瑰櫒鍦ㄦ湁闄愮殑鍐呭瓨鍜孋PU璧勬簮鐨勬儏鍐典笅杩涜搴旂敤绋嬪簭鐨勫紑鍙戝拰鍙戝竷灏卞彉寰楀崄鍒嗘湁鍒┿€?/p>

锛?锛塖pring骞舵病鏈夐棴闂ㄩ€犺溅锛孲pring鍒╃敤浜嗗凡鏈夌殑鎶€鏈紝姣斿ORM妗嗘灦銆乴ogging妗嗘灦銆丣2EE銆丵uartz鍜孞DK Timer锛屼互鍙婂叾浠栬鍥炬妧鏈€?/p>

锛?锛塖pring妗嗘灦鏄寜鐓фā鍧楃殑褰㈠紡鏉ョ粍缁囩殑銆傜敱鍖呭拰绫荤殑缂栧彿灏卞彲浠ョ湅鍑哄叾鎵€灞炵殑妯″潡锛屽紑鍙戣€呭彧闇€閫夌敤闇€瑕佺殑妯″潡鍗冲彲銆?/p>

锛?锛夎娴嬭瘯涓€涓敤Spring寮€鍙戠殑搴旂敤绋嬪簭鍗佸垎绠€鍗曪紝鍥犱负娴嬭瘯鐩稿叧鐨勭幆澧冧唬鐮侀兘宸茬粡鍥婃嫭鍦ㄦ鏋朵腑浜嗐€傛洿鍔犵畝鍗曠殑鏄紝鍒╃敤JavaBean褰㈠紡鐨凱OJO绫伙紝鍙互寰堟柟渚垮湴鍒╃敤渚濊禆娉ㄥ叆鏉ュ啓鍏ユ祴璇曟暟鎹€?/p>

锛?锛塖pring鐨刉eb妗嗘灦涔熸槸涓€涓簿蹇冭璁$殑Web MVC妗嗘灦锛屼负寮€鍙戣€呭湪Web妗嗘灦鐨勯€夋嫨涓婃彁渚涗簡涓€涓櫎涓绘祦妗嗘灦锛堟瘮濡係truts锛夊拰杩囧害璁捐鐨勩€佷笉娴佽Web妗嗘灦浠ュ鐨勯€夋嫨銆?/p>

锛?锛塖pring鎻愪緵浜嗕竴涓究鎹风殑浜嬪姟绠$悊鎺ュ彛锛岄€傜敤浜庡皬鍨嬬殑鏈湴浜嬪姟澶勭悊锛堟瘮濡傚湪鍗旸B鐨勭幆澧冧笅锛夊拰澶嶆潅鐨勫叡鍚屼簨鍔″鐞嗭紙姣斿鍒╃敤JTA鐨勫鏉侱B鐜锛夈€?/p>

 

鎶€鏈浘鐗? class=

3  浠€涔堟槸鎺у埗鍙嶈浆锛圛oC锛夛紝浠€涔堟槸渚濊禆娉ㄥ叆

锛?锛夋帶鍒跺弽杞槸搴旂敤浜庤蒋浠跺伐绋嬮鍩熺殑锛屽湪杩愯鏃惰瑁呴厤鍣ㄥ璞$敤鏉ョ粦瀹氳€﹀悎瀵硅薄鐨勪竴绉嶇紪绋嬫妧宸э紝瀵硅薄涔嬮棿鐨勮€﹀悎鍏崇郴鍦ㄧ紪璇戞椂閫氬父鏄湭鐭ョ殑銆傚湪浼犵粺鐨勭紪绋嬫柟寮忎腑锛屼笟鍔¢€昏緫鐨勬祦绋嬫槸鐢卞簲鐢ㄧ▼搴忎腑鏃╁凡琚瀹氬ソ鍏宠仈鍏崇郴鐨勫璞℃潵鍐冲畾鐨勩€傚湪浣跨敤鎺у埗鍙嶈浆鐨勬儏鍐典笅锛屼笟鍔¢€昏緫鐨勬祦绋嬫槸鐢卞璞″叧绯诲浘鏉ュ喅瀹氱殑锛岃瀵硅薄鍏崇郴鍥剧敱瑁呴厤鍣ㄨ礋璐e疄渚嬪寲锛岃繖绉嶅疄鐜版柟寮忚繕鍙互灏嗗璞′箣闂寸殑鍏宠仈鍏崇郴鐨勫畾涔夋娊璞″寲銆傜粦瀹氱殑杩囩▼鏄€氳繃“渚濊禆娉ㄥ叆”瀹炵幇鐨勩€?/p>

锛?锛夋帶鍒跺弽杞槸涓€绉嶄互缁欎簣搴旂敤绋嬪簭涓洰鏍囩粍浠舵洿澶氭帶鍒朵负鐩殑璁捐鑼冨紡锛屽苟鍦ㄥ疄闄呭伐浣滀腑璧峰埌浜嗘湁鏁堢殑浣滅敤銆?/p>

锛?锛変緷璧栨敞鍏ユ槸鍦ㄧ紪璇戦樁娈靛皻鏈煡鎵€闇€鐨勫姛鑳芥槸鏉ヨ嚜鍝釜鐨勭被鐨勬儏鍐典笅锛屽皢鍏朵粬瀵硅薄鎵€渚濊禆鐨勫姛鑳藉璞″疄渚嬪寲鐨勬ā寮忋€傝繖灏遍渶瑕佷竴绉嶆満鍒舵潵婵€娲荤浉搴旂殑缁勪欢浠ユ彁渚涚壒瀹氱殑鍔熻兘锛屾墍浠ヤ緷璧栨敞鍏ユ槸鎺у埗鍙嶈浆鐨勫熀纭€銆傚惁鍒欏鏋滃湪缁勪欢涓嶅彈妗嗘灦鎺у埗鐨勬儏鍐典笅锛屾鏋跺張鎬庝箞鐭ラ亾瑕佸垱寤哄摢涓粍浠跺憿锛?/p>

 

鎶€鏈浘鐗? class=

4  鍦↗ava涓緷璧栨敞鍏ユ湁鍝簺鏂瑰紡

锛?锛夋瀯閫犲櫒娉ㄥ叆銆?/p>

锛?锛塖etter鏂规硶娉ㄥ叆銆?/p>

锛?锛夋帴鍙f敞鍏ャ€?/p>

 

鎶€鏈浘鐗? class=

5  BeanFactory鍜孉pplicationContext鏈変粈涔堝尯鍒?/p>

BeanFactory 鍙互鐞嗚В涓哄惈鏈塀ean闆嗗悎鐨勫伐鍘傜被銆侭eanFactory 鍖呭惈浜咮ean鐨勫畾涔夛紝浠ヤ究鍦ㄦ帴鏀跺埌瀹㈡埛绔姹傛椂灏嗗搴旂殑Bean瀹炰緥鍖栥€?/p>

BeanFactory杩樿兘鍦ㄥ疄渚嬪寲瀵硅薄鏃剁敓鎴愬崗浣滅被涔嬮棿鐨勫叧绯汇€傛涓惧皢Bean鑷韩浠嶣ean瀹㈡埛绔殑閰嶇疆涓В鏀惧嚭鏉ャ€侭eanFactory杩樺寘鍚獴ean鐢熷懡鍛ㄦ湡鐨勬帶鍒讹紝璋冪敤瀹㈡埛绔殑鍒濆鍖栨柟娉曪紙Initialization Method锛夊拰閿€姣佹柟娉曪紙Destruction Method锛夈€?/p>

浠庤〃闈笂鐪嬶紝ApplicationContext濡傚悓BeanFactory涓€鏍峰叿鏈塀ean瀹氫箟銆丅ean鍏宠仈鍏崇郴鐨勮缃強鏍规嵁璇锋眰鍒嗗彂Bean鐨勫姛鑳姐€備絾ApplicationContext鍦ㄦ鍩虹涓婅繕鎻愪緵浜嗗叾浠栧姛鑳姐€?/p>

锛?锛夋彁渚涗簡鏀寔鍥介檯鍖栫殑鏂囨湰娑堟伅銆?/p>

锛?锛夌粺涓€鐨勮祫婧愭枃浠惰鍙栨柟寮忋€?/p>

锛?锛夊凡鍦ㄧ洃鍚櫒涓敞鍐岀殑Bean鐨勪簨浠躲€?/p>

浠ヤ笅鏄笁绉嶈緝甯歌鐨?ApplicationContext 瀹炵幇鏂瑰紡銆?/p>

锛?锛?ClassPathXmlApplicationContext锛?nbsp;浠嶤lassPath鐨刋ML閰嶇疆鏂囦欢涓鍙栦笂涓嬫枃锛屽苟鐢熸垚涓婁笅鏂囧畾涔夈€傚簲鐢ㄧ▼搴忎笂涓嬫枃浠庣▼搴忕幆澧冨彉閲忎腑鍙栧緱銆?nbsp;

  •  
ApplicationContext context = new ClassPathXmlApplicationContext(“application.xml”);

锛?锛塅ileSystemXmlApplicationContext 锛氱敱鏂囦欢绯荤粺涓殑XML閰嶇疆鏂囦欢璇诲彇涓婁笅鏂囥€?nbsp;

  •  
ApplicationContext context = new FileSystemXmlApplicationContext(“application.xml”);

锛?锛塜mlWebApplicationContext锛氱敱Web搴旂敤鐨刋ML鏂囦欢璇诲彇涓婁笅鏂囥€?/p>

 

鎶€鏈浘鐗? class=

6  Spring鎻愪緵鍑犵閰嶇疆鏂瑰紡鏉ヨ缃厓鏁版嵁

Spring鎻愪緵浠ヤ笅涓夌閰嶇疆鏂瑰紡鏉ヨ缃厓鏁版嵁锛?/p>

锛?锛夊熀浜嶺ML鐨勯厤缃€?/p>

锛?锛夊熀浜庢敞瑙g殑閰嶇疆銆?/p>

锛?锛夊熀浜嶫ava鐨勯厤缃€?/p>

 

鎶€鏈浘鐗? class=

7  濡備綍浣跨敤XML閰嶇疆鏂瑰紡閰嶇疆Spring

鍦⊿pring妗嗘灦涓紝渚濊禆鍜屾湇鍔¢渶瑕佷笓闂ㄧ殑閰嶇疆鏂囦欢瀹炵幇锛屼竴鑸敤XML鏍煎紡鐨勯厤缃枃浠躲€傝繖浜涢厤缃枃浠剁殑鏍煎紡閲囩敤鍏叡鐨勬ā鏉匡紝鐢变竴绯诲垪鐨凚ean瀹氫箟鍜屼笓闂ㄧ殑搴旂敤閰嶇疆閫夐」缁勬垚銆?nbsp;

Spring XML閰嶇疆鐨勪富瑕佺洰鐨勬槸浣挎墍鏈夌殑Spring缁勪欢閮藉彲浠ョ敤XML鏂囦欢鐨勫舰寮忔潵杩涜閰嶇疆銆傝繖鎰忓懗鐫€涓嶄細鍑虹幇鍏朵粬鐨凷pring閰嶇疆绫诲瀷锛堟瘮濡傚0鏄庨厤缃柟寮忔垨鍩轰簬Java Class鐨勯厤缃柟寮忥級銆?/p>

Spring鐨刋ML閰嶇疆鏂瑰紡鏄娇鐢ㄨSpring鍛藉悕绌洪棿鎵€鏀寔鐨勪竴绯诲垪鐨刋ML鏍囩鏉ュ疄鐜扮殑銆係pring涓昏鐨勫懡鍚嶇┖闂存湁context銆乥eans銆乯dbc銆乼x銆乤op銆乵vc鍜宎so銆備緥濡傦細

<beans>   <!-- JSON Support -->   <bean name="viewResolver"        class="org.springframework.web.servlet.view.BeanNameViewResolver"/>   <bean name="jsonTemplate"        class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>   <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/></beans>

涓嬮潰杩欎釜web.xml浠呴厤缃簡DispatcherServlet锛屾渶绠€鍗曠殑閰嶇疆渚胯兘婊¤冻搴旂敤绋嬪簭閰嶇疆杩愯鏃剁粍浠剁殑闇€姹傘€???????

<web-app>   <display-name>Archetype Created Web Application</display-name>   <servlet>      <servlet-name>spring</servlet-name>      <servlet-class>         org.springframework.web.servlet.DispatcherServlet      </servlet-class>      <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>      <servlet-name>spring</servlet-name>      <url-pattern>/</url-pattern>   </servlet-mapping></web-app>

鎶€鏈浘鐗? class=

8  Spring鎻愪緵鍝簺閰嶇疆褰㈠紡

Spring瀵笿ava閰嶇疆鐨勬敮鎸佹槸鐢盄Configuration娉ㄨВ鍜孈Bean娉ㄨВ鏉ュ疄鐜扮殑銆傜敱@Bean娉ㄨВ鐨勬柟娉曞皢浼氬疄渚嬪寲銆侀厤缃拰鍒濆鍖栦竴涓柊瀵硅薄锛岃繖涓璞″皢鐢盨pring鐨処oC瀹瑰櫒鏉ョ鐞嗐€侤Bean澹版槑鎵€璧峰埌鐨勪綔鐢ㄤ笌鍏冪礌绫讳技銆傝@Configuration鎵€娉ㄨВ鐨勭被鍒欒〃绀鸿繖涓被鐨勪富瑕佺洰鐨勬槸浣滀负Bean瀹氫箟鐨勮祫婧愩€傝@Configuration澹版槑鐨勭被鍙互閫氳繃鍦ㄥ悓涓€涓被鍐呴儴璋冪敤@bean鏂规硶鏉ヨ缃祵鍏ean鐨勪緷璧栧叧绯汇€?/p>

鏈€绠€鍗曠殑@Configuration 澹版槑绫昏鍙傝€冧笅闈㈢殑浠g爜锛???????

@Configurationpublic class AppConfig   @Bean   public MyService myService()       return new MyServiceImpl();   

涓庝笂闈㈢殑@Beans閰嶇疆鏂囦欢鐩稿悓鐨刋ML閰嶇疆鏂囦欢濡備笅锛???????

<beans>   <bean id="myService" class="com.gupaoedu.services.MyServiceImpl"/></beans>

涓婅堪閰嶇疆鏂瑰紡鐨勫疄渚嬪寲鏂瑰紡濡備笅锛???????

public static void main(String[] args)    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);   MyService myService = ctx.getBean(MyService.class);   myService.doStuff();

瑕佷娇鐢ㄧ粍浠舵壂鎻忥紝浠呴渶鐢ˊConfiguration杩涜娉ㄨВ鍗冲彲锛???????

@Configuration@ComponentScan(basePackages = "com.gupaoedu")public class AppConfig  

鍦ㄤ笂闈㈢殑渚嬪瓙涓紝com.gupaoedu鍖呴鍏堜細琚壂鎻忓埌锛岀劧鍚庡湪瀹瑰櫒鍐呮煡鎵捐@Component 澹版槑鐨勭被锛屾壘鍒板悗灏嗚繖浜涚被鎸夌収Spring Bean瀹氫箟杩涜娉ㄥ唽銆?nbsp;

濡傛灉浣犺鍦╓eb搴旂敤寮€鍙戜腑閫夌敤涓婅堪閰嶇疆鏂瑰紡锛岄渶瑕佺敤AnnotationConfigWebApplicationContext绫绘潵璇诲彇閰嶇疆鏂囦欢锛屽彲浠ョ敤鏉ラ厤缃甋pring鐨凷ervlet鐩戝惉鍣–ontrextLoaderListener鎴栬€匰pring MVC鐨凞ispatcherServlet銆?/p>

渚嬪锛???????

<web-app>
   <context-param>      <param-name>contextClass</param-name>      <param-value>         org.springframework.web.context.support.AnnotationConfigWebApplicationContext      </param-value>   </context-param>   <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>com.gupaoedu.AppConfig</param-value>   </context-param>   <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   </listener>   <servlet>      <servlet-name>dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>         <param-name>contextClass</param-name>         <param-value>            org.springframework.web.context.support.AnnotationConfigWebApplicationContext         </param-value>      </init-param>      <init-param>         <param-name>contextConfigLocation</param-name>         <param-value>com.gupaoedu.web.MVCConfig</param-value>      </init-param>   </servlet>   <servlet-mapping>      <servlet-name>dispatcher</servlet-name>      <url-pattern>/web/*</url-pattern>   </servlet-mapping></web-app>

9  鎬庢牱鐢ㄦ敞瑙g殑鏂瑰紡閰嶇疆Spring

Spring鍦?.5鐗堟湰浠ュ悗寮€濮嬫敮鎸佺敤娉ㄨВ鐨勬柟寮忛厤缃緷璧栨敞鍏ャ€傚彲浠ョ敤娉ㄨВ鐨勬柟寮忔潵鏇夸唬XML鏂瑰紡鐨凚ean鎻忚堪锛屽彲浠ュ皢Bean鎻忚堪杞Щ鍒扮粍浠剁被鐨勫唴閮紝鍙渶瑕佸湪鐩稿叧绫讳笂銆佹柟娉曚笂鎴栬€呭瓧娈靛0鏄庝笂浣跨敤娉ㄨВ鍗冲彲銆傛敞瑙f敞鍏ュ皢浼氳瀹瑰櫒鍦╔ML娉ㄥ叆涔嬪墠澶勭悊锛屾墍浠ュ悗鑰呬細瑕嗙洊鍓嶈€呭浜庡悓涓€涓睘鎬х殑澶勭悊缁撴灉銆?/p>

娉ㄨВ瑁呴厤鍦⊿pring涓槸榛樿鍏抽棴鐨勶紝闇€瑕佸湪Spring鏂囦欢涓繘琛岄厤缃墠鑳戒娇鐢ㄥ熀浜庢敞瑙g殑瑁呴厤妯″紡銆傚鏋滀綘鎯宠鍦ㄥ簲鐢ㄧ▼搴忎腑浣跨敤娉ㄨВ鐨勬柟寮忥紝璇峰弬鑰冨涓嬮厤缃細???????

<beans>   <context:annotation-config/></beans>

閰嶇疆瀹屾垚浠ュ悗锛屽氨鍙互鐢ㄦ敞瑙g殑鏂瑰紡鍦⊿pring涓悜灞炴€с€佹柟娉曞拰鏋勯€犳柟娉曚腑鑷姩瑁呴厤鍙橀噺銆?/p>

涓嬮潰鏄嚑绉嶆瘮杈冮噸瑕佺殑娉ㄨВ绫诲瀷銆?/p>

锛?锛堾Required锛氳娉ㄨВ搴旂敤浜庤鍊兼柟娉曘€?nbsp;

锛?锛堾Autowired锛氳娉ㄨВ搴旂敤浜庤鍊兼柟娉曘€侀潪璁惧€兼柟娉曘€佹瀯閫犳柟娉曞拰鍙橀噺銆?nbsp;

锛?锛堾Qualifier锛氳娉ㄨВ鍜孈Autowired娉ㄨВ鎼厤浣跨敤锛岀敤浜庢秷闄ょ壒瀹欱ean鑷姩瑁呴厤鐨勬涔夈€?nbsp;

锛?锛塉SR-250 Annotations锛歋pring鏀寔鍩轰簬JSR-250 娉ㄨВ鐨勬敞瑙o紝鍗矦Resource銆丂PostConstruct鍜孈PreDestroy銆?/p>

鎶€鏈浘鐗? class=

10  璇疯В閲奡pring Bean鐨勭敓鍛藉懆鏈?/p>

Spring Bean鐨勭敓鍛藉懆鏈熺畝鍗曟槗鎳傘€傚湪涓€涓狟ean瀹炰緥琚垵濮嬪寲鏃讹紝闇€瑕佹墽琛屼竴绯诲垪鍒濆鍖栨搷浣滀互浣垮叾杈惧埌鍙敤鐨勭姸鎬併€傚悓鏍凤紝褰撲竴涓狟ean涓嶅啀琚皟鐢ㄦ椂闇€瑕佽繘琛岀浉鍏崇殑鏋愭瀯鎿嶄綔锛屽苟浠嶣ean瀹瑰櫒涓Щ闄ゃ€?/p>

Spring Bean Factory 璐熻矗绠$悊鍦⊿pring瀹瑰櫒涓鍒涘缓鐨凚ean鐨勭敓鍛藉懆鏈熴€侭ean鐨勭敓鍛藉懆鏈熺敱涓ょ粍鍥炶皟鏂规硶缁勬垚銆?nbsp;

锛?锛夊垵濮嬪寲涔嬪悗璋冪敤鐨勫洖璋冩柟娉曘€?nbsp;

锛?锛夐攢姣佷箣鍓嶈皟鐢ㄧ殑鍥炶皟鏂规硶銆?nbsp;

Spring鎻愪緵浜嗕互涓?绉嶆柟寮忔潵绠$悊Bean鐨勭敓鍛藉懆鏈熶簨浠讹細

锛?锛塈nitializingBean鍜孌isposableBean鍥炶皟鎺ュ彛銆?/p>

锛?锛夐拡瀵圭壒娈婅涓虹殑鍏朵粬Aware鎺ュ彛銆?/p>

锛?锛塀ean閰嶇疆鏂囦欢涓殑 customInit() 鏂规硶鍜?customDestroy() 鏂规硶銆?/p>

锛?锛堾PostConstruct鍜孈PreDestroy娉ㄨВ鏂瑰紡銆?/p>

浣跨敤customInit()鍜?customDestroy()鏂规硶绠$悊Bean鐢熷懡鍛ㄦ湡鐨勪唬鐮佹牱渚嬪涓嬶細???????

<beans>   <bean id="demoBean" class="com.gupaoedu.task.DemoBean"        init-Method="customInit" destroy-Method="customDestroy">   </bean></beans>

鎶€鏈浘鐗? class=

鏇村缁忓吀楂橀闈㈣瘯棰?/p>

11  Spring Bean浣滅敤鍩熺殑鍖哄埆鏄粈涔?/p>

12  浠€涔堟槸Spring Inner Bean

13  Spring涓殑鍗曚緥Bean鏄嚎绋嬪畨鍏ㄧ殑鍚?/p>

14  璇蜂妇渚嬭鏄庡浣曞湪Spring涓敞鍏ヤ竴涓狫ava闆嗗悎

15  濡備綍鍚慡pring Bean涓敞鍏ava.util.Properties

16  璇疯В閲奡pring Bean鐨勮嚜鍔ㄨ閰?/p>

17  鑷姩瑁呴厤鏈夊摢浜涘眬闄愭€?/p>

18  璇疯В閲婂悇绉嶈嚜鍔ㄨ閰嶆ā寮忕殑鍖哄埆

19  璇蜂妇渚嬭В閲夽Required娉ㄨВ

20  璇蜂妇渚嬭鏄嶡Qualifier娉ㄨВ

21  鏋勯€犳柟娉曟敞鍏ュ拰璁惧€兼敞鍏ユ湁浠€涔堝尯鍒?/p>

22  Spring涓湁鍝簺涓嶅悓绫诲瀷鐨勪簨浠?/p>

23  FileSystemResource鍜孋lassPathResource鏈変粈涔堝尯鍒?/p>

24  Spring涓敤鍒颁簡鍝簺璁捐妯″紡

25  鍦⊿pring涓浣曟洿鏈夋晥鍦颁娇鐢↗DBC

26  璇疯В閲奡pring涓殑IoC瀹瑰櫒

27  鍦⊿pring涓彲浠ユ敞鍏ull鎴栫┖瀛楃涓插悧

 

 

Java妗嗘灦缁忓吀闈㈣瘯棰樹箣Spring锛堝唴闄勭瓟妗堬級 

Spring 妗嗘灦鏄竴涓紑婧愮殑Java骞冲彴锛屽畠涓哄鏄撹€屽揩閫熺殑寮€鍙戝嚭鑰愮敤鐨凧ava搴旂敤绋嬪簭鎻愪緵浜嗗叏闈㈢殑鍩虹璁炬柦銆備粖澶╁崈閿嬪皬缂栫潃閲嶇粰澶у鎬荤粨浜嗕竴浜涘叧浜嶴pring妗嗘灦鐨勭粡鍏搁潰璇曢锛屽笇鏈涜兘鍔╁ぇ瀹剁殑闈㈣瘯涓€鑷備箣鍔涖€?/p>

1銆丼pring妗嗘灦涓湁鍝簺涓嶅悓绫诲瀷鐨勪簨浠讹紵

Spring 鎻愪緵浜嗕互涓?涓爣鍑嗙殑浜嬩欢锛?/p>

  • 涓婁笅鏂囨洿鏂颁簨浠讹紙ContextRefreshedEvent锛夛細璇ヤ簨浠朵細鍦ˋpplicationContext琚垵濮嬪寲鎴栬€呮洿鏂版椂鍙戝竷銆備篃鍙互鍦ㄨ皟鐢–onfigurableApplicationContext 鎺ュ彛涓殑refresh()鏂规硶鏃惰瑙﹀彂銆?/li>
  • 涓婁笅鏂囧紑濮嬩簨浠讹紙ContextStartedEvent锛夛細褰撳鍣ㄨ皟鐢–onfigurableApplicationContext鐨凷tart()鏂规硶寮€濮?閲嶆柊寮€濮嬪鍣ㄦ椂瑙﹀彂璇ヤ簨浠躲€?/li>
  • 涓婁笅鏂囧仠姝簨浠讹紙ContextStoppedEvent锛夛細褰撳鍣ㄨ皟鐢–onfigurableApplicationContext鐨凷top()鏂规硶鍋滄瀹瑰櫒鏃惰Е鍙戣浜嬩欢銆?/li>
  • 涓婁笅鏂囧叧闂簨浠讹紙ContextClosedEvent锛夛細褰揂pplicationContext琚叧闂椂瑙﹀彂璇ヤ簨浠躲€傚鍣ㄨ鍏抽棴鏃讹紝鍏剁鐞嗙殑鎵€鏈夊崟渚婤ean閮借閿€姣併€?/li>
  • 璇锋眰澶勭悊浜嬩欢锛圧equestHandledEvent锛夛細鍦╓eb搴旂敤涓紝褰撲竴涓猦ttp璇锋眰锛坮equest锛夌粨鏉熻Е鍙戣浜嬩欢銆?/li>

2銆丼pring 妗嗘灦涓兘鐢ㄥ埌浜嗗摢浜涜璁℃ā鍨嬶紵

Spring妗嗘灦涓娇鐢ㄥ埌浜嗗ぇ閲忕殑璁捐妯″紡锛屼笅闈㈠垪涓句簡姣旇緝鏈変唬琛ㄦ€х殑锛?/p>

  • 浠g悊妯″紡—鍦ˋOP鍜宺emoting涓鐢ㄧ殑姣旇緝澶氥€?/li>
  • 鍗曚緥妯″紡—鍦╯pring閰嶇疆鏂囦欢涓畾涔夌殑bean榛樿涓哄崟渚嬫ā寮忋€?/li>
  • 妯℃澘鏂规硶—鐢ㄦ潵瑙e喅浠g爜閲嶅鐨勯棶棰樸€?/li>
  • 鍓嶇鎺у埗鍣?mdash;Srping鎻愪緵浜咲ispatcherServlet鏉ュ璇锋眰杩涜鍒嗗彂銆?/li>
  • 瑙嗗浘甯姪(View Helper )—Spring鎻愪緵浜嗕竴绯诲垪鐨凧SP鏍囩锛岄珮鏁堝畯鏉ヨ緟鍔╁皢鍒嗘暎鐨勪唬鐮佹暣鍚堝湪瑙嗗浘閲屻€?/li>
  • 渚濊禆娉ㄥ叆—璐┛浜嶣eanFactory / ApplicationContext鎺ュ彛鐨勬牳蹇冪悊蹇点€?/li>
  • 宸ュ巶妯″紡—BeanFactory鐢ㄦ潵鍒涘缓瀵硅薄鐨勫疄渚嬨€?/li>

3銆佷娇鐢⊿pring妗嗘灦鐨勫ソ澶勬槸浠€涔堬紵

  1. 杞婚噺锛歋pring 鏄交閲忕殑锛屽熀鏈殑鐗堟湰澶х害2MB銆?/li>
  2. 鎺у埗鍙嶈浆锛歋pring閫氳繃鎺у埗鍙嶈浆瀹炵幇浜嗘澗鏁h€﹀悎锛屽璞′滑缁欏嚭瀹冧滑鐨勪緷璧栵紝鑰屼笉鏄垱寤烘垨鏌ユ壘渚濊禆鐨勫璞′滑銆?/li>
  3. 闈㈠悜鍒囬潰鐨勭紪绋?AOP)锛歋pring鏀寔闈㈠悜鍒囬潰鐨勭紪绋嬶紝骞朵笖鎶婂簲鐢ㄤ笟鍔¢€昏緫鍜岀郴缁熸湇鍔″垎寮€銆?/li>
  4. 瀹瑰櫒锛歋pring 鍖呭惈骞剁鐞嗗簲鐢ㄤ腑瀵硅薄鐨勭敓鍛藉懆鏈熷拰閰嶇疆銆?/li>
  5. MVC妗嗘灦锛歋pring鐨刉EB妗嗘灦鏄釜绮惧績璁捐鐨勬鏋讹紝鏄疻eb妗嗘灦鐨勪竴涓緢濂界殑鏇夸唬鍝併€?/li>
  6. 浜嬪姟绠$悊锛歋pring 鎻愪緵涓€涓寔缁殑浜嬪姟绠$悊鎺ュ彛锛屽彲浠ユ墿灞曞埌涓婅嚦鏈湴浜嬪姟涓嬭嚦鍏ㄥ眬浜嬪姟锛圝TA锛夈€?/li>
  7. 寮傚父澶勭悊锛歋pring 鎻愪緵鏂逛究鐨凙PI鎶婂叿浣撴妧鏈浉鍏崇殑寮傚父锛堟瘮濡傜敱JDBC锛孒ibernate or JDO鎶涘嚭鐨勶級杞寲涓轰竴鑷寸殑unchecked 寮傚父銆?/li>

4銆佽В閲奡pring鏀寔鐨勫嚑绉峛ean鐨勪綔鐢ㄥ煙锛?/p>

Spring妗嗘灦鏀寔浠ヤ笅浜旂bean鐨勪綔鐢ㄥ煙锛?/p>

  1. singleton : bean鍦ㄦ瘡涓猄pring ioc 瀹瑰櫒涓彧鏈変竴涓疄渚嬨€?/li>
  2. prototype锛氫竴涓猙ean鐨勫畾涔夊彲浠ユ湁澶氫釜瀹炰緥銆?/li>
  3. request锛氭瘡娆ttp璇锋眰閮戒細鍒涘缓涓€涓猙ean锛岃浣滅敤鍩熶粎鍦ㄥ熀浜巜eb鐨凷pring ApplicationContext鎯呭舰涓嬫湁鏁堛€?/li>
  4. session锛氬湪涓€涓狧TTP Session涓紝涓€涓猙ean瀹氫箟瀵瑰簲涓€涓疄渚嬨€傝浣滅敤鍩熶粎鍦ㄥ熀浜巜eb鐨凷pring ApplicationContext鎯呭舰涓嬫湁鏁堛€?/li>
  5. global-session锛氬湪涓€涓叏灞€鐨凥TTP Session涓紝涓€涓猙ean瀹氫箟瀵瑰簲涓€涓疄渚嬨€傝浣滅敤鍩熶粎鍦ㄥ熀浜巜eb鐨凷pring ApplicationContext鎯呭舰涓嬫湁鏁堛€?/li>

5銆佸湪 Spring涓浣曟敞鍏ヤ竴涓猨ava闆嗗悎锛?/p>

Spring鎻愪緵浠ヤ笅鍑犵闆嗗悎鐨勯厤缃厓绱狅細

 

  • <list>绫诲瀷鐢ㄤ簬娉ㄥ叆涓€鍒楀€硷紝鍏佽鏈夌浉鍚岀殑鍊笺€?/li>
  • <set> 绫诲瀷鐢ㄤ簬娉ㄥ叆涓€缁勫€硷紝涓嶅厑璁告湁鐩稿悓鐨勫€笺€?/li>
  • <map> 绫诲瀷鐢ㄤ簬娉ㄥ叆涓€缁勯敭鍊煎锛岄敭鍜屽€奸兘鍙互涓轰换鎰忕被鍨嬨€?/li>
  • <props>绫诲瀷鐢ㄤ簬娉ㄥ叆涓€缁勯敭鍊煎锛岄敭鍜屽€奸兘鍙兘涓篠tring绫诲瀷銆?/li>

 

6.鑱婁竴鑱?Spring 涓殑绾跨▼瀹夊叏鎬?/p>

ImportNew. 2018-10-23 12:00
 
鏉ユ簮锛歋ylvanasSun 锛?/div>
juejin.im/post/5a0045ef5188254de169968e
Spring涓庣嚎绋嬪畨鍏?/div>
Spring浣滀负涓€涓狪OC/DI瀹瑰櫒锛屽府鍔╂垜浠鐞嗕簡璁歌澶氬鐨?ldquo;bean”銆備絾鍏跺疄锛孲pring骞舵病鏈変繚璇佽繖浜涘璞$殑绾跨▼瀹夊叏锛岄渶瑕佺敱寮€鍙戣€呰嚜宸辩紪鍐欒В鍐崇嚎绋嬪畨鍏ㄩ棶棰樼殑浠g爜銆?/div>
Spring瀵规瘡涓猙ean鎻愪緵浜嗕竴涓猻cope灞炴€ф潵琛ㄧず璇ean鐨勪綔鐢ㄥ煙銆傚畠鏄痓ean鐨勭敓鍛藉懆鏈熴€備緥濡傦紝涓€涓猻cope涓簊ingleton鐨刡ean锛屽湪绗竴娆¤娉ㄥ叆鏃讹紝浼氬垱寤轰负涓€涓崟渚嬪璞★紝璇ュ璞′細涓€鐩磋澶嶇敤鍒板簲鐢ㄧ粨鏉熴€?/div>
鎴戜滑浜ょ敱Spring绠$悊鐨勫ぇ澶氭暟瀵硅薄鍏跺疄閮芥槸涓€浜涙棤鐘舵€佺殑瀵硅薄锛岃繖绉嶄笉浼氬洜涓哄绾跨▼鑰屽鑷寸姸鎬佽鐮村潖鐨勫璞″緢閫傚悎Spring鐨勯粯璁cope锛屾瘡涓崟渚嬬殑鏃犵姸鎬佸璞¢兘鏄嚎绋嬪畨鍏ㄧ殑锛堜篃鍙互璇村彧瑕佹槸鏃犵姸鎬佺殑瀵硅薄锛屼笉绠″崟渚嬪渚嬮兘鏄嚎绋嬪畨鍏ㄧ殑锛屼笉杩囧崟渚嬫瘯绔熻妭鐪佷簡涓嶆柇鍒涘缓瀵硅薄涓嶨C鐨勫紑閿€锛夈€?/div>
鏃犵姸鎬佺殑瀵硅薄鍗虫槸鑷韩娌℃湁鐘舵€佺殑瀵硅薄锛岃嚜鐒朵篃灏变笉浼氬洜涓哄涓嚎绋嬬殑浜ゆ浛璋冨害鑰岀牬鍧忚嚜韬姸鎬佸鑷寸嚎绋嬪畨鍏ㄩ棶棰樸€傛棤鐘舵€佸璞″寘鎷垜浠粡甯镐娇鐢ㄧ殑DO銆丏TO銆乂O杩欎簺鍙綔涓烘暟鎹殑瀹炰綋妯″瀷鐨勮传琛€瀵硅薄锛岃繕鏈塖ervice銆丏AO鍜孋ontroller锛岃繖浜涘璞″苟娌℃湁鑷繁鐨勭姸鎬侊紝瀹冧滑鍙槸鐢ㄦ潵鎵ц鏌愪簺鎿嶄綔鐨勩€備緥濡傦紝姣忎釜DAO鎻愪緵鐨勫嚱鏁伴兘鍙槸瀵规暟鎹簱鐨凜RUD锛岃€屼笖姣忎釜鏁版嵁搴揅onnection閮戒綔涓哄嚱鏁扮殑灞€閮ㄥ彉閲忥紙灞€閮ㄥ彉閲忔槸鍦ㄧ敤鎴锋爤涓殑锛岃€屼笖鐢ㄦ埛鏍堟湰韬氨鏄嚎绋嬬鏈夌殑鍐呭瓨鍖哄煙锛屾墍浠ヤ笉瀛樺湪绾跨▼瀹夊叏闂锛夛紝鐢ㄥ畬鍗冲叧锛堟垨浜よ繕缁欒繛鎺ユ睜锛夈€?/div>
鏈変汉鍙兘浼氳涓猴紝鎴戜娇鐢╮equest浣滅敤鍩熶笉灏卞彲浠ラ伩鍏嶆瘡涓姹備箣闂寸殑瀹夊叏闂浜嗗悧锛熻繖鏄畬鍏ㄩ敊璇殑锛屽洜涓篊ontroller榛樿鏄崟渚嬬殑锛屼竴涓猚ontroller瀵硅薄鏄細琚涓嚎绋嬪叡浜殑锛岃繖灏卞張鍥炲埌浜嗙嚎绋嬬殑瀹夊叏闂銆傚綋鐒讹紝浣犱篃鍙互鎶奀ontroller鐨剆cope鏀规垚prototype锛屽疄闄呬笂Struts2灏辨槸杩欎箞鍋氱殑锛屼絾鏈変竴鐐硅娉ㄦ剰锛孲pring MVC瀵硅姹傜殑鎷︽埅绮掑害鏄熀浜庢瘡涓柟娉曠殑锛岃€孲truts2鏄熀浜庢瘡涓被鐨勶紝鎵€浠ユ妸Controller璁句负澶氫緥灏嗕細棰戠箒鐨勫垱寤轰笌鍥炴敹瀵硅薄锛屼弗閲嶅奖鍝嶅埌浜嗘€ц兘銆?/div>
閫氳繃闃呰涓婃枃鍏跺疄宸茬粡璇寸殑寰堟竻妤氫簡锛孲pring鏍规湰灏辨病鏈夊bean鐨勫绾跨▼瀹夊叏闂鍋氬嚭浠讳綍淇濊瘉涓庢帾鏂姐€傚浜庢瘡涓猙ean鐨勭嚎绋嬪畨鍏ㄩ棶棰橈紝鏍规湰鍘熷洜鏄瘡涓猙ean鑷韩鐨勮璁°€備笉瑕佸湪bean涓0鏄庝换浣曟湁鐘舵€佺殑瀹炰緥鍙橀噺鎴栫被鍙橀噺锛屽鏋滃繀椤诲姝わ紝閭d箞灏变娇鐢═hreadLocal鎶婂彉閲忓彉涓虹嚎绋嬬鏈夌殑锛屽鏋渂ean鐨勫疄渚嬪彉閲忔垨绫诲彉閲忛渶瑕佸湪澶氫釜绾跨▼涔嬮棿鍏变韩锛岄偅涔堝氨鍙兘浣跨敤synchronized銆乴ock銆丆AS绛夎繖浜涘疄鐜扮嚎绋嬪悓姝ョ殑鏂规硶浜嗐€?/div>
涓嬮潰灏嗛€氳繃瑙f瀽ThreadLocal鐨勬簮鐮佹潵浜嗚В瀹冪殑瀹炵幇涓庝綔鐢紝ThreadLocal鏄竴涓緢濂界敤鐨勫伐鍏风被锛屽畠鍦ㄦ煇浜涙儏鍐典笅瑙e喅浜嗙嚎绋嬪畨鍏ㄩ棶棰橈紙鍦ㄥ彉閲忎笉闇€瑕佽澶氫釜绾跨▼鍏变韩鏃讹級銆?/div>
ThreadLocal
ThreadLocal鏄竴涓负绾跨▼鎻愪緵绾跨▼灞€閮ㄥ彉閲忕殑宸ュ叿绫汇€傚畠鐨勬€濇兂涔熷崄鍒嗙畝鍗曪紝灏辨槸涓虹嚎绋嬫彁渚涗竴涓嚎绋嬬鏈夌殑鍙橀噺鍓湰锛岃繖鏍峰涓嚎绋嬮兘鍙互闅忔剰鏇存敼鑷繁绾跨▼灞€閮ㄧ殑鍙橀噺锛屼笉浼氬奖鍝嶅埌鍏朵粬绾跨▼銆備笉杩囬渶瑕佹敞鎰忕殑鏄紝ThreadLocal鎻愪緵鐨勫彧鏄竴涓祬鎷疯礉锛屽鏋滃彉閲忔槸涓€涓紩鐢ㄧ被鍨嬶紝閭d箞灏辫鑰冭檻瀹冨唴閮ㄧ殑鐘舵€佹槸鍚︿細琚敼鍙橈紝鎯宠瑙e喅杩欎釜闂鍙互閫氳繃閲嶅啓ThreadLocal鐨刬nitialValue()鍑芥暟鏉ヨ嚜宸卞疄鐜版繁鎷疯礉锛屽缓璁湪浣跨敤ThreadLocal鏃朵竴寮€濮嬪氨閲嶅啓璇ュ嚱鏁般€?/div>
ThreadLocal涓庡儚synchronized杩欐牱鐨勯攣鏈哄埗鏄笉鍚岀殑銆傞鍏堬紝瀹冧滑鐨勫簲鐢ㄥ満鏅笌瀹炵幇鎬濊矾灏变笉涓€鏍凤紝閿佹洿寮鸿皟鐨勬槸濡備綍鍚屾澶氫釜绾跨▼鍘绘纭湴鍏变韩涓€涓彉閲忥紝ThreadLocal鍒欐槸涓轰簡瑙e喅鍚屼竴涓彉閲忓浣曚笉琚涓嚎绋嬪叡浜€備粠鎬ц兘寮€閿€鐨勮搴︿笂鏉ヨ锛屽鏋滈攣鏈哄埗鏄敤鏃堕棿鎹㈢┖闂寸殑璇濓紝閭d箞ThreadLocal灏辨槸鐢ㄧ┖闂存崲鏃堕棿銆?/div>
ThreadLocal涓惈鏈変竴涓彨鍋歍hreadLocalMap鐨勫唴閮ㄧ被锛岃绫讳负涓€涓噰鐢ㄧ嚎鎬ф帰娴嬫硶瀹炵幇鐨凥ashMap銆傚畠鐨刱ey涓篢hreadLocal瀵硅薄鑰屼笖杩樹娇鐢ㄤ簡WeakReference锛孴hreadLocalMap姝f槸鐢ㄦ潵瀛樺偍鍙橀噺鍓湰鐨勩€?/div>
/**
* ThreadLocalMap is a customized hash map suitable only for
* maintaining thread local values. No operations are exported
* outside of the ThreadLocal class. The class is package private to
* allow declaration of fields in class Thread. To help deal with
* very large and long-lived usages, the hash table entries use
* WeakReferences for keys. However, since reference queues are not
* used, stale entries are guaranteed to be removed only when
* the table starts running out of space.
*/
static class ThreadLocalMap
/**
* The entries in this hash map extend WeakReference, using
* its main ref field as the key (which is always a
* ThreadLocal object). Note that null keys (i.e. entry.get()
* == null) mean that the key is no longer referenced, so the
* entry can be expunged from table. Such entries are referred to
* as "stale entries" in the code that follows.
*/
static class Entry extends WeakReference<ThreadLocal<?>>
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v)
super(k);
value = v;
....
ThreadLocal涓彧鍚湁涓変釜鎴愬憳鍙橀噺锛岃繖涓変釜鍙橀噺閮芥槸涓嶵hreadLocalMap鐨刪ash绛栫暐鐩稿叧鐨勩€?/div>
/**
* ThreadLocals rely on per-thread linear-probe hash maps attached
* to each thread (Thread.threadLocals and
* inheritableThreadLocals). The ThreadLocal objects act as keys,
* searched via threadLocalHashCode. This is a custom hash code
* (useful only within ThreadLocalMaps) that eliminates collisions
* in the common case where consecutively constructed ThreadLocals
* are used by the same threads, while remaining well-behaved in
* less common cases.
*/
private final int threadLocalHashCode = nextHashCode();
/**
* The next hash code to be given out. Updated atomically. Starts at
* zero.
*/
private static AtomicInteger nextHashCode =
new AtomicInteger();
/**
* The difference between successively generated hash codes - turns
* implicit sequential thread-local IDs into near-optimally spread
* multiplicative hash values for power-of-two-sized tables.
*/
private static final int HASH_INCREMENT = 0x61c88647;
/**
* Returns the next hash code.
*/
private static int nextHashCode()
return nextHashCode.getAndAdd(HASH_INCREMENT);
鍞竴鐨勫疄渚嬪彉閲弔hreadLocalHashCode鏄敤鏉ヨ繘琛屽鍧€鐨刪ashcode锛屽畠鐢卞嚱鏁皀extHashCode()鐢熸垚锛岃鍑芥暟绠€鍗曞湴閫氳繃涓€涓閲廐ASH_INCREMENT鏉ョ敓鎴恏ashcode銆傝嚦浜庝负浠€涔堣繖涓閲忎负0x61c88647锛屼富瑕佹槸鍥犱负ThreadLocalMap鐨勫垵濮嬪ぇ灏忎负16锛屾瘡娆℃墿瀹归兘浼氫负鍘熸潵鐨?鍊嶏紝杩欐牱瀹冪殑瀹归噺姘歌繙涓?鐨刵娆℃柟锛岃澧為噺閫変负0x61c88647涔熸槸涓轰簡灏藉彲鑳藉潎鍖€鍦板垎甯冿紝鍑忓皯纰版挒鍐茬獊銆?/div>
/**
* The initial capacity -- MUST be a power of two.
*/
private static final int INITIAL_CAPACITY = 16;
/**
* Construct a new map initially containing (firstKey, firstValue).
* ThreadLocalMaps are constructed lazily, so we only create
* one when we have at least one entry to put in it.
*/
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue)
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
瑕佽幏寰楀綋鍓嶇嚎绋嬬鏈夌殑鍙橀噺鍓湰闇€瑕佽皟鐢╣et()鍑芥暟銆傞鍏堬紝瀹冧細璋冪敤getMap()鍑芥暟鍘昏幏寰楀綋鍓嶇嚎绋嬬殑ThreadLocalMap锛岃繖涓嚱鏁伴渶瑕佹帴鏀跺綋鍓嶇嚎绋嬬殑瀹炰緥浣滀负鍙傛暟銆傚鏋滃緱鍒扮殑ThreadLocalMap涓簄ull锛岄偅涔堝氨鍘昏皟鐢╯etInitialValue()鍑芥暟鏉ヨ繘琛屽垵濮嬪寲锛屽鏋滀笉涓簄ull锛屽氨閫氳繃map鏉ヨ幏寰楀彉閲忓壇鏈苟杩斿洖銆?/div>
setInitialValue()鍑芥暟浼氬幓鍏堣皟鐢╥nitialValue()鍑芥暟鏉ョ敓鎴愬垵濮嬪€硷紝璇ュ嚱鏁伴粯璁よ繑鍥瀗ull锛屾垜浠彲浠ラ€氳繃閲嶅啓杩欎釜鍑芥暟鏉ヨ繑鍥炴垜浠兂瑕佸湪ThreadLocal涓淮鎶ょ殑鍙橀噺銆備箣鍚庯紝鍘昏皟鐢╣etMap()鍑芥暟鑾峰緱ThreadLocalMap锛屽鏋滆map宸茬粡瀛樺湪锛岄偅涔堝氨鐢ㄦ柊鑾峰緱value鍘昏鐩栨棫鍊硷紝鍚﹀垯灏辫皟鐢╟reateMap()鍑芥暟鏉ュ垱寤烘柊鐨刴ap銆?/div>
/**
* Returns the value in the current thread鈥榮 copy of this
* thread-local variable. If the variable has no value for the
* current thread, it is first initialized to the value returned
* by an invocation of the @link #initialValue method.
* @return the current thread鈥榮 value of this thread-local
*/
public T get()
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
return setInitialValue();
/**
* Variant of set() to establish initialValue. Used instead
* of set() in case user has overridden the set() method.
* @return the initial value
*/
private T setInitialValue()
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
protected T initialValue()
return null;
ThreadLocal鐨剆et()涓巖emove()鍑芥暟瑕佹瘮get()鐨勫疄鐜拌繕瑕佺畝鍗曪紝閮藉彧鏄€氳繃getMap()鏉ヨ幏寰桾hreadLocalMap鐒跺悗瀵瑰叾杩涜鎿嶄綔銆?/div>
/**
* Sets the current thread鈥榮 copy of this thread-local variable
* to the specified value. Most subclasses will have no need to
* override this method, relying solely on the @link #initialValue
* method to set the values of thread-locals.
* @param value the value to be stored in the current thread鈥榮 copy of
* this thread-local.
*/
public void set(T value)
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
/**
* Removes the current thread鈥榮 value for this thread-local
* variable. If this thread-local variable is subsequently
* @linkplain #get read by the current thread, its value will be
* reinitialized by invoking its @link #initialValue method,
* unless its value is @linkplain #set set by the current thread
* in the interim. This may result in multiple invocations of the
* @code initialValue method in the current thread.
* @since 1.5
*/
public void remove()
ThreadLocalMap m = getMap(Thread.currentThread());
if (m != null)
m.remove(this);
getMap()鍑芥暟涓巆reateMap()鍑芥暟鐨勫疄鐜颁篃鍗佸垎绠€鍗曪紝浣嗘槸閫氳繃瑙傚療杩欎袱涓嚱鏁板彲浠ュ彂鐜颁竴涓瀵嗭細ThreadLocalMap鏄瓨鏀惧湪Thread涓殑銆?/div>
/**
* Get the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
* @param t the current thread
* @return the map
*/
ThreadLocalMap getMap(Thread t)
return t.threadLocals;
/**
* Create the map associated with a ThreadLocal. Overridden in
* InheritableThreadLocal.
* @param t the current thread
* @param firstValue value for the initial entry of the map
*/
void createMap(Thread t, T firstValue)
t.threadLocals = new ThreadLocalMap(this, firstValue);
// Thread涓殑婧愮爜
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
/*
* InheritableThreadLocal values pertaining to this thread. This map is
* maintained by the InheritableThreadLocal class.
*/
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
浠旂粏鎯虫兂鍏跺疄灏辫兘澶熺悊瑙h繖绉嶈璁$殑鎬濇兂銆傛湁涓€绉嶆櫘閬嶇殑鏂规硶鏄€氳繃涓€涓叏灞€鐨勭嚎绋嬪畨鍏ㄧ殑Map鏉ュ瓨鍌ㄥ悇涓嚎绋嬬殑鍙橀噺鍓湰锛屼絾鏄繖绉嶅仛娉曞凡缁忓畬鍏ㄨ繚鑳屼簡ThreadLocal鐨勬湰鎰忥紝璁捐ThreadLocal鐨勫垵琛峰氨鏄负浜嗛伩鍏嶅涓嚎绋嬪幓骞跺彂璁块棶鍚屼竴涓璞★紝灏界瀹冩槸绾跨▼瀹夊叏鐨勩€傝€屽湪姣忎釜Thread涓瓨鏀句笌瀹冨叧鑱旂殑ThreadLocalMap鏄畬鍏ㄧ鍚圱hreadLocal鐨勬€濇兂鐨勶紝褰撴兂瑕佸绾跨▼灞€閮ㄥ彉閲忚繘琛屾搷浣滄椂锛屽彧闇€瑕佹妸Thread浣滀负key鏉ヨ幏寰桾hread涓殑ThreadLocalMap鍗冲彲銆傝繖绉嶈璁$浉姣旈噰鐢ㄤ竴涓叏灞€Map鐨勬柟娉曚細澶氬崰鐢ㄥ緢澶氬唴瀛樼┖闂达紝浣嗕篃鍥犳涓嶉渶瑕侀澶栫殑閲囧彇閿佺瓑绾跨▼鍚屾鏂规硶鑰岃妭鐪佷簡鏃堕棿涓婄殑娑堣€椼€?/div>
ThreadLocal涓殑鍐呭瓨娉勬紡
鎴戜滑瑕佽€冭檻涓€绉嶄細鍙戠敓鍐呭瓨娉勬紡鐨勬儏鍐碉紝濡傛灉ThreadLocal琚缃负null鍚庯紝鑰屼笖娌℃湁浠讳綍寮哄紩鐢ㄦ寚鍚戝畠锛屾牴鎹瀮鍦惧洖鏀剁殑鍙揪鎬у垎鏋愮畻娉曪紝ThreadLocal灏嗕細琚洖鏀躲€傝繖鏍蜂竴鏉ワ紝ThreadLocalMap涓氨浼氬惈鏈塳ey涓簄ull鐨凟ntry锛岃€屼笖ThreadLocalMap鏄湪Thread涓殑锛屽彧瑕佺嚎绋嬭繜杩熶笉缁撴潫锛岃繖浜涙棤娉曡闂埌鐨剉alue浼氬舰鎴愬唴瀛樻硠婕忋€備负浜嗚В鍐宠繖涓棶棰橈紝ThreadLocalMap涓殑getEntry()銆乻et()鍜宺emove()鍑芥暟閮戒細娓呯悊key涓簄ull鐨凟ntry锛屼互涓嬮潰鐨刧etEntry()鍑芥暟鐨勬簮鐮佷负渚嬨€?/div>
/**
* Get the entry associated with key. This method
* itself handles only the fast path: a direct hit of existing
* key. It otherwise relays to getEntryAfterMiss. This is
* designed to maximize performance for direct hits, in part
* by making this method readily inlinable.
* @param key the thread local object
* @return the entry associated with key, or null if no such
*/
private Entry getEntry(ThreadLocal<?> key)
int i = key.threadLocalHashCode & (table.length - 1);
Entry e = table[i];
if (e != null && e.get() == key)
return e;
else
return getEntryAfterMiss(key, i, e);
/**
* Version of getEntry method for use when key is not found in
* its direct hash slot.
* @param key the thread local object
* @param i the table index for key鈥榮 hash code
* @param e the entry at table[i]
* @return the entry associated with key, or null if no such
*/
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e)
Entry[] tab = table;
int len = tab.length;
// 娓呯悊key涓簄ull鐨凟ntry
while (e != null)
ThreadLocal<?> k = e.get();
if (k == key)
return e;
if (k == null)
expungeStaleEntry(i);
else
i = nextIndex(i, len);
e = tab[i];
return null;
鍦ㄤ笂鏂囦腑鎴戜滑鍙戠幇浜員hreadLocalMap鐨刱ey鏄竴涓急寮曠敤锛岄偅涔堜负浠€涔堜娇鐢ㄥ急寮曠敤鍛紵浣跨敤寮哄紩鐢╧ey涓庡急寮曠敤key鐨勫樊鍒涓嬶細
浣嗚娉ㄦ剰鐨勬槸锛孴hreadLocalMap浠呬粎鍚湁杩欎簺琚姩鎺柦鏉ヨˉ鏁戝唴瀛樻硠婕忛棶棰樸€傚鏋滀綘鍦ㄤ箣鍚庢病鏈夎皟鐢═hreadLocalMap鐨剆et()銆乬etEntry()鍜宺emove()鍑芥暟鐨勮瘽锛岄偅涔堜粛鐒朵細瀛樺湪鍐呭瓨娉勬紡闂銆?/div>
鍦ㄤ娇鐢ㄧ嚎绋嬫睜鐨勬儏鍐典笅锛屽鏋滀笉鍙婃椂杩涜娓呯悊锛屽唴瀛樻硠婕忛棶棰樹簨灏忥紝鐢氳嚦杩樹細浜х敓绋嬪簭閫昏緫涓婄殑闂銆傛墍浠ワ紝涓轰簡瀹夊叏鍦颁娇鐢═hreadLocal锛屽繀椤昏鍍忔瘡娆′娇鐢ㄥ畬閿佸氨瑙i攣涓€鏍凤紝鍦ㄦ瘡娆′娇鐢ㄥ畬ThreadLocal鍚庨兘瑕佽皟鐢╮emove()鏉ユ竻鐞嗘棤鐢ㄧ殑Entry
 

spring鍜宻pringMVC鐨勯潰璇曢棶棰樻€荤粨

鐗堟潈澹版槑锛氭湰鏂囦负鍗氫富鍘熷垱鏂囩珷锛岄伒寰?a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"> CC 4.0 BY-SA 鐗堟潈鍗忚锛岃浆杞借闄勪笂鍘熸枃鍑哄閾炬帴鍜屾湰澹版槑銆?/span>
鏈枃閾炬帴锛?a href="https://blog.csdn.net/qq_39470733/article/details/77709607">https://blog.csdn.net/qq_39470733/article/details/77709607

1.Spring涓瑼OP鐨勫簲鐢ㄥ満鏅€丄op鍘熺悊銆佸ソ澶勶紵

绛旓細AOP--Aspect Oriented Programming闈㈠悜鍒囬潰缂栫▼锛涚敤鏉ュ皝瑁呮í鍒囧叧娉ㄧ偣锛屽叿浣撳彲浠ュ湪涓嬮潰鐨勫満鏅腑浣跨敤:

Authentication 鏉冮檺銆丆aching 缂撳瓨銆丆ontext passing 鍐呭浼犻€掋€丒rror handling 閿欒澶勭悊Lazy loading鎳掑姞杞姐€丏ebugging璋冭瘯銆乴ogging, tracing, profiling and monitoring 璁板綍璺熻釜浼樺寲銆€鏍″噯銆丳erformance optimization銆€鎬ц兘浼樺寲銆丳ersistence 鎸佷箙鍖栥€丷esource pooling銆€璧勬簮姹犮€丼ynchronization銆€鍚屾銆乀ransactions 浜嬪姟

鍘熺悊锛欰OP鏄潰鍚戝垏闈㈢紪绋嬶紝鏄€氳繃鍔ㄦ€佷唬鐞嗙殑鏂瑰紡涓虹▼搴忔坊鍔犵粺涓€鍔熻兘锛岄泦涓В鍐充竴浜涘叕鍏遍棶棰樸€?/p>

浼樼偣锛?.鍚勪釜姝ラ涔嬮棿鐨勮壇濂介殧绂绘€ц€﹀悎鎬уぇ澶ч檷浣?nbsp;
           2.婧愪唬鐮佹棤鍏虫€э紝鍐嶆墿灞曞姛鑳界殑鍚屾椂涓嶅婧愮爜杩涜淇敼鎿嶄綔 

2.Spring涓璉OC鐨勪綔鐢ㄤ笌鍘熺悊锛熷璞″垱寤虹殑杩囩▼銆?/p>

绛旓細IOC--Inversion of Control鎺у埗鍙嶈浆銆傚綋鏌愪釜瑙掕壊闇€瑕佸彟澶栦竴涓鑹插崗鍔╃殑鏃跺€欙紝鍦ㄤ紶缁熺殑绋嬪簭璁捐杩囩▼涓紝閫氬父鐢辫皟鐢ㄨ€呮潵鍒涘缓琚皟鐢ㄨ€呯殑瀹炰緥瀵硅薄銆備絾鍦╯pring涓垱寤鸿璋冪敤鑰呯殑宸ヤ綔涓嶅啀鐢辫皟鐢ㄨ€呮潵瀹屾垚锛屽洜姝ょО涓烘帶鍒跺弽杞€傚垱寤鸿璋冪敤鑰呯殑宸ヤ綔鐢眘pring鏉ュ畬鎴愶紝鐒跺悗娉ㄥ叆璋冪敤鑰?nbsp;鐩存帴浣跨敤銆?/p>

3.浠嬬粛spring妗嗘灦

   瀹冩槸涓€涓竴绔欏紡锛坒ull-stack鍏ㄦ爤寮忥級妗嗘灦锛屾彁渚涗簡浠庤〃鐜板眰-springMVC鍒颁笟鍔″眰-spring鍐嶅埌鎸佷箙灞?springdata鐨勪竴濂楀畬鏁寸殑瑙e喅鏂规銆傛垜浠湪椤圭洰涓彲浠ュ彧浣跨敤spring涓€涓鏋讹紝瀹冨氨鍙互鎻愪緵琛ㄧ幇灞傜殑mvc妗嗘灦锛屾寔涔呭眰鐨凞ao妗嗘灦銆傚畠鐨勪袱澶ф牳蹇僆oC鍜孉OP鏇存槸涓烘垜浠▼搴忚В鑰﹀拰浠g爜绠€娲佹槗缁存姢鎻愪緵浜嗘敮鎸併€?/p>

4.Spring甯歌鍒涘缓瀵硅薄鐨勬敞瑙o紵

绛旓細@Component@Controller@ Service@ Repository

5.Spring涓敤鍒扮殑璁捐妯″紡

绛旓細绠€鍗曞伐鍘傘€佸伐鍘傛柟娉曘€佸崟渚嬫ā寮忋€侀€傞厤鍣ㄣ€佸寘瑁呭櫒銆佷唬鐞嗐€佽瀵熻€呫€佺瓥鐣ャ€佹ā鏉挎柟娉?/p>

璇︾粏浠嬬粛:璇峰弬鑰冩湰寰崥鐨勶細寮€鍙戝父鐢ㄨ璁℃ā寮?/p>

6.Spring鐨勪紭鐐癸紵

绛旓細1.闄嶄綆浜嗙粍浠朵箣闂寸殑鑰﹀悎鎬?锛屽疄鐜颁簡杞欢鍚勫眰涔嬮棿鐨勮В鑰?nbsp;
2.鍙互浣跨敤瀹规槗鎻愪緵鐨勪紬澶氭湇鍔★紝濡備簨鍔$鐞嗭紝娑堟伅鏈嶅姟绛?nbsp;
3.瀹瑰櫒鎻愪緵鍗曚緥妯″紡鏀寔 
4.瀹瑰櫒鎻愪緵浜咥OP鎶€鏈紝鍒╃敤瀹冨緢瀹规槗瀹炵幇濡傛潈闄愭嫤鎴紝杩愯鏈熺洃鎺х瓑鍔熻兘 
5.瀹瑰櫒鎻愪緵浜嗕紬澶氱殑杈呭姪绫伙紝鑳藉姞蹇簲鐢ㄧ殑寮€鍙?nbsp;
6.spring瀵逛簬涓绘祦鐨勫簲鐢ㄦ鏋舵彁渚涗簡闆嗘垚鏀寔锛屽hibernate锛孞PA锛孲truts绛?nbsp;
7.spring灞炰簬浣庝镜鍏ュ紡璁捐锛屼唬鐮佺殑姹℃煋鏋佷綆 
8.鐙珛浜庡悇绉嶅簲鐢ㄦ湇鍔″櫒 
9.spring鐨凞I鏈哄埗闄嶄綆浜嗕笟鍔″璞℃浛鎹㈢殑澶嶆潅鎬?nbsp;
10.Spring鐨勯珮搴﹀紑鏀炬€э紝骞朵笉寮哄埗搴旂敤瀹屽叏渚濊禆浜嶴pring锛屽紑鍙戣€呭彲浠ヨ嚜鐢遍€夋嫨spring 鐨勯儴鍒嗘垨鍏ㄩ儴 

7.Spring Bean鐨勪綔鐢ㄥ煙涔嬮棿鏈変粈涔堝尯鍒紵

Spring瀹瑰櫒涓殑bean鍙互鍒嗕负5涓寖鍥淬€傛墍鏈夎寖鍥寸殑鍚嶇О閮芥槸鑷鏄庣殑锛屼絾鏄负浜嗛伩鍏嶆贩娣嗭紝杩樻槸璁╂垜浠潵瑙i噴涓€涓嬶細

singleton锛氳繖绉峛ean鑼冨洿鏄粯璁ょ殑锛岃繖绉嶈寖鍥寸‘淇濅笉绠℃帴鍙楀埌澶氬皯涓姹傦紝姣忎釜瀹瑰櫒涓彧鏈変竴涓猙ean鐨勫疄渚嬶紝鍗曚緥鐨勬ā寮忕敱bean factory鑷韩鏉ョ淮鎶ゃ€?/p>

prototype锛氬師褰㈣寖鍥翠笌鍗曚緥鑼冨洿鐩稿弽锛屼负姣忎竴涓猙ean璇锋眰鎻愪緵涓€涓疄渚嬨€?/p>

request锛氬湪璇锋眰bean鑼冨洿鍐呬細姣忎竴涓潵鑷鎴风鐨勭綉缁滆姹傚垱寤轰竴涓疄渚嬶紝鍦ㄨ姹傚畬鎴愪互鍚庯紝bean浼氬け鏁堝苟琚瀮鍦惧洖鏀跺櫒鍥炴敹銆?/p>

Session锛氫笌璇锋眰鑼冨洿绫讳技锛岀‘淇濇瘡涓猻ession涓湁涓€涓猙ean鐨勫疄渚嬶紝鍦╯ession杩囨湡鍚庯紝bean浼氶殢涔嬪け鏁堛€?/p>

global-session锛歡lobal-session鍜孭ortlet搴旂敤鐩稿叧銆傚綋浣犵殑搴旂敤閮ㄧ讲鍦≒ortlet瀹瑰櫒涓伐浣滄椂锛屽畠鍖呭惈寰堝portlet銆傚鏋滀綘鎯宠澹版槑璁╂墍鏈夌殑portlet鍏辩敤鍏ㄥ眬鐨勫瓨鍌ㄥ彉閲忕殑璇濓紝閭d箞杩欏叏灞€鍙橀噺闇€瑕佸瓨鍌ㄥ湪global-session涓€?/p>

鍏ㄥ眬浣滅敤鍩熶笌Servlet涓殑session浣滅敤鍩熸晥鏋滅浉鍚屻€?/p>

8.Spring绠$悊浜嬪姟鏈夊嚑绉嶆柟寮忥紵

绛旓細鏈変袱绉嶆柟寮忥細

1銆佺紪绋嬪紡浜嬪姟锛屽湪浠g爜涓‖缂栫爜銆?涓嶆帹鑽愪娇鐢?

2銆佸0鏄庡紡浜嬪姟锛屽湪閰嶇疆鏂囦欢涓厤缃紙鎺ㄨ崘浣跨敤锛?/p>

澹版槑寮忎簨鍔″張鍒嗕负涓ょ锛?/p>

a銆佸熀浜嶺ML鐨勫0鏄庡紡浜嬪姟

b銆佸熀浜庢敞瑙g殑澹版槑寮忎簨鍔?/p>

9.spring涓嚜鍔ㄨ閰嶇殑鏂瑰紡鏈夊摢浜涳紵

绛旓細1銆?No锛氬嵆涓嶅惎鐢ㄨ嚜鍔ㄨ閰嶃€?/p>

2銆?byName锛氶€氳繃灞炴€х殑鍚嶅瓧鐨勬柟寮忔煡鎵綣avaBean渚濊禆鐨勫璞″苟涓哄叾娉ㄥ叆銆傛瘮濡傝绫籆omputer鏈変釜灞炴€rinter锛屾寚瀹氬叾autowire灞炴€т负byName鍚庯紝Spring IoC瀹瑰櫒浼氬湪閰嶇疆鏂囦欢涓煡鎵緄d/name灞炴€т负printer鐨刡ean锛岀劧鍚庝娇鐢⊿eter鏂规硶涓哄叾娉ㄥ叆銆?/p>

3銆?byType锛氶€氳繃灞炴€х殑绫诲瀷鏌ユ壘JavaBean渚濊禆鐨勫璞″苟涓哄叾娉ㄥ叆銆傛瘮濡傜被Computer鏈変釜灞炴€rinter锛岀被鍨嬩负Printer锛岄偅涔堬紝鎸囧畾鍏禷utowire灞炴€т负byType鍚庯紝Spring IoC瀹瑰櫒浼氭煡鎵綜lass灞炴€т负Printer鐨刡ean锛屼娇鐢⊿eter鏂规硶涓哄叾娉ㄥ叆銆?/p>

4銆?constructor锛氶€歜yType涓€鏍凤紝涔熸槸閫氳繃绫诲瀷鏌ユ壘渚濊禆瀵硅薄銆備笌byType鐨勫尯鍒湪浜庡畠涓嶆槸浣跨敤Seter鏂规硶娉ㄥ叆锛岃€屾槸浣跨敤鏋勯€犲瓙娉ㄥ叆銆?/p>

5銆?autodetect锛氬湪byType鍜宑onstructor涔嬮棿鑷姩鐨勯€夋嫨娉ㄥ叆鏂瑰紡銆?/p>

6銆?default锛氱敱涓婄骇鏍囩<beans>鐨刣efault-autowire灞炴€х‘瀹氥€?/p>

10.spring涓殑鏍稿績绫绘湁閭d簺锛屽悇鏈変粈涔堜綔鐢紵

绛旓細BeanFactory锛氫骇鐢熶竴涓柊鐨勫疄渚嬶紝鍙互瀹炵幇鍗曚緥妯″紡

BeanWrapper锛氭彁渚涚粺涓€鐨刧et鍙妔et鏂规硶

ApplicationContext:鎻愪緵妗嗘灦鐨勫疄鐜帮紝鍖呮嫭BeanFactory鐨勬墍鏈夊姛鑳?/p>

11.Bean鐨勮皟鐢ㄦ柟寮忔湁鍝簺锛?/p>

绛旓細鏈変笁绉嶆柟寮忓彲浠ュ緱鍒癇ean骞惰繘琛岃皟鐢細
1銆佷娇鐢˙eanWrapper
HelloWorld hw=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(hw);
bw.setPropertyvalue(”msg”,”HelloWorld”);
system.out.println(bw.getPropertyCalue(”msg”));
2銆佷娇鐢˙eanFactory
InputStream is=new FileInputStream(”config.xml”);
XmlBeanFactory factory=new XmlBeanFactory(is);
HelloWorld hw=(HelloWorld) factory.getBean(”HelloWorld”);
system.out.println(hw.getMsg());
3銆佷娇鐢ˋpplicationConttext
ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”);
HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”);
System.out.println(hw.getMsg());

12.浠€涔堟槸IOC锛屼粈涔堝張鏄疍I锛屼粬浠湁浠€涔堝尯鍒紵

绛旓細渚濊禆娉ㄥ叆DI鏄竴涓▼搴忚璁℃ā寮忓拰鏋舵瀯妯″瀷锛?涓€浜涙椂鍊欎篃绉颁綔鎺у埗鍙嶈浆锛屽敖绠″湪鎶€鏈笂鏉ヨ锛屼緷璧栨敞鍏ユ槸涓€涓狪OC鐨勭壒娈婂疄鐜帮紝渚濊禆娉ㄥ叆鏄寚涓€涓璞″簲鐢ㄥ彟澶栦竴涓璞℃潵鎻愪緵涓€涓壒娈婄殑鑳藉姏锛屼緥濡傦細鎶婁竴涓?鏁版嵁搴撹繛鎺ュ凡鍙傛暟鐨勫舰寮忎紶鍒颁竴涓璞$殑缁撴瀯鏂规硶閲岄潰鑰屼笉鏄湪閭d釜瀵硅薄鍐呴儴鑷鍒涘缓涓€涓繛鎺ャ€傛帶鍒跺弽杞拰渚濊禆娉ㄥ叆鐨勫熀鏈€濇兂灏辨槸鎶婄被鐨勪緷璧栦粠绫诲唴閮ㄨ浆鍖栧埌澶?閮ㄤ互鍑忓皯渚濊禆

搴旂敤鎺у埗鍙嶈浆锛屽璞″湪琚垱寤虹殑鏃跺€欙紝鐢变竴涓皟鎺х郴缁熷唴鎵€鏈夊璞$殑澶栫晫瀹炰綋锛屽皢鍏舵墍渚濊禆鐨勫璞$殑寮曠敤锛屼紶閫掔粰瀹冦€備篃鍙互璇达紝渚濊禆琚敞鍏ュ埌瀵硅薄涓€傛墍 浠ワ紝鎺у埗鍙嶈浆鏄紝鍏充簬涓€涓璞″浣曡幏鍙栦粬鎵€渚濊禆鐨勫璞$殑寮曠敤锛岃繖涓矗浠荤殑鍙嶈浆銆?/p>

13.spring鏈変袱绉嶄唬鐞嗘柟寮忥細

绛? 鑻ョ洰鏍囧璞″疄鐜颁簡鑻ュ共鎺ュ彛锛宻pring浣跨敤JDK鐨刯ava.lang.reflect.Proxy绫讳唬鐞嗐€?/p>

      浼樼偣锛氬洜涓烘湁鎺ュ彛锛屾墍浠ヤ娇绯荤粺鏇村姞鏉捐€﹀悎

      缂虹偣锛氫负姣忎竴涓洰鏍囩被鍒涘缓鎺ュ彛

鑻ョ洰鏍囧璞℃病鏈夊疄鐜颁换浣曟帴鍙o紝spring浣跨敤CGLIB搴撶敓鎴愮洰鏍囧璞$殑瀛愮被銆?/p>

      浼樼偣锛氬洜涓轰唬鐞嗙被涓庣洰鏍囩被鏄户鎵垮叧绯伙紝鎵€浠ヤ笉闇€瑕佹湁鎺ュ彛鐨勫瓨鍦ㄣ€?/p>

      缂虹偣锛氬洜涓烘病鏈変娇鐢ㄦ帴鍙o紝鎵€浠ョ郴缁熺殑鑰﹀悎鎬ф病鏈変娇鐢↗DK鐨勫姩鎬佷唬鐞嗗ソ銆?/p>

14.springMVC鐨勬祦绋嬶紵

绛旓細1.鐢ㄦ埛鍙戦€佽姹傝嚦鍓嶇鎺у埗鍣―ispatcherServlet

2.DispatcherServlet鏀跺埌璇锋眰璋冪敤HandlerMapping澶勭悊鍣ㄦ槧灏勫櫒銆?/p>

3.澶勭悊鍣ㄦ槧灏勫櫒鏍规嵁璇锋眰url鎵惧埌鍏蜂綋鐨勫鐞嗗櫒锛岀敓鎴愬鐞嗗櫒瀵硅薄鍙婂鐞嗗櫒鎷︽埅鍣?濡傛灉鏈夊垯鐢熸垚)涓€骞惰繑鍥炵粰DispatcherServlet銆?/p>

4.DispatcherServlet閫氳繃HandlerAdapter澶勭悊鍣ㄩ€傞厤鍣ㄨ皟鐢ㄥ鐞嗗櫒

5.鎵ц澶勭悊鍣?Controller锛屼篃鍙悗绔帶鍒跺櫒)銆?/p>

6.Controller鎵ц瀹屾垚杩斿洖ModelAndView

7.HandlerAdapter灏哻ontroller鎵ц缁撴灉ModelAndView杩斿洖缁橠ispatcherServlet

8.DispatcherServlet灏哅odelAndView浼犵粰ViewReslover瑙嗗浘瑙f瀽鍣?/p>

9.ViewReslover瑙f瀽鍚庤繑鍥炲叿浣揤iew

10.DispatcherServlet瀵筕iew杩涜娓叉煋瑙嗗浘锛堝嵆灏嗘ā鍨嬫暟鎹~鍏呰嚦瑙嗗浘涓級銆?/p>

11.DispatcherServlet鍝嶅簲鐢ㄦ埛

鎶€鏈浘鐗? class=

15.Springmvc鐨勪紭鐐?/p>

绛旓細1.瀹冩槸鍩轰簬缁勪欢鎶€鏈殑.鍏ㄩ儴鐨勫簲鐢ㄥ璞?鏃犺鎺у埗鍣ㄥ拰瑙嗗浘,杩樻槸涓氬姟瀵硅薄涔嬬被鐨勯兘鏄?java缁勪欢.骞朵笖鍜孲pring鎻愪緵鐨勫叾浠栧熀纭€缁撴瀯绱у瘑闆嗘垚.

2.涓嶄緷璧栦簬Servlet API(鐩爣铏芥槸濡傛,浣嗘槸鍦ㄥ疄鐜扮殑鏃跺€欑‘瀹炴槸渚濊禆浜嶴ervlet鐨?

3. 鍙互浠绘剰浣跨敤鍚勭瑙嗗浘鎶€鏈?鑰屼笉浠呬粎灞€闄愪簬JSP

4 . 鏀寔鍚勭璇锋眰璧勬簮鐨勬槧灏勭瓥鐣?/p>

5 .瀹冨簲鏄槗浜庢墿灞曠殑

以上是关于Spring缁忓吀楂橀闈㈣瘯棰橈紝鍘熸潵鏄暱杩欎釜鏍峰瓙的主要内容,如果未能解决你的问题,请参考以下文章

楂橀闈㈣瘯棰?--鎸佺画鏇存柊

69閬揝pring闈㈣瘯棰樺拰绛旀

Spring闈㈣瘯鑰冪偣

DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode