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缁忓吀楂橀闈㈣瘯棰橈紝鍘熸潵鏄暱杩欎釜鏍峰瓙
鏈枃閫夎嚜銆奡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>
1 浠€涔堟槸Spring妗嗘灦锛孲pring妗嗘灦鏈夊摢浜涗富瑕佹ā鍧?/p>
Spring妗嗘灦鏄竴涓负Java搴旂敤绋嬪簭寮€鍙戞彁渚涚患鍚堛€佸箍娉涚殑鍩虹鎬ф敮鎸佺殑Java骞冲彴銆係pring甯姪寮€鍙戣€呰В鍐充簡寮€鍙戜腑鍩虹鎬х殑闂锛屼娇寰楀紑鍙戜汉鍛樺彲浠ヤ笓娉ㄤ簬搴旂敤绋嬪簭鐨勫紑鍙戙€係pring妗嗘灦鏈韩涔熸槸鎸夌収璁捐妯″紡绮惧績鎵撻€犵殑锛岃繖浣垮緱鎴戜滑鍙互鍦ㄥ紑鍙戠幆澧冧腑瀹夊績鍦伴泦鎴怱pring妗嗘灦锛屼笉蹇呮媴蹇僑pring鏄浣曞湪鍚庡彴宸ヤ綔鐨勩€?/p>
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>
3 浠€涔堟槸鎺у埗鍙嶈浆锛圛oC锛夛紝浠€涔堟槸渚濊禆娉ㄥ叆
锛?锛夋帶鍒跺弽杞槸搴旂敤浜庤蒋浠跺伐绋嬮鍩熺殑锛屽湪杩愯鏃惰瑁呴厤鍣ㄥ璞$敤鏉ョ粦瀹氳€﹀悎瀵硅薄鐨勪竴绉嶇紪绋嬫妧宸э紝瀵硅薄涔嬮棿鐨勮€﹀悎鍏崇郴鍦ㄧ紪璇戞椂閫氬父鏄湭鐭ョ殑銆傚湪浼犵粺鐨勭紪绋嬫柟寮忎腑锛屼笟鍔¢€昏緫鐨勬祦绋嬫槸鐢卞簲鐢ㄧ▼搴忎腑鏃╁凡琚瀹氬ソ鍏宠仈鍏崇郴鐨勫璞℃潵鍐冲畾鐨勩€傚湪浣跨敤鎺у埗鍙嶈浆鐨勬儏鍐典笅锛屼笟鍔¢€昏緫鐨勬祦绋嬫槸鐢卞璞″叧绯诲浘鏉ュ喅瀹氱殑锛岃瀵硅薄鍏崇郴鍥剧敱瑁呴厤鍣ㄨ礋璐e疄渚嬪寲锛岃繖绉嶅疄鐜版柟寮忚繕鍙互灏嗗璞′箣闂寸殑鍏宠仈鍏崇郴鐨勫畾涔夋娊璞″寲銆傜粦瀹氱殑杩囩▼鏄€氳繃“渚濊禆娉ㄥ叆”瀹炵幇鐨勩€?/p>
锛?锛夋帶鍒跺弽杞槸涓€绉嶄互缁欎簣搴旂敤绋嬪簭涓洰鏍囩粍浠舵洿澶氭帶鍒朵负鐩殑璁捐鑼冨紡锛屽苟鍦ㄥ疄闄呭伐浣滀腑璧峰埌浜嗘湁鏁堢殑浣滅敤銆?/p>
锛?锛変緷璧栨敞鍏ユ槸鍦ㄧ紪璇戦樁娈靛皻鏈煡鎵€闇€鐨勫姛鑳芥槸鏉ヨ嚜鍝釜鐨勭被鐨勬儏鍐典笅锛屽皢鍏朵粬瀵硅薄鎵€渚濊禆鐨勫姛鑳藉璞″疄渚嬪寲鐨勬ā寮忋€傝繖灏遍渶瑕佷竴绉嶆満鍒舵潵婵€娲荤浉搴旂殑缁勪欢浠ユ彁渚涚壒瀹氱殑鍔熻兘锛屾墍浠ヤ緷璧栨敞鍏ユ槸鎺у埗鍙嶈浆鐨勫熀纭€銆傚惁鍒欏鏋滃湪缁勪欢涓嶅彈妗嗘灦鎺у埗鐨勬儏鍐典笅锛屾鏋跺張鎬庝箞鐭ラ亾瑕佸垱寤哄摢涓粍浠跺憿锛?/p>
4 鍦↗ava涓緷璧栨敞鍏ユ湁鍝簺鏂瑰紡
锛?锛夋瀯閫犲櫒娉ㄥ叆銆?/p>
锛?锛塖etter鏂规硶娉ㄥ叆銆?/p>
锛?锛夋帴鍙f敞鍏ャ€?/p>
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>
6 Spring鎻愪緵鍑犵閰嶇疆鏂瑰紡鏉ヨ缃厓鏁版嵁
Spring鎻愪緵浠ヤ笅涓夌閰嶇疆鏂瑰紡鏉ヨ缃厓鏁版嵁锛?/p>
锛?锛夊熀浜嶺ML鐨勯厤缃€?/p>
锛?锛夊熀浜庢敞瑙g殑閰嶇疆銆?/p>
锛?锛夊熀浜嶫ava鐨勯厤缃€?/p>
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>
8 Spring鎻愪緵鍝簺閰嶇疆褰㈠紡
Spring瀵笿ava閰嶇疆鐨勬敮鎸佹槸鐢盄Configuration娉ㄨВ鍜孈Bean娉ㄨВ鏉ュ疄鐜扮殑銆傜敱@Bean娉ㄨВ鐨勬柟娉曞皢浼氬疄渚嬪寲銆侀厤缃拰鍒濆鍖栦竴涓柊瀵硅薄锛岃繖涓璞″皢鐢盨pring鐨処oC瀹瑰櫒鏉ョ鐞嗐€侤Bean澹版槑鎵€璧峰埌鐨勪綔鐢ㄤ笌鍏冪礌绫讳技銆傝@Configuration鎵€娉ㄨВ鐨勭被鍒欒〃绀鸿繖涓被鐨勪富瑕佺洰鐨勬槸浣滀负Bean瀹氫箟鐨勮祫婧愩€傝@Configuration澹版槑鐨勭被鍙互閫氳繃鍦ㄥ悓涓€涓被鍐呴儴璋冪敤@bean鏂规硶鏉ヨ缃祵鍏ean鐨勪緷璧栧叧绯汇€?/p>
鏈€绠€鍗曠殑@Configuration 澹版槑绫昏鍙傝€冧笅闈㈢殑浠g爜锛???????
@Configuration
public 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>
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>
鏇村缁忓吀楂橀闈㈣瘯棰?/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妗嗘灦鐨勫ソ澶勬槸浠€涔堬紵
- 杞婚噺锛歋pring 鏄交閲忕殑锛屽熀鏈殑鐗堟湰澶х害2MB銆?/li>
- 鎺у埗鍙嶈浆锛歋pring閫氳繃鎺у埗鍙嶈浆瀹炵幇浜嗘澗鏁h€﹀悎锛屽璞′滑缁欏嚭瀹冧滑鐨勪緷璧栵紝鑰屼笉鏄垱寤烘垨鏌ユ壘渚濊禆鐨勫璞′滑銆?/li>
- 闈㈠悜鍒囬潰鐨勭紪绋?AOP)锛歋pring鏀寔闈㈠悜鍒囬潰鐨勭紪绋嬶紝骞朵笖鎶婂簲鐢ㄤ笟鍔¢€昏緫鍜岀郴缁熸湇鍔″垎寮€銆?/li>
- 瀹瑰櫒锛歋pring 鍖呭惈骞剁鐞嗗簲鐢ㄤ腑瀵硅薄鐨勭敓鍛藉懆鏈熷拰閰嶇疆銆?/li>
- MVC妗嗘灦锛歋pring鐨刉EB妗嗘灦鏄釜绮惧績璁捐鐨勬鏋讹紝鏄疻eb妗嗘灦鐨勪竴涓緢濂界殑鏇夸唬鍝併€?/li>
- 浜嬪姟绠$悊锛歋pring 鎻愪緵涓€涓寔缁殑浜嬪姟绠$悊鎺ュ彛锛屽彲浠ユ墿灞曞埌涓婅嚦鏈湴浜嬪姟涓嬭嚦鍏ㄥ眬浜嬪姟锛圝TA锛夈€?/li>
- 寮傚父澶勭悊锛歋pring 鎻愪緵鏂逛究鐨凙PI鎶婂叿浣撴妧鏈浉鍏崇殑寮傚父锛堟瘮濡傜敱JDBC锛孒ibernate or JDO鎶涘嚭鐨勶級杞寲涓轰竴鑷寸殑unchecked 寮傚父銆?/li>
4銆佽В閲奡pring鏀寔鐨勫嚑绉峛ean鐨勪綔鐢ㄥ煙锛?/p>
Spring妗嗘灦鏀寔浠ヤ笅浜旂bean鐨勪綔鐢ㄥ煙锛?/p>
- singleton : bean鍦ㄦ瘡涓猄pring ioc 瀹瑰櫒涓彧鏈変竴涓疄渚嬨€?/li>
- prototype锛氫竴涓猙ean鐨勫畾涔夊彲浠ユ湁澶氫釜瀹炰緥銆?/li>
- request锛氭瘡娆ttp璇锋眰閮戒細鍒涘缓涓€涓猙ean锛岃浣滅敤鍩熶粎鍦ㄥ熀浜巜eb鐨凷pring ApplicationContext鎯呭舰涓嬫湁鏁堛€?/li>
- session锛氬湪涓€涓狧TTP Session涓紝涓€涓猙ean瀹氫箟瀵瑰簲涓€涓疄渚嬨€傝浣滅敤鍩熶粎鍦ㄥ熀浜巜eb鐨凷pring ApplicationContext鎯呭舰涓嬫湁鏁堛€?/li>
- 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>
spring鍜宻pringMVC鐨勯潰璇曢棶棰樻€荤粨
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鍝嶅簲鐢ㄦ埛
15.Springmvc鐨勪紭鐐?/p>
绛旓細1.瀹冩槸鍩轰簬缁勪欢鎶€鏈殑.鍏ㄩ儴鐨勫簲鐢ㄥ璞?鏃犺鎺у埗鍣ㄥ拰瑙嗗浘,杩樻槸涓氬姟瀵硅薄涔嬬被鐨勯兘鏄?java缁勪欢.骞朵笖鍜孲pring鎻愪緵鐨勫叾浠栧熀纭€缁撴瀯绱у瘑闆嗘垚.
2.涓嶄緷璧栦簬Servlet API(鐩爣铏芥槸濡傛,浣嗘槸鍦ㄥ疄鐜扮殑鏃跺€欑‘瀹炴槸渚濊禆浜嶴ervlet鐨?
3. 鍙互浠绘剰浣跨敤鍚勭瑙嗗浘鎶€鏈?鑰屼笉浠呬粎灞€闄愪簬JSP
4 . 鏀寔鍚勭璇锋眰璧勬簮鐨勬槧灏勭瓥鐣?/p>
5 .瀹冨簲鏄槗浜庢墿灞曠殑
以上是关于Spring缁忓吀楂橀闈㈣瘯棰橈紝鍘熸潵鏄暱杩欎釜鏍峰瓙的主要内容,如果未能解决你的问题,请参考以下文章