锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Spring鍙互閫氳繃鎸囧畾classpath*:涓巆lasspath:鍓嶇紑鍔犺礬寰勭殑鏂瑰紡浠巆lasspath鍔犺澆鏂囦歡,濡俠ean鐨勫畾涔夋枃浠?classpath*:鐨勫嚭鐜版槸涓轟簡浠庡涓猨ar鏂囦歡涓姞杞界浉鍚岀殑鏂囦歡.classpath:鍙兘鍔犺澆鎵懼埌鐨勭涓涓枃浠? 姣斿 resource1.jar涓殑package 'com.test.rs' 鏈変竴涓?'jarAppcontext.xml' 鏂囦歡,鍐呭濡備笅: <bean name="ProcessorImplA" class="com.test.spring.di.ProcessorImplA" /> resource2.jar涓殑package 'com.test.rs' 涔熸湁涓涓?'jarAppcontext.xml' 鏂囦歡,鍐呭濡備笅: <bean id="ProcessorImplB" class="com.test.spring.di.ProcessorImplB" /> 閫氳繃浣跨敤涓嬮潰鐨勪唬鐮佸垯鍙互灝嗕袱涓猨ar鍖呬腑鐨勬枃浠墮兘鍔犺澆榪涙潵 ApplicationContext ctx = new ClassPathXmlApplicationContext( 鑰屽鏋滃啓鎴愪笅闈㈢殑浠g爜,灝卞彧鑳芥壘鍒板叾涓殑涓涓獂ml鏂囦歡(欏哄簭鍙栧喅浜巎ar鍖呯殑鍔犺澆欏哄簭) ApplicationContext ctx = new ClassPathXmlApplicationContext( classpath*:鐨勪嬌鐢ㄦ槸涓轟簡澶氫釜component(鏈緇堝彂甯冩垚涓嶅悓鐨刯ar鍖?騫惰寮鍙?鍚勮嚜鐨刡ean瀹氫箟鏂囦歡鎸夌収涓瀹氱殑瑙勫垯:package+filename,鑰屼嬌鐢ㄨ繖浜沜omponent鐨勮皟鐢ㄨ呭彲浠ユ妸榪欎簺鏂囦歡閮藉姞杞借繘鏉? classpath*:鐨勫姞杞戒嬌鐢ㄤ簡classloader鐨? 鏉ヨ嚜錛?a target="_blank">http://blog.csdn.net/kkdelta/article/details/5507799 銆銆 Spring妗嗘灦鍒欑畝鍖栦簡浣跨敤JEE緇勪歡(鍖呮嫭JMS)鐨勪換鍔°傚畠鎻愪緵鐨勬ā鏉挎満鍒墮殣钘忎簡鍏稿瀷鐨凧MS瀹炵幇鐨勭粏鑺傦紝榪欐牱寮鍙戜漢鍛樺彲浠ラ泦涓簿鍔涙斁鍦ㄥ鐞嗘秷鎭殑瀹為檯宸ヤ綔涓紝鑰屼笉鐢ㄦ媴蹇冨浣曞幓鍒涘緩錛岃闂垨娓呴櫎JMS璧勬簮銆?/p>
銆銆 鏈枃灝嗗Spring JMS API浣滀竴涓榪幫紝騫墮氳繃涓涓繍琛屽湪JBoss MQ鏈嶅姟鍣ㄤ笂鐨剋eb渚嬬▼鏉ヤ粙緇嶅浣曚嬌鐢⊿pring JMS API鏉ュ紓姝ュ鐞嗭紙鍙戦佸拰鎺ユ敹錛夋秷鎭傛垜灝嗛氳繃浼犵粺JMS瀹炵幇鍜孲pring JMS瀹炵幇涓よ呴棿鐨勬瘮杈冿紝鏉ュ睍紺轟嬌鐢⊿pring JMS澶勭悊娑堟伅鏄浣曠殑綆鍗曞拰鐏墊椿銆?/p>
銆銆鍦ㄧ幇瀹炰腑錛屽ぇ澶氭暟web璇鋒眰閮芥槸鍚屾澶勭悊鐨勩備緥濡傦紝褰撶敤鎴瘋鐧誨叆涓涓綉绔欙紝棣栧厛杈撳叆鐢ㄦ埛鍚嶅拰瀵嗙爜錛岀劧鍚庢湇鍔″櫒楠岃瘉鐧誨綍鍚堟硶鎬с傚鏋滈獙璇佹垚鍔燂紝紼嬪簭灝嗗厑璁歌鐢ㄦ埛榪涘叆緗戠珯銆傝繖閲岋紝鐧誨綍璇鋒眰鍦ㄤ粠瀹㈡埛绔帴鏀朵互鍚庤鍗蟲椂澶勭悊浜嗐備俊鐢ㄥ崱楠岃瘉鏄彟涓涓悓姝ュ鐞嗙殑渚嬪瓙錛涘彧鏈夋湇鍔″櫒璇佸疄杈撳叆鐨勪俊鐢ㄥ崱鍙鋒槸鏈夋晥鐨勶紝鍚屾椂瀹㈡埛鍦ㄥ笎鎴蜂笂鏈夎凍澶熺殑瀛樻錛屽鎴鋒墠琚厑璁哥戶緇搷浣溿備絾鏄鎴戜滑鎬濊冧竴涓嬪湪欏哄簭澶勭悊緋葷粺涓婄殑鏀粯緇撶畻姝ラ銆備竴鏃︾郴緇熻瘉瀹炶鐢ㄦ埛淇$敤鍗$殑淇℃伅鏄噯紜殑錛屽茍涓斿湪甯愭埛涓婃湁瓚沖鐨勮祫閲戯紝灝變笉蹇呯瓑鍒版墍鏈夌殑鏀粯緇嗚妭钀藉疄銆佽漿璐﹀畬鎴愩傛敮浠樼粨綆楀彲浠ュ紓姝ユ柟寮忚繘琛岋紝榪欐牱瀹㈡埛鍙互緇х畫榪涜鏍告煡鎿嶄綔銆?/p>
銆銆 闇瑕佹瘮鍏稿瀷鍚屾璇鋒眰鑰楄垂鏇撮暱鏃墮棿鐨勮姹傦紝鍙互浣跨敤寮傛澶勭悊銆傚彟涓涓紓姝ュ鐞嗙殑渚嬪瓙鏄紝鍦ㄦ湰鍦拌捶嬈懼鐞嗙▼搴忎腑錛屾彁浜よ嚦鑷姩鎵塊攢緋葷粺錛圓US錛夌殑淇$敤璇鋒眰澶勭悊榪囩▼銆傚綋鍊熸柟鎻愪氦璐鋒鐢寵鍚庯紝鎶墊娂鍏徃浼氬悜AUS鍙戦佽姹傦紝浠ヨ幏鍙栦俊鐢ㄥ巻鍙茶褰曘傜敱浜庤繖涓姹傝姹傚緱鍒板叏闈㈣屽張璇︾粏鐨勪俊鐢ㄦ姤鍛婏紝鍖呮嫭鍊熸柟鐜頒粖鍜岃繃鍘葷殑甯愭埛錛屾渶榪戠殑浠樻鍜屽叾浠栬儲鍔¤祫鏂欙紝鏈嶅姟鍣ㄩ渶瑕佽楄垂杈冮暱鐨勬椂闂達紙鍑犲皬鏃舵垨鐫鏈夋椂鐢氳嚦鏄嚑澶╋級鏉ュ榪欎簺璇鋒眰浣滃嚭鍝嶅簲銆傚鎴風紼嬪簭錛堝簲鐢級瑕佷笌鏈嶅姟鍣ㄨ繛鎺ュ茍鑰楄垂濡傛闀跨殑鏃墮棿鏉ョ瓑寰呯粨鏋滐紝榪欐槸姣棤鎰忎箟鐨勩傚洜姝ら氫俊搴旇鏄紓姝ュ彂鐢熺殑錛涗篃灝辨槸錛屼竴鏃﹁姹傝鎻愪氦錛屽畠灝辮鏀劇疆鍦ㄩ槦鍒椾腑錛屽悓鏃跺鎴風涓庢湇鍔″櫒鏂紑榪炴帴銆傜劧鍚嶢US鏈嶅姟浠庢寚瀹氱殑闃熷垪涓夊嚭璇鋒眰榪涜澶勭悊錛屽茍灝嗗鐞嗗緱鍒扮殑娑堟伅鏀劇疆鍦ㄥ彟涓涓秷鎭槦鍒楅噷銆傛渶鍚庯紝瀹㈡埛绔▼搴忎粠榪欎釜闃熷垪涓夊嚭澶勭悊緇撴灉錛岀揣鎺ョ潃澶勭悊榪欎釜淇$敤鍘嗗彶鏁版嵁銆?/p>
銆銆 濡傛灉鎮ㄤ嬌鐢ㄨ繃JMS浠g爜錛屾偍浼氬彂鐜板畠涓嶫DBC鎴朖CA寰堝儚銆傚畠鎵鍖呭惈鐨勬牱鏈唬鐮佸垱寤烘垨JMS璧勬簮瀵硅薄鍥炴函錛屼嬌寰楁瘡涓嬈℃偍闇瑕佸啓涓涓柊綾繪潵鍙戦佸拰鎺ユ敹娑堟伅鏃訛紝閮藉叿鏈夋洿濂界殑浠g爜瀵嗛泦鎬у拰閲嶅鎬с備互涓嬪簭鍒楁樉紺轟簡浼犵粺JMS瀹炵幇鎵鍖呮嫭鐨勬楠わ細 鎮ㄥ彲浠ョ湅鍒幫紝姝ラ6鏄鐞嗘秷鎭殑鍞竴鍦版柟銆傚叾浠栨楠ら兘鍙槸綆$悊涓庡疄闄呬笟鍔¤姹傛棤鍏崇殑JMS璧勬簮錛屼絾鏄紑鍙戜漢鍛樺繀欏葷紪鍐欏茍緇存姢榪欎簺棰濆姝ラ鐨勪唬鐮併?/p>
銆銆 Spring妗嗘灦鎻愪緵浜嗕竴涓ā鏉挎満鍒舵潵闅愯棌Java APIs鐨勭粏鑺傘侸EE寮鍙戜漢鍛樺彲浠ヤ嬌鐢↗DBCTemplate鍜孞NDITemplate綾繪潵鍒嗗埆璁塊棶鍚庡彴鏁版嵁搴撳拰JEE璧勬簮錛堟暟鎹簮錛岃繛鎺ユ睜錛夈侸MS涔熶笉渚嬪銆係pring鎻愪緵JMSTemplate綾伙紝鍥犳寮鍙戜漢鍛樹笉鐢ㄤ負涓涓狫MS瀹炵幇鍘葷紪鍐欐牱鏈唬鐮併傛帴涓嬫潵鏄湪寮鍙慗MS搴旂敤紼嬪簭鏃禨pring鎵鍏鋒湁涓浜涚殑浼樺娍銆?/p>
璇存槑錛氬洜涓哄彧鏄負浜嗘紨紺哄浣曚嬌鐢╯pring緙栧啓jms鐨勫簲鐢紝鎵浠ユ湰渚嬫病鏈変粈涔堝疄闄呯敤閫斻?/p>
紼嬪簭鍔熻兘錛歁essageProducer.java鏍規嵁涓鐢ㄦ埛淇℃伅浜х敓涓涓秷鎭彂閫佸埌 JMS Provider錛涚敱MessageConsumer.java鎺ユ敹銆?/p>
1.鍦↗boss閲岄厤緗甔ML鏂囦歡鍒涘緩涓涓柊鐨凧MS provider銆?br />
鎵撳紑浣嶄簬%JBOSS_HOME%server\default\deploy\jms鏂囦歡澶逛笅鐨刯bossmq-destinations-service.xml鏂囦歡錛屽姞鍏ヤ互涓嬩唬鐮佺墖鏂細 (4).嫻嬭瘯鐢ㄤ緥錛?br />
//======== 鐢熶駭鑰呮祴璇曠敤渚?=============== //============ 娑堣垂鑰呮祴璇曠敤渚?=============== Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1418938 銆銆鎴戜滑鐨凚ean閰嶇疆濡備笅錛?/p>
銆銆濡備笂鎵榪幫紝鎴戜滑瀹氫箟浜嗕竴涓狿ropertyPlaceholderConfigurer綾葷殑瀹炰緥錛屽茍灝嗗叾浣嶇疆灞炴ц緗負鎴戜滑鐨勫睘鎬ф枃浠躲傝綾昏瀹炵幇涓築ean宸ュ巶鐨勫悗澶勭悊鍣紝騫跺皢浣跨敤瀹氫箟鍦ㄦ枃浠朵腑鐨勫睘鎬ф潵浠f浛鎵鏈夌殑鍗犱綅絎︼紙${...}value錛夈?/p>
銆銆鍒╃敤榪欑鎶鏈紝鎴戜滑鍙互浠巃pplicationContext.xml涓Щ闄ゆ墍鏈夌壒瀹氫簬涓繪満鐨勯厤緗睘鎬с傞氳繃榪欑鏂瑰紡錛屾垜浠彲浠ヨ嚜鐢卞湴涓鴻鏂囦歡娣誨姞鏂扮殑Bean錛岃屼笉蹇呮媴蹇冪壒瀹氫簬涓繪満灞炴х殑鍚屾鎬с傝繖鏍峰彲浠ョ畝鍖栫敓浜ч儴緗插拰緇存姢銆?/p>
PropertyPlaceholderConfigurer浣滀負涓涓猙ean factory post-processor瀹炵幇錛屽彲浠ョ敤鏉ュ皢BeanFactory瀹氫箟涓殑灞炴у兼斁緗埌鍙︿竴涓崟鐙殑Java Properties鏍煎紡鐨勬枃浠朵腑銆傝繖浣垮緱鐢ㄦ埛涓嶇敤瀵笲eanFactory鐨勪富XML瀹氫箟鏂囦歡榪涜澶嶆潅鍜屽嵄闄╃殑淇敼錛屽氨鍙互瀹氬埗涓浜涘熀鏈殑灞炴э紙姣斿璇存暟鎹簱鐨剈rls,鐢ㄦ埛鍚嶅拰瀵嗙爜錛夈? 鑰冭檻涓涓狟eanFactory瀹氫箟鐨勭墖鏂紝閲岄潰鐢ㄥ崰浣嶇瀹氫箟浜咲ataSource錛? 鍦ㄤ笅闈㈣繖涓緥瀛愪腑錛屽畾涔変簡涓涓猟atasource錛屽茍涓旀垜浠細鍦ㄤ竴涓閮≒orperties鏂囦歡涓厤緗竴浜涚浉鍏沖睘鎬с?鍦ㄨ繍琛屾椂錛屾垜浠負BeanFactory鎻愪緵涓涓?tt class="literal">PropertyPlaceholderConfigurer錛屽畠灝嗙敤Properties鏂囦歡涓殑鍊兼浛鎹㈡帀榪欎釜datasource鐨勫睘鎬у鹼細 鐪熸鐨勫兼潵鑷簬鍙︿竴涓狿roperties鏍煎紡鐨勬枃浠? 濡傛灉瑕佸湪BeanFactory涓嬌鐢紝bean factory post-processor蹇呴』鎵嬪姩榪愯: 娉ㄦ剰錛孉pplicationContext鑳藉鑷姩杈ㄨ鍜屽簲鐢ㄥ湪鍏朵笂閮ㄧ講鐨勫疄鐜頒簡BeanFactoryPostProcessor鐨刡ean銆傝繖灝辨剰鍛崇潃錛屽綋浣跨敤ApplicationContext鐨勬椂鍊欏簲鐢≒ropertyPlaceholderConfigurer浼氶潪甯哥殑鏂逛究銆傜敱浜庤繖涓師鍥狅紝寤鴻鎯寵浣跨敤榪欎釜鎴栬呭叾浠朾ean factory postprocessor鐨勭敤鎴蜂嬌鐢ˋpplicationContext浠f浛BeanFactroy銆? PropertyPlaceHolderConfigurer涓嶄粎浠呭湪浣犳寚瀹氱殑Porperties鏂囦歡涓煡鎵懼睘鎬э紝 濡傛灉瀹冨湪鍏朵腑娌℃湁鎵懼埌浣犳兂浣跨敤鐨勫睘鎬э紝瀹冭繕浼氬湪Java鐨勭郴緇焢roperties涓煡鎵俱?榪欎釜琛屼負鑳藉閫氳繃璁劇疆閰嶇疆涓殑systemPropertiesMode 灞炴ф潵瀹氬埗銆傝繖涓睘鎬ф湁涓変釜鍊鹼紝 涓涓閰嶇疆鎬繪槸瑕嗙洊錛屼竴涓瀹?span class="emphasis">姘鎬笉瑕嗙洊錛屼竴涓瀹冧粎鍦╬roperties鏂囦歡涓壘涓嶅埌鐨勬椂鍊欒鐩栥?璇峰弬鑰?PropertiesPlaceholderConfigurer鐨凧avaDoc鑾峰緱鏇村淇℃伅銆?
"classpath*:com/test/rs/jarAppcontext.xml");
"classpath:com/test/rs/jarAppcontext.xml");getResources()
鏂規硶,濡傛灉鏄湪涓嶅悓鐨凧2EE鏈嶅姟鍣ㄤ笂榪愯,鐢變簬搴旂敤鏈嶅姟鍣ㄦ彁渚涜嚜宸辯殑classloader瀹炵幇錛屽畠浠湪澶勭悊jar鏂囦歡鏃剁殑琛屼負涔熻浼氭湁鎵涓嶅悓銆? 瑕佹祴璇?classpath*:
鏄惁鏈夋晥錛屽彲浠ョ敤classloader浠巆lasspath涓殑jar鏂囦歡閲屽姞杞芥枃浠舵潵榪涜嫻嬭瘯錛? getClass().getClassLoader().getResources("<someFileInsideTheJar>")
銆?涓婇潰鐨勪緥瀛愭槸鍦╯un鐨刯re涓繍琛岀殑鐘舵?
]]>
Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
瑙f焙鏂規硶:
1. 鍦╳eb.xml鏂囦歡涓坊鍔爈istener
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<aop:scoped-proxy/>
</bean>
]]>杞夎嚜錛歨ttp://blog.csdn.net/huxin1/archive/2008/12/31/3669583.aspx
document.body.oncopy = function() {
if (window.clipboardData) {
setTimeout(function() {
var text = clipboardData.getData("text");
if (text && text.length>300) {
text = text + "\r\n\n鏈枃鏉ヨ嚜CSDN鍗氬錛岃漿杞借鏍囨槑鍑哄錛? + location.href;
clipboardData.setData("text", text);
}
}, 100);
}
}
1. 浣跨敤Spring娉ㄨВ鏉ユ敞鍏ュ睘鎬?
1.1. 浣跨敤娉ㄨВ浠ュ墠鎴戜滑鏄庢牱娉ㄥ叆灞炴х殑
綾葷殑瀹炵幇錛?
閰嶇疆鏂囦歡錛?
1.2. 寮曞叆@Autowired娉ㄨВ錛堜笉鎺ㄨ崘浣跨敤錛屽緩璁嬌鐢ˊResource錛?
綾葷殑瀹炵幇錛堝鎴愬憳鍙橀噺榪涜鏍囨敞錛?
鎴栬咃紙瀵規柟娉曡繘琛屾爣娉級
閰嶇疆鏂囦歡
@Autowired鍙互瀵規垚鍛樺彉閲忋佹柟娉曞拰鏋勯犲嚱鏁拌繘琛屾爣娉紝鏉ュ畬鎴愯嚜鍔ㄨ閰嶇殑宸ヤ綔銆備互涓婁袱縐嶄笉鍚屽疄鐜版柟寮?
涓紝@Autowired鐨勬爣娉ㄤ綅緗笉鍚岋紝瀹冧滑閮戒細鍦⊿pring鍦ㄥ垵濮嬪寲userManagerImpl榪欎釜bean鏃訛紝鑷姩瑁呴厤userDao榪欎釜灞?
鎬э紝鍖哄埆鏄細絎竴縐嶅疄鐜頒腑錛孲pring浼氱洿鎺ュ皢UserDao綾誨瀷鐨勫敮涓涓涓猙ean璧嬪肩粰userDao榪欎釜鎴愬憳鍙橀噺錛涚浜岀瀹炵幇涓紝Spring浼?
璋冪敤setUserDao鏂規硶鏉ュ皢UserDao綾誨瀷鐨勫敮涓涓涓猙ean瑁呴厤鍒皍serDao榪欎釜灞炴с?
1.3. 璁〡Autowired宸ヤ綔璧鋒潵
瑕佷嬌@Autowired鑳藉宸ヤ綔錛岃繕闇瑕佸湪閰嶇疆鏂囦歡涓姞鍏ヤ互涓嬩唬鐮?
1.4. @Qualifier
@Autowired鏄牴鎹被鍨嬭繘琛岃嚜鍔ㄨ閰嶇殑銆傚湪涓婇潰鐨勪緥瀛愪腑錛屽鏋滃綋
Spring涓婁笅鏂囦腑瀛樺湪涓嶆涓涓猆serDao綾誨瀷鐨刡ean鏃訛紝灝變細鎶涘嚭BeanCreationException寮傚父錛涘鏋淪pring涓婁笅鏂囦腑
涓嶅瓨鍦║serDao綾誨瀷鐨刡ean錛屼篃浼氭姏鍑築eanCreationException寮傚父銆傛垜浠彲浠ヤ嬌鐢ˊQualifier閰嶅悎
@Autowired鏉ヨВ鍐寵繖浜涢棶棰樸?
1. 鍙兘瀛樺湪澶氫釜UserDao瀹炰緥
榪欐牱錛孲pring浼氭壘鍒癷d涓簎serDao鐨刡ean榪涜瑁呴厤銆?
2. 鍙兘涓嶅瓨鍦║serDao瀹炰緥
1.5. @Resource錛圝SR-250鏍囧噯娉ㄨВ錛屾帹鑽愪嬌鐢ㄥ畠鏉ヤ唬鏇縎pring涓撴湁鐨凘Autowired娉ㄨВ錛?
Spring 涓嶄絾鏀寔鑷繁瀹氫箟鐨凘Autowired娉ㄨВ錛岃繕鏀寔鍑犱釜鐢盝SR-250瑙勮寖瀹氫箟鐨勬敞瑙o紝瀹冧滑鍒嗗埆鏄疈Resource銆丂PostConstruct浠ュ強@PreDestroy銆?
@Resource
鐨勪綔鐢ㄧ浉褰撲簬@Autowired錛屽彧涓嶈繃@Autowired鎸塨yType鑷姩娉ㄥ叆錛岃孈Resource榛樿鎸塨yName鑷姩娉ㄥ叆緗簡銆?
@Resource鏈変袱涓睘鎬ф槸姣旇緝閲嶈鐨勶紝鍒嗗埆鏄痭ame鍜宼ype錛孲pring灝咢Resource娉ㄨВ鐨刵ame灞炴цВ鏋愪負bean鐨勫悕瀛楋紝鑰?
type灞炴у垯瑙f瀽涓篵ean鐨勭被鍨嬨傛墍浠ュ鏋滀嬌鐢╪ame灞炴э紝鍒欎嬌鐢╞yName鐨勮嚜鍔ㄦ敞鍏ョ瓥鐣ワ紝鑰屼嬌鐢╰ype灞炴ф椂鍒欎嬌鐢╞yType鑷姩娉ㄥ叆絳?
鐣ャ傚鏋滄棦涓嶆寚瀹歯ame涔熶笉鎸囧畾type灞炴э紝榪欐椂灝嗛氳繃鍙嶅皠鏈哄埗浣跨敤byName鑷姩娉ㄥ叆絳栫暐銆?
@Resource瑁呴厤欏哄簭
1.6. @PostConstruct錛圝SR-250錛?
鍦ㄦ柟娉曚笂鍔犱笂娉ㄨВ@PostConstruct錛岃繖涓柟娉曞氨浼氬湪Bean鍒濆鍖栦箣鍚庤Spring瀹瑰櫒鎵ц錛堟敞錛欱ean鍒濆鍖栧寘鎷紝瀹炰緥鍖朆ean錛屽茍瑁呴厤Bean鐨勫睘鎬э紙渚濊禆娉ㄥ叆錛夛級銆?
瀹冪殑涓涓吀鍨嬬殑搴旂敤鍦烘櫙鏄紝褰撲綘闇瑕佸線Bean閲屾敞鍏ヤ竴涓叾鐖剁被涓畾涔夌殑灞炴э紝鑰屼綘鍙堟棤娉曞鍐欑埗綾葷殑灞炴ф垨灞炴х殑setter鏂規硶鏃訛紝濡傦細
榪欓噷閫氳繃@PostConstruct錛屼負UserDaoImpl鐨勭埗綾婚噷瀹氫箟鐨勪竴涓猻essionFactory縐佹湁灞炴э紝娉ㄥ叆浜?
鎴戜滑鑷繁瀹氫箟鐨剆essionFactory錛堢埗綾葷殑setSessionFactory鏂規硶涓篺inal錛屼笉鍙鍐欙級錛屼箣鍚庢垜浠氨鍙互閫氳繃璋冪敤
super.getSessionFactory()鏉ヨ闂灞炴т簡銆?
1.7. @PreDestroy錛圝SR-250錛?
鍦ㄦ柟娉曚笂鍔犱笂娉ㄨВ@PreDestroy錛岃繖涓柟娉曞氨浼氬湪Bean鍒濆鍖栦箣鍚庤Spring瀹瑰櫒鎵ц銆傜敱浜庢垜浠綋鍓嶈繕娌℃湁闇瑕佺敤鍒板畠鐨勫満鏅紝榪欓噷涓嶄笉鍘繪紨紺恒傚叾鐢ㄦ硶鍚孈PostConstruct銆?
1.8. 浣跨敤<context:annotation-config />綆鍖栭厤緗?
Spring2.1
娣誨姞浜嗕竴涓柊鐨刢ontext鐨凷chema鍛藉悕絀洪棿錛岃鍛藉悕絀洪棿瀵規敞閲婇┍鍔ㄣ佸睘鎬ф枃浠跺紩鍏ャ佸姞杞芥湡緇囧叆絳夊姛鑳芥彁渚涗簡渚挎嵎鐨勯厤緗傛垜浠煡閬撴敞閲婃湰韜槸涓嶄細
鍋氫換浣曚簨鎯呯殑錛屽畠浠呮彁渚涘厓鏁版嵁淇℃伅銆傝浣垮厓鏁版嵁淇℃伅鐪熸璧蜂綔鐢紝蹇呴』璁╄礋璐e鐞嗚繖浜涘厓鏁版嵁鐨勫鐞嗗櫒宸ヤ綔璧鋒潵銆?
AutowiredAnnotationBeanPostProcessor
鍜孋ommonAnnotationBeanPostProcessor灝辨槸澶勭悊榪欎簺娉ㄩ噴鍏冩暟鎹殑澶勭悊鍣ㄣ備絾鏄洿鎺ュ湪Spring閰嶇疆鏂囦歡涓畾涔夎繖浜?
Bean鏄懼緱姣旇緝絎ㄦ嫏銆係pring涓烘垜浠彁渚涗簡涓縐嶆柟渚跨殑娉ㄥ唽榪欎簺BeanPostProcessor鐨勬柟寮忥紝榪欏氨
鏄?lt;context:annotation-config />錛?
<context:annotationconfig
/>灝嗛殣寮忓湴鍚慡pring瀹瑰櫒娉ㄥ唽AutowiredAnnotationBeanPostProcessor銆?
CommonAnnotationBeanPostProcessor銆?
PersistenceAnnotationBeanPostProcessor浠ュ強
RequiredAnnotationBeanPostProcessor榪?涓狟eanPostProcessor銆?
2. 浣跨敤Spring娉ㄨВ瀹屾垚Bean鐨勫畾涔?
浠ヤ笂鎴戜滑浠嬬粛浜嗛氳繃@Autowired鎴朄Resource鏉ュ疄鐜板湪Bean涓嚜鍔ㄦ敞鍏ョ殑鍔熻兘錛屼笅闈㈡垜浠皢浠嬬粛濡備綍娉ㄨВBean錛屼粠鑰屼粠XML閰嶇疆鏂囦歡涓畬鍏ㄧЩ闄ean瀹氫箟鐨勯厤緗?
2.1. @Component錛堜笉鎺ㄨ崘浣跨敤錛夈丂Repository銆丂Service銆丂Controller
鍙渶瑕佸湪瀵瑰簲鐨勭被涓婂姞涓婁竴涓狜Component娉ㄨВ錛屽氨灝嗚綾誨畾涔変負涓涓狟ean浜嗭細
浣跨敤@Component娉ㄨВ瀹氫箟鐨凚ean錛岄粯璁ょ殑鍚嶇О錛坕d錛夋槸灝忓啓寮澶寸殑闈為檺瀹氱被鍚嶃傚榪欓噷瀹氫箟鐨凚ean鍚嶇О灝辨槸userDaoImpl銆備綘涔熷彲浠ユ寚瀹欱ean鐨勫悕縐幫細
@Component("userDao")
@Component
鏄墍鏈夊彈Spring綆$悊緇勪歡鐨勯氱敤褰㈠紡錛孲pring榪樻彁渚涗簡鏇村姞緇嗗寲鐨勬敞瑙e艦寮忥細@Repository銆丂Service銆?
@Controller錛屽畠浠垎鍒搴斿瓨鍌ㄥ眰Bean錛屼笟鍔″眰Bean錛屽拰灞曠ず灞侭ean銆傜洰鍓嶇増鏈紙2.5錛変腑錛岃繖浜涙敞瑙d笌@Component鐨勮涔?
鏄竴鏍風殑錛屽畬鍏ㄩ氱敤錛屽湪Spring浠ュ悗鐨勭増鏈腑鍙兘浼氱粰瀹冧滑榪藉姞鏇村鐨勮涔夈傛墍浠ワ紝鎴戜滑鎺ㄨ崘浣跨敤@Repository銆丂Service銆?
@Controller鏉ユ浛浠Component銆?
2.2. 浣跨敤<context:component-scan />璁〣ean瀹氫箟娉ㄨВ宸ヤ綔璧鋒潵
榪欓噷錛屾墍鏈夐氳繃<bean>鍏冪礌瀹氫箟Bean鐨勯厤緗唴瀹瑰凡緇忚縐婚櫎錛屼粎闇瑕佹坊鍔犱竴
琛?lt;context:component-scan />閰嶇疆灝辮В鍐蟲墍鏈夐棶棰樹簡鈥斺擲pring
XML閰嶇疆鏂囦歡寰楀埌浜嗘瀬鑷寸殑綆鍖栵紙褰撶劧閰嶇疆鍏冩暟鎹繕鏄渶瑕佺殑錛屽彧涓嶈繃浠ユ敞閲婂艦寮忓瓨鍦ㄧ艦浜嗭級銆?lt;context:component-scan
/>鐨刡ase-package灞炴ф寚瀹氫簡闇瑕佹壂鎻忕殑綾誨寘錛岀被鍖呭強鍏墮掑綊瀛愬寘涓墍鏈夌殑綾婚兘浼氳澶勭悊銆?
<context:component-scan />榪樺厑璁稿畾涔夎繃婊ゅ櫒灝嗗熀鍖呬笅鐨勬煇浜涚被綰沖叆鎴栨帓闄ゃ係pring鏀寔浠ヤ笅4縐嶇被鍨嬬殑榪囨護鏂瑰紡錛?
浠ユ鍒欒〃杈懼紡涓轟緥錛屾垜鍒椾婦涓涓簲鐢ㄥ疄渚嬶細
鍊煎緱娉ㄦ剰鐨勬槸<context:component-scan
/>閰嶇疆欏逛笉浣嗗惎鐢ㄤ簡瀵圭被鍖呰繘琛屾壂鎻忎互瀹炴柦娉ㄩ噴椹卞姩Bean瀹氫箟鐨勫姛鑳斤紝鍚屾椂榪樺惎鐢ㄤ簡娉ㄩ噴椹卞姩鑷姩娉ㄥ叆鐨勫姛鑳斤紙鍗寵繕闅愬紡鍦板湪鍐呴儴娉ㄥ唽浜?
AutowiredAnnotationBeanPostProcessor鍜孋ommonAnnotationBeanPostProcessor錛夛紝
鍥犳褰撲嬌鐢?lt;context:component-scan
/>鍚庯紝灝卞彲浠ュ皢<context:annotation-config />縐婚櫎浜嗐?
2.3. 浣跨敤@Scope鏉ュ畾涔塀ean鐨勪綔鐢ㄨ寖鍥?
鍦ㄤ嬌鐢╔ML瀹氫箟Bean鏃訛紝鎴戜滑鍙兘榪橀渶瑕侀氳繃bean鐨剆cope灞炴ф潵瀹氫箟涓涓狟ean鐨勪綔鐢ㄨ寖鍥達紝鎴戜滑鍚屾牱鍙互閫氳繃@Scope娉ㄨВ鏉ュ畬鎴愯繖欏瑰伐浣滐細
3. 鍙傝?
http://kingtai168.javaeye.com/blog/244002
http://www.javaeye.com/topic/244153
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning
]]>
銆銆
銆銆package com.deity.ranking.util;
import java.util.List;
銆銆import org.springframework.jdbc.core.JdbcTemplate;
銆銆import org.springframework.jdbc.core.support.JdbcDaoSupport;
銆銆/** * 鍒嗛〉鍑芥暟 *銆
* @author allenpan */
public class Pagination extends JdbcDaoSupport{
銆銆public static final int NUMBERS_PER_PAGE = 10;
銆銆//涓欏墊樉紺虹殑璁板綍鏁?br />
銆銆private int numPerPage;
銆銆//璁板綍鎬繪暟
銆銆private int totalRows;
銆銆//鎬婚〉鏁?br />
銆銆private int totalPages;
銆銆//褰撳墠欏電爜
銆銆private int currentPage;
銆銆//璧峰琛屾暟
銆銆private int startIndex;
銆銆//緇撴潫琛屾暟
銆銆private int lastIndex;
銆銆//緇撴灉闆嗗瓨鏀綥ist
銆銆private List resultList;
銆銆//JdbcTemplate jTemplate
銆銆private JdbcTemplate jTemplate;
銆銆/**
銆銆* 姣忛〉鏄劇ず10鏉¤褰曠殑鏋勯犲嚱鏁?浣跨敤璇ュ嚱鏁板繀欏誨厛緇橮agination璁劇疆currentPage錛宩Template鍒濆?br />
銆銆* @param sql oracle璇彞
銆銆*/
銆銆public Pagination(String sql){
銆銆if(jTemplate == null){
銆銆throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
銆銆}else if(sql.equals("")){
銆銆throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
銆銆}
銆銆new Pagination(sql,currentPage,NUMBERS_PER_PAGE,jTemplate);
銆銆}
銆銆/**鍒嗛〉鏋勯犲嚱鏁?br />
銆銆* @param sql 鏍規嵁浼犲叆鐨剆ql璇彞寰楀埌涓浜涘熀鏈垎欏典俊鎭?br />
銆銆* @param currentPage 褰撳墠欏?br />
銆銆* @param numPerPage 姣忛〉璁板綍鏁?br />
銆銆* @param jTemplate JdbcTemplate瀹炰緥
銆銆*/
銆銆public Pagination(String sql,int currentPage,int numPerPage,JdbcTemplate jTemplate){
銆銆if(jTemplate == null){
銆銆throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
銆銆}else if(sql == null || sql.equals("")){
銆銆throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
銆銆}
銆銆//璁劇疆姣忛〉鏄劇ず璁板綍鏁?br />
銆銆setNumPerPage(numPerPage);
銆銆//璁劇疆瑕佹樉紺虹殑欏墊暟
銆銆setCurrentPage(currentPage);
銆銆//璁$畻鎬昏褰曟暟
銆銆StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
銆銆totalSQL.append(sql);
銆銆totalSQL.append(" ) totalTable ");
銆銆//緇橨dbcTemplate璧嬪?br />
銆銆setJdbcTemplate(jTemplate);
銆銆//鎬昏褰曟暟
銆銆setTotalRows(getJdbcTemplate().queryForInt(totalSQL.toString()));
銆銆//璁$畻鎬婚〉鏁?br />
銆銆setTotalPages();
銆銆//璁$畻璧峰琛屾暟
銆銆setStartIndex();
銆銆//璁$畻緇撴潫琛屾暟
銆銆setLastIndex();
銆銆System.out.println("lastIndex="+lastIndex);//////////////////
銆銆//鏋勯爋racle鏁版嵁搴撶殑鍒嗛〉璇彞
銆銆StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
銆銆paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
銆銆paginationSQL.append(sql);
銆銆paginationSQL.append("銆) temp where ROWNUM <= " + lastIndex);
銆銆paginationSQL.append(" ) WHERE銆num > " + startIndex);
銆銆//瑁呭叆緇撴灉闆?br />
銆銆setResultList(getJdbcTemplate().queryForList(paginationSQL.toString()));
銆銆}
銆銆/**
銆銆* @param args
銆銆*/
銆銆public static void main(String[] args) {
銆銆// TODO Auto-generated method stub銆銆銆銆}
銆銆public int getCurrentPage() {
銆銆return currentPage;
銆銆}
銆銆public void setCurrentPage(int currentPage) {
銆銆this.currentPage = currentPage;
銆銆}
銆銆public int getNumPerPage() {
銆銆return numPerPage;
銆銆}
銆銆public void setNumPerPage(int numPerPage) {
銆銆this.numPerPage = numPerPage;
銆銆}
銆銆public List getResultList() {
銆銆return resultList;銆銆銆銆}
銆銆public void setResultList(List resultList) {
銆銆this.resultList = resultList;
銆銆}
銆銆public int getTotalPages() {
銆銆return totalPages;
銆銆}
銆銆//璁$畻鎬婚〉鏁?br />
銆銆public void setTotalPages() {
銆銆if(totalRows % numPerPage == 0){
銆銆this.totalPages = totalRows / numPerPage;
銆銆}else{
銆銆this.totalPages銆= (totalRows / numPerPage) + 1;
銆銆}
銆銆}
銆銆public int getTotalRows() {
銆銆return totalRows;
銆銆}
銆銆public void setTotalRows(int totalRows) {
銆銆this.totalRows = totalRows;
銆銆}
銆銆public int getStartIndex() {
銆銆return startIndex;
銆銆}
銆銆public void setStartIndex() {
銆銆this.startIndex = (currentPage - 1) * numPerPage;
銆銆}
銆銆public int getLastIndex() {
銆銆return lastIndex;
銆銆}
銆銆public JdbcTemplate getJTemplate() {
銆銆return jTemplate;
銆銆}
銆銆public void setJTemplate(JdbcTemplate template) {
銆銆jTemplate = template;
銆銆}
銆銆//璁$畻緇撴潫鏃跺欑殑绱㈠紩
銆銆public void setLastIndex() {
銆銆System.out.println("totalRows="+totalRows);///////////
銆銆System.out.println("numPerPage="+numPerPage);///////////
銆銆if( totalRows < numPerPage){
銆銆this.lastIndex = totalRows;
銆銆}else if((totalRows % numPerPage == 0) || (totalRows % numPerPage != 0 && currentPage < totalPages)){
銆銆this.lastIndex = currentPage * numPerPage;
銆銆}else if(totalRows % numPerPage != 0 && currentPage == totalPages){//鏈鍚庝竴欏?br />
銆銆this.lastIndex = totalRows ;
銆銆}
銆銆}}鍦ㄦ垜鐨勪笟鍔¢昏緫浠g爜涓細
銆銆/**
銆銆* find season ranking list from DC
銆銆* @param areaId 閫夋墜鍖哄煙id
銆銆* @param rankDate 璧涘
銆銆* @param category 綾誨埆
銆銆* @param characterName 瑙掕壊鍚?br />
銆銆* @return List
銆銆*/
銆銆public List findSeasonRankingList(Long areaId, int rankYear,int rankMonth,
銆銆Long categoryId,String characterName) {
銆銆//SQL璇彞
銆銆StringBuffer sql = new StringBuffer(" SELECT C.USERID userid,D.POSNAME posname,C.GAMEID gameid,C.AMOUNT amount,C.RANK rank FROM ");
銆銆//琛ㄣ銆銆銆銆銆銆銆銆銆銆銆sql.append(" (SELECT B.USERID USERID,");
銆銆sql.append(" B.POSID POSID,");
銆銆sql.append(" A.DISTRICT_CODE DISTRICTCODE,");
銆銆sql.append(" A.GAMEID GAMEID,");
銆銆sql.append(" AMOUNT AMOUNT,");
銆銆sql.append(" RANK RANK ");
銆銆sql.append(" FROM TB_FS_RANK A ");
銆銆sql.append(" LEFT JOIN TB_CHARACTER_INFO B ");
銆銆sql.append(" ON A.DISTRICT_CODE = B.DISTRICT_CODE ");
銆銆sql.append(" AND A.GAMEID = B.GAMEID ");
銆銆//闄勫姞鏉′歡
銆銆if(areaId != null && areaId.intValue() != 0){
銆銆sql.append(" and A.DISTRICT_CODE = " + areaId.intValue());
銆銆}
銆銆if( rankYear > 1970 && rankMonth > 0){
銆銆//hql.append(" and sas.id.dt >= to_date('" + rankYear + "-" + rankMonth + "-01 00:00:00'," + "YYYY-MM-DD HH24:MI:SS");
銆銆//hql.append(" and sas.id.dt <= to_date('" + rankYear + "-" + rankMonth + "-" + TimeTool.findMaxDateInMonth(rankYear,rankMonth) + " 23:59:59'," + "YYYY-MM-DD HH24:MI:SS");
銆銆sql.append(" and A.DT = fn_time_convert(to_date('" + rankYear + "-" + rankMonth + "'," + "'YYYY-MM')) ");
銆銆}
銆銆if(categoryId != null && categoryId.intValue() != 0){
銆銆sql.append(" and A.CID = " + categoryId.intValue());
銆銆}
銆銆if(characterName != null && !characterName.trim().equals("")){
銆銆sql.append(" and A.GAMEID = '" + characterName.trim()+"' ");
銆銆}
銆銆sql.append(" ORDER BY RANK ASC) C ");
銆銆sql.append(" LEFT JOIN TB_FS_POSITION D ");
銆銆sql.append(" ON C.POSID = D.POSID ");
銆銆sql.append(" ORDER BY C.RANK ");
銆銆System.out.println("hql="+sql.toString());////////////////
銆銆//浣跨敤鑷繁鐨勫垎欏電▼搴忔帶鍒剁粨鏋滈泦
銆銆Pagination pageInfo = new Pagination(sql.toString(),1,10,getJdbcTemplate());
銆銆return pageInfo.getResultList();
銆銆//return getJdbcTemplate().queryForList(sql.toString());
銆銆}
鏂囩珷渚嗘簮錛歨ttp://java.chinaitlab.com/Spring/38091.html
]]>
]]>寮傛娑堟伅浼犻掑拰闈㈠悜鏈嶅姟鏋舵瀯
JMS
Spring JMS
紺轟緥紼嬪簭
<!-- Register User Send/Receive Queue -->
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=registerUserQueue">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<!-- Register User Send/Receive Topic -->
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=registerUserTopic">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
2.鍦╯pring鐨勯厤緗枃浠朵腑閰嶇疆JMS緇勪歡鐨勫叿浣撶粏鑺傘?br />
錛?錛塉NDI涓婁笅鏂囨槸鍙栧緱JMS璧勬簮鐨勮搗濮嬩綅緗紝鍥犳棣栧厛鎴戜滑瑕侀厤緗甁NDI妯℃澘錛?br />
<!-- JNDI涓婁笅鏂?瀹冩槸鍙栧緱JMS璧勬簮鐨勮搗濮嬩綅緗? -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.provider.url">localhost</prop>
<prop key="java.naming.factory.url.pkgs">
org.jnp.interfaces:org.jboss.naming
</prop>
</props>
</property>
</bean>
娉ㄦ剰錛氭JNDI妯℃澘鐢ㄥ埌浜唎rg.jnp.interfaces.NamingContextFactory鎵浠ヨ鎶?JBOSS_HOME%\client涓嬬殑jbossall-client.jar鍔犲埌浣犵殑欏圭洰鐨刢lasspath涓?br />
錛?錛夐厤緗繛鎺ュ伐鍘傦細
<!-- JMS榪炴帴宸ュ巶 -->
<bean id="jmsConnectionFactory"class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>XAConnectionFactory</value>
</property>
</bean>
娉ㄦ剰錛歑AConnectionFactory榪欎釜JNDI鍚嶅瓧鏄湪%JBOSS_HOME%server\default\deploy\jms鏂囦歡澶逛笅鐨刯ms-ds.xml涓畾涔夌殑(瀹冩槸鐢盝Boss鎸囧畾鐨?銆?br />
錛?錛夐厤緗甁msTemplate緇勪歡銆傚湪渚嬬▼涓垜浠嬌鐢↗msTemplate102銆傚悓鏃朵嬌鐢╠efaultDestination灞炴ф潵鎸囧畾JMS鐩爣銆?br />
<!-- JMS妯℃澘閰嶇疆 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate102">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="destination" />
<property name="pubSubDomain">
<value>true</value>
</property>
<!-- 絳夊緟娑堟伅鐨勬椂闂?ms) -->
<property name="receiveTimeout">
<value>30000</value>
</property>
</bean>
娉ㄦ剰錛氬鏋滀嬌鐢╰opic-subscribe(涓婚璁㈤槄)妯″紡錛岃妯℃澘鐨刾ubSubDomain灞炴у間負true;鑻ヤ嬌鐢≒ToP(鐐瑰鐐?妯″紡錛宲ubSubDomain灞炴у間負false鎴栦笉閰嶇疆璇ュ睘鎬с?br />
(4)瀹氫箟涓涓狫MS鐩爣鏉ュ彂閫佸拰鎺ユ敹娑堟伅:
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</property>
<property name="jndiName">
<value>topic/registerUserTopic</value>
</property>
</bean>
(5)閰嶇疆鍙戦佽呭拰鎺ユ敹鑰呯粍浠?
<!-- 娑堟伅鍙戝竷鑰?-->
<bean id="msgProducer" class="com.boco.jms.MessageProducer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- 娑堟伅鎺ユ敹鑰?-->
<bean id="msgConsumer" class="com.boco.jms.MessageConsumer">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
3.鐩稿簲鐨勭被:
(1). User瀵硅薄銆?br />
/**
* User.java
* created on Jul 2, 2006
* Copyrights 2006 BOCO,Inc. All rights reserved.
*/
package com.boco.dto;
import java.io.Serializable;
/**
* desc: 鐢ㄦ埛淇℃伅 Bean
* @author qiujy
*/
public class User {
private int id;
private String username;
private String password;
private String email;
public User(){}
//浠ヤ笅涓篏etter,setter鏂規硶鐣?br />
......
}
(2).娑堟伅鐢熶駭鑰咃細
/**
* MessageProducer.java
* created on Jul 22, 2006
* Copyrights 2006 BOCO,Inc. All rights reserved.
*/
package com.boco.jms;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.boco.dto.User;
/**
* desc:娑堟伅鐢熶駭鑰?br />
* @author qiujy
*
*/
public class MessageProducer {
/** JMS妯℃澘 */
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate){
this.jmsTemplate = jmsTemplate;
}
public void sendMessage(final User user){
//璋冪敤妯℃澘鐨剆end鏉ュ彂閫佹秷鎭?br />
jmsTemplate.send(new MessageCreator(){
public Message createMessage(Session session) throws JMSException {
//鏋勯犱竴涓鍙戦佺殑娑堟伅
MapMessage message = session.createMapMessage();
message.setInt("id", user.getId());
message.setString("username", user.getUsername());
message.setString("password", user.getPassword());
message.setString("email", user.getEmail());
System.out.println("send success!!");
return message;
}
});
}
}
(3).娑堟伅娑堣垂鑰咃細
/**
* MessageConsumer.java
* created on Jul 22, 2006
* Copyrights 2006 BOCO,Inc. All rights reserved.
*/
package com.boco.jms;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import org.springframework.jms.core.JmsTemplate;
import com.boco.dto.User;
/**
* desc:娑堟伅娑堣垂鑰?br />
* @author qiujy
*
*/
public class MessageConsumer {
/** JMS妯℃澘 */
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate){
this.jmsTemplate = jmsTemplate;
}
public User receiveMessage(){
//鍙傛暟涓篋estination鐨凧NDI鍚嶅瓧鍘繪帀鍓嶉潰鐨勬ā寮忕被鍨嬫爣璇?br />
//MapMessage msg = (MapMessage)jmsTemplate.receive("registerUserQueue");
MapMessage msg = (MapMessage)jmsTemplate.receive("registerUserTopic");
User user = new User();
try {
user.setId(msg.getInt("id"));
user.setUsername(msg.getString("username"));
user.setPassword(msg.getString("password"));
user.setEmail(msg.getString("email"));
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
}
/**
* TestMsgProducer.java
* created on Jul 22, 2006
* Copyrights 2006 BOCO,Inc. All rights reserved.
*/
package com.boco.jms;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.boco.dto.User;
/**
* desc:
* @author qiujy
*
*/
public class TestMsgProducer extends TestCase {
private ApplicationContext context;
/**
* @param arg0
*/
public TestMsgProducer(String arg0) {
super(arg0);
context = new ClassPathXmlApplicationContext("applicationContext_jms.xml");
}
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Test method for {@link com.boco.jms.MessageProducer#sendMessage(com.boco.dto.User)}.
*/
public void testSendMessage() {
User user = new User();
user.setId(132);
user.setUsername("JMSTest");
user.setPassword("password");
user.setEmail("support@boco.com.cn");
MessageProducer producer = (MessageProducer)context.getBean("msgProducer");
producer.sendMessage(user);
}
}
/**
* TestMsgConsumer.java
* created on Jul 22, 2006
* Copyrights 2006 BOCO,Inc. All rights reserved.
*/
package com.boco.jms;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.boco.dto.User;
/**
* desc:
* @author qiujy
*
*/
public class TestMsgConsumer extends TestCase {
private ApplicationContext context;
/**
* @param arg0
*/
public TestMsgConsumer(String arg0) {
super(arg0);
context = new ClassPathXmlApplicationContext("applicationContext_jms.xml");
}
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Test method for {@link com.boco.jms.MessageConsumer#receiveMessage()}.
*/
public void testReceiveMessage() {
MessageConsumer consumer = (MessageConsumer)context.getBean("msgConsumer");
User user = consumer.receiveMessage();
assertNotNull(user);
System.out.println( "id========" + user.getId()
+ "\nname======" + user.getUsername()
+ "\npassword==" + user.getPassword()
+ "\nemail=====" + user.getEmail());
}
}
鍚姩JBOSS鏈嶅姟鍣?鍏堣繍琛孴estMsgConsumer.java;鍐嶈繍琛孴estMsgProducer.java,鍗沖彲鐪嬪埌鎺у埗鍙扮殑杈撳嚭浜?鍢誨樆~~~~~
]]>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/starxing/test/jdbc.properties</value>
</property>
<!--
浣跨敤locations灞炴у畾涔夊涓厤緗枃浠?br />
<property name="locations">
<list>
<value>classpath:config/maxid.properties</value>
<value>classpath:config/jdoserver.properties</value>
</list>
</property>
-->
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${database.url}</value>
</property>
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="username">
<value>${database.user}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
</bean>
</beans>
3.Config.java
package com.starxing.test;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class Config {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"com/starxing/test/conf.xml"));
// 濡傛灉瑕佸湪BeanFactory涓嬌鐢紝bean factory post-processor蹇呴』鎵嬪姩榪愯:
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource(
"com/starxing/test/jdbc.properties"));
cfg.postProcessBeanFactory(factory);
DriverManagerDataSource dataSource = (DriverManagerDataSource) factory
.getBean("dataSource");
System.out.println(dataSource.getDriverClassName());
// 娉ㄦ剰錛孉pplicationContext鑳藉鑷姩杈ㄨ鍜屽簲鐢ㄥ湪鍏朵笂閮ㄧ講鐨勫疄鐜頒簡BeanFactoryPostProcessor鐨刡ean銆傝繖灝辨剰鍛崇潃錛屽綋浣跨敤ApplicationContext鐨勬椂鍊欏簲鐢≒ropertyPlaceholderConfigurer浼氶潪甯哥殑鏂逛究銆傜敱浜庤繖涓師鍥狅紝寤鴻鎯寵浣跨敤榪欎釜鎴栬呭叾浠朾ean
// factory postprocessor鐨勭敤鎴蜂嬌鐢ˋpplicationContext浠f浛BeanFactroy銆?br />
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/starxing/test/conf.xml");
DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context
.getBean("dataSource");
System.out.println(dataSource2.getDriverClassName());
}
}
鐩稿叧鏂囨。:
銆浣跨敤榪欎竴瑙e喅鏂規錛屾垜浠彲浠ョ敓鎴愬涓嬬殑灞炴ф枃浠訛紙/WEB-INF/jdbc.properties錛夛細
jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost/test
jdbc.user=postgres
jdbc.password=
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.user}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=rootXmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);
鏂囩珷鏉ユ簮:http://www.cublog.cn/u/9295/showart.php?id=261437
]]>
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ajaxtags.helpers.AjaxXmlBuilder;
import org.ajaxtags.servlets.BaseAjaxServlet;
import org.springframework.web.context.support.WebApplicationContextUtils;
import dgut.ke.model.Book;
import dgut.ke.service.IBookService;
@SuppressWarnings("serial")
public class BookCallOut extends BaseAjaxServlet {
private IBookService bookService;
@Override
public String getXmlContent(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setCharacterEncoding("UTF-8");
String bookName = request.getParameter("bookName");
System.out.println("---- " + bookName);
Book book = (Book) bookService.findForObject(
"from Book as b where b.bookName=?",new Object[]{bookName});
AjaxXmlBuilder builder = new AjaxXmlBuilder()
.addItemAsCData("鎻愮ず鏍囬", "<P>鍏充簬涔︾睄:<b>" + book.getBookNo() +"</b>鐨勪俊鎭涓?<br/></p>");
return builder.toString();
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
// 蹇呴』鍦ㄩ噸鍐欐柟娉曠殑絎竴琛岃皟鐢ㄤ互涓嬭鍙?br />
super.init(servletConfig);
bookService = (IBookService) WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext())
.getBean("bookService");
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class MailTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom("sskwfnihc@gmail.com");
mail.setTo("sskwfnihc@163.com");
mail.setSubject("KE緇欎綘鍙戜簡涓灝侀偖浠?);
mail.setText("This is a mailTest example");
mailSender.send(mail);
System.out.println("鎴愬姛鍙戦佹枃鏈枃浠訛紒");
}
}
spring閰嶇疆鏂囦歡:
... ...
<!-- 鍙戦佺數瀛愰偖浠?-->
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="host" value="smtp.gmail.com"></property>
<property name="port" value="465"></property>
<property name="username">
<value>sskwfnihc</value>
</property>
<property name="password">
<value>write your password here</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
</props>
</property>
</bean>
... ...
鍦╯pring2.0涓紝涓鍏卞畾涔変簡5縐嶄綔鐢ㄥ煙錛屽垎鍒槸singleton銆乸rototype銆乺equest銆乻ession銆?br /> globalSession銆傚叾涓悗闈笁縐嶄粎瀵筗EB搴旂敤紼嬪簭鏈夋晥銆?br /> spring2.0閫氳繃scope=""灞炴ф潵瀹氫箟Bean鏈変綔鐢ㄥ煙錛岃屽湪spring1.x涓紝鐢變簬鍙湁singleton 鍜?br /> prototype榪欎袱縐嶄綔鐢ㄥ煙錛屽洜姝わ紝spring1.x涓畾涔塀ean鐨勪綔鐢ㄥ煙鏄氳繃singleton=“true|false" 鏉?br /> 瀹氫箟鐨勩傚湪spring2.0涓鍏堝簲璇ヨ冭檻鐢╯cope灞炴ф潵瀹氫箟Bean 鐨勪綔鐢ㄥ煙銆?/p>
Singleton浣滅敤鍩?br />
spring2.0鏄粯璁ean鐨勪綔鐢ㄥ煙鏄痵cope="singleton"銆傚嵆spring鐨処oC瀹瑰櫒浠呬負姣忎釜Bean鍒涘緩涓涓?br />
瀹炰緥騫朵笖淇濇寔Bean鐨勫紩鐢紝鎹㈣█涔嬶紝姣忔璋冪敤getBean()鏂規硶璇鋒眰榪斿洖鐨凚ean鎬繪槸鍚屼竴涓疄渚嬨傝繖綾諱技
浜庛婅璁℃ā寮忋嬩竴涔︿腑鐨凷ingleton錛堝崟渚嬶級妯″紡銆?/p>
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {};
pubilc static Singleton getInstance() {
return instance;
}
}
鐢變簬鏋勯犳柟娉曡鐢蟲槑涓簆rivate錛屽洜姝わ紝鍦ㄧ被鐨勫閮ㄤ笉鍙疄渚嬪寲璇ョ被錛屽敮涓鐨勫疄渚嬫寔鏈夊湪闈欐佸紩鐢ㄤ腑
浠庡閮ㄨ幏鍙栬瀹炰緥鐨勫敮涓鍏ュ彛鏄皟鐢⊿ingleton鐨勯潤鎬佹柟娉昐ingleton.getInstance().
Prototype浣滅敤鍩?/p>
濡傛灉闇瑕佹瘡嬈¤繑鍥濨ean鐨勬柊瀹炰緥錛屽嵆姣忔閮芥柊寤轟竴涓疄渚嬶紝鍒欏彲浠ラ氳繃璁劇疆scope="prototype"鏉?br />
瀹炵幇銆傝繖鏍鋒垜浠氨鍛婅瘔浜唖pring瀹瑰櫒閲囩敤Prototype浣滅敤鍩熴傞噰鐢╬rototype浣滅敤鍩熸椂錛宻pring瀹瑰櫒鎬諱細
榪斿洖涓涓垱寤虹殑鏂板疄渚嬶紝鍥犳spring瀹瑰櫒涓鏃﹀皢瀹炰緥浜ょ粰瀹㈡埛绔紝灝變笉鍐嶅鍏惰繘琛岃窡韙紩鐢ㄤ簡錛屾墍浠?br />
鏃犳硶瀵筽rototype浣滅敤鍩熺殑Bean瀹氫箟destory-method,涓嶈繃浠嶅彲浠ヤ嬌鐢╥nit-method銆?br />
鍏跺畠涓変釜灞炴у間粎瀵筗EB紼嬪簭鏈夋晥錛岃繖閲屼笉浣滆璁恒?/p>
IoC鍏ㄧОInversion of Control,鐩磋瘧涓烘帶鍒跺弽杞?浣曡皳IoC?鍦ㄨВ閲婃姒傚康涔嬪墠,鎴戜滑鏉ョ湅鐪嬩笅闈㈢殑渚嬪瓙
import ...
public class BookService {
private BookDAO bookDAO = new DbBookDAO();
public List<Book> listBooks(String author) {
List<Book> books = bookDAO.listAll();
return books ;
}
}
鍙互鐪嬪埌,listBooks鏂規硶鐨勫姛鑳介潪甯哥畝鍗?鍗沖垪鍑烘墍鏈変功綾?鑰屽垪鍑烘墍鏈変功綾嶇殑鍔熻兘琚鎵樼粰bookDAO瀵硅薄.
鑰冭檻鍒頒功綾嶅彲鑳藉瓨鍦ㄥ縐嶅艦寮?濡傛暟鎹簱,XML鏂囦歡絳?灝咮ookDAO鐢蟲槑涓烘帴鍙?鍥犳鎴戜滑瀹炵幇浜嗕竴涓叿浣?br />
瀛愮被DbBookDAO.
鐜板湪闇瑕佽冭檻鐨勬槸,BookService濡備綍鎸佹湁bookDAO瀵硅薄.鏈綆鍗曠殑鍔炴硶鏄?鍦˙ookService鐨勫唴閮ㄦ寔鏈?br />
涓涓狣bBookDAO鐨勫疄渚?涓婇潰鐨勪緥瀛愮‘瀹炴槸榪欎箞鍋氱殑.鑰冭檻涓涓嬩細鍙戠幇浠ヤ笅鐨勯棶棰?
(1)鍦˙ookService涓‖緙栫爜鍒涘緩浜咮ookDAO,濡傛灉闇瑕佸彟涓縐岯ookDAO鐨勫疄鐜?鍒欓渶瑕佷慨鏀笲ookService鐨?br />
浠g爜,鎹㈠彞璇濆氨鏄疊ookService緇勪歡涓嶈兘鑴辯BookDAO鐨勫叿浣撳疄鐜?
(2)BookDAO鐨勫疄渚嬫棤娉曡鍏跺畠緇勪歡鍏變韓.鍋囪鍏跺畠鐨勭粍浠朵篃闇瑕佸紩鐢˙ookDAO,鍒欏涓粍浠跺緢闅懼叡浜竴涓?br />
BookDAO瀹炰緥,鍥犱負璇ュ疄渚嬬殑鐢熷懡鍛ㄦ湡瀹氫箟鍦ㄤ簡BookService緇勪歡涓簡,浠庤岄毦浠ュ叡浜?
(3)濡傛灉BookDAO浠嶉渶瑕佸紩鐢ㄥ叾瀹冭祫婧?渚嬪DataSource,鍒橞ookService鍙兘榪橀渶瑕佽礋璐g鐞嗗拰緇存姢涓涓?br />
DataSource,鑰岃繖瀹炰緥涓嶆槸浣滀負涓婂眰緇勪歡BookService鐨勮亴璐?
(4)嫻嬭瘯BookService鏄鏉傜殑,鍥犱負棣栧厛緙栧啓DbBookDAO,鍊樿嫢DbBookDAO榪樹緷璧栦簬DataSource,鏄浉褰撳鏉?
浠庝互涓婂嚑鐐瑰彲浠ョ湅鍑?濡傛灉緋葷粺涓湁澶ч噺鐨勭粍浠?鍏跺畠鐢熷懡鍛ㄦ湡鍜岀浉浜掍箣闂寸殑渚濊禆鍏崇郴,濡傛灉鐢辯粍浠?br />
鑷繁緇存姢,涓嶄絾鍔犲ぇ鐨勭郴緇熺殑澶嶆潅搴?鑰屼笖浼氬鑷寸粍浠朵箣闂寸殑鏋佷負绱у瘑鐨勮﹀悎,緇ц岀粰嫻嬭瘯鍜岀淮鎶ゅ甫鏉ヤ簡
鏋佸ぇ鐨勫洶闅?
鍦↖oC妯″紡涓?鎺у埗鏉冨彂鐢熶簡鍙嶈漿:浠庡簲鐢ㄧ▼搴忚漿縐誨埌浜咺oC瀹瑰櫒.緇勪歡涓嶅啀鐢卞簲鐢ㄧ▼搴忚礋璐e垱寤哄拰閰嶇疆
鑰屾槸鐢卞鍣ㄨ礋璐?搴旂敤紼嬪簭鍙渶鐩存帴浣跨敤宸茬粡鍒涘緩騫墮厤緗ソ鐨勭粍浠?
import ...
public class BookService {
private BookDAO bookDAO ;
public void setBookDAO(BookDAO bookDAO) {
this.bookDAO = bookDAO ;
}
public List<Book> listBooks(String author) {
List<Book> books = bookDAO.listAll();
return books ;
}
}
淇敼鍚庣殑bookDAO瀹炰緥涓嶅啀鐢盉ookService鍒涘緩,鑰屾槸鐢盜oC瀹瑰櫒璐熻矗灝嗘煇涓狟ookDAO瀹炰緥閫氳繃setBookDAO()
娉ㄥ叆.榪欐牱鍋氱殑濂藉:
(1)BookService涓嶅繀鍏沖績濡備綍鍒涘緩BookDAO鐨勫疄渚?涔熶笉蹇呭叧蹇傿ookDAO鐨勫叿浣撳疄鐜?鍙渶瑕佷嬌鐢ㄥ畠灝卞彲浠ヤ簡
鍥犳綆鍖栦簡BookService鐨勭紪鐮?
(2)BookDAO鐨勫疄渚嬬敱IoC瀹瑰櫒綆$悊,鍥犳,鍙互鍦ㄥ涓粍浠朵箣闂村叡浜?鍙瀹冧滑涔熷疄鐜頒簡鐩稿簲鐨剆etBookDAO()
鏂規硶灝卞彲浠ヤ簡.
(3)嫻嬭瘯BookService涔熷彉鎴愬崄鍒嗗鏄撲簡.
IoC瀹瑰櫒璐熻矗瀹炰緥鍖栨墍鏈夌殑緇勪歡,鍥犳闇瑕佸憡璇夊鍣ㄥ浣曞垱寤虹粍浠跺拰鍚勭粍浠朵箣闂寸殑渚濊禆鍏崇郴.
spring鐨処oc瀹瑰櫒鏍規嵁XML閰嶇疆鏂囦歡鏉ュ垵濮嬪寲Bean.闇瑕佹敞鎰忕殑涓鐐規槸,ApplicationContext鍒濆鍖朆ean鍜屽熀
鏈殑BeanFactory鏈夋墍涓嶅悓,鍩烘湰鐨凚eanFactory鎬繪槸寤惰繜鍔犺澆Bean,鐩村埌絎竴嬈¤皟鐢╣etBean("BeanId")鏂規硶
璇鋒眰Bean瀹炰緥鏃?BeanFactory鎵嶄細鍒涘緩榪欎釜Bean,鑰孉pplicationContext鍦ㄨ嚜韜垵濮嬪寲鏃跺氨涓嬈℃у垱寤轟簡
鎵鏈夌殑Bean,浜嗚В榪欎竴鐐規槸闈炲父閲嶈鐨?鍥犱負ApplicationContext鍦ㄥ垵濮嬪寲鏃跺氨鑳介獙璇乆ML閰嶇疆鏂囦歡鐨勬紜?
鑰屼嬌鐢˙eanFactory,鐩村埌璋冪敤getBean("BeanId")鏂規硶鑾峰彇Bean瀹炰緥鏃?鎵嶅彲鑳戒細鍙戠幇閰嶇疆閿欒鑰屽鑷存姏鍑?br />
寮傚父.
鍙湁鍦ㄩ潪甯哥畝鍗曠殑鎯呭喌涓?浣跨敤鍩烘湰鐨凚eanFactory鎵嶅彲鑳芥弧瓚蟲垜浠殑闇姹?緇濆ぇ澶氭椂鍊欐垜浠嬌鐢?br />
ApplicationContext鏄渶浣崇殑閫夋嫨.鍦ㄥ惎鍔ㄧ殑鏃跺欏氨鑳芥嫻嬮厤緗枃浠剁殑閿欒,榪欐瘮浣跨敤鍩烘湰鐨凚eanFactory
鍦ㄨ繍琛屼竴孌墊椂闂村悗璋冪敤getBean("BeanId")鎶涘嚭寮傚父瑕佸ソ寰楀.騫朵笖,寤惰繜鍔犺澆浼氬甫鏉ユц兘涓婄殑鎹熷け.
ApplicationContext鐢變簬鍦ㄥ惎鍔ㄦ椂闇瑕佷竴嬈℃у埆瀹炰緥鍖栨墍鏈夌殑Bean,濡傛灉瀹氫箟鐨凚ean姣旇緝澶?鍒欏惎鍔ㄧ殑鏃墮棿
浼氭瘮杈冮暱.
<script type='text/javascript'>
function regUserChked(){public class CharacterEncodingFilter
Servlet 2.3/2.4 Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form.
This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case ("forceEncoding"="true"). In the latter case, the encoding will also be applied as default response encoding on Servlet 2.4+ containers (although this will usually be overridden by a full content type set in the view).
public interface TimeBookInterface{
public void doAuditing(String name);
}
(2)緙栧啓鎺ュ彛瀹炵幇綾?浠g爜濡備笅
/*******TimeBook.java********/
package com.gc.dao.impl;
public class TimeBook implements TimeBookInterface{
public void doAuditing(String name){
//瀹℃煡鏁版嵁鐨勭浉鍏崇▼搴?br> ......
}
}
(3)緙栧啓涓涓唬鐞嗙被,鐢ㄦ潵瀹炵幇鏃ュ織杈撳嚭,鍏朵唬鐮佸涓?br>/*******TimeBookProxy.java********/
package com.gc.dao.impl;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class TimeBookProxy{
private Logger logger = Logger.getLogger(this.getClass().getName());
private TimeBookInterface timeBookInterface;
public TimeBookProxy(TimeBookInterface timeBookInterface){
this.timeBookInterface = timeBookInterface;
}
//瀹為檯涓氬姟澶勭悊
public void doAuditing(String name){
logger.log(Level.INFO,name+"寮濮嬪鏍告暟鎹?);
timeBookInterface.doAuditing(name);
logger.log(Level.INFO,name+"瀹℃牳鏁版嵁緇撴潫");
}
}
(4)緙栧啓嫻嬭瘯浠g爜綾?br>......
public static void main(String[] args){
//閽堝鎺ュ彛榪涜緙栫▼
TimeBookProxy timeBookProxy = new TimeBookProxy(new TimeBook());
timeBookProxy.doAuditing("寮犱笁");
}
浣跨敤java鐨勪唬鐞嗘満鍒惰繘琛屾棩蹇楄緭鍑?/em>
(1)緙栧啓涓涓棩蹇椾俊鎭殑浠g悊綾籐ogProxy,榪欎釜浠g悊綾誨疄鐜頒簡鎺ュ彛InvocationHandler,
鍙互瀵逛換浣曟帴鍙e疄鐜版棩蹇椾俊鎭殑杈撳嚭.鍏朵唬鐞嗗涓?br>package com.gc.dao;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
//浠g悊綾誨疄鐜頒簡鎺ュ彛InvocationHandler
public class LogProxy implements InvocationHandler{
private Logger logger = Logger.getLogger(this.getClass().getName());
private Object delegate;
//緇戝畾瀵硅薄
public Object bing(Object delegate){
this.delegate = delegate;
return Proxy.new ProxyInstance(delegate.getClass().getClassLoader(),delegate.getClass().getInterfaces(),this);
}
//閽堝鎺ュ彛緙栫▼
public Object invoke(Object proxy,Mehtod method,Object[] args)throws Throwable{
Object result = null;
try{
//鍦ㄦ柟娉曡皟鐢ㄥ墠鍚庤繘琛屾棩蹇楄緭鍑?br> logger.log(Level.INFO,args[0] + "寮濮嬪鏍告暟鎹?);
result = method.invoke(delegate,args);
logger.log(Level.INFO,args[0] + "瀹℃牳鏁版嵁緇撴潫");
}
catch(Exception e){
logger.log(Level.INFO,e.toString());
}
return result;
}
}
(2)寤虹珛涓涓帴鍙imeBookInterface,浠g爜濡備笅
/*******TimeBookInterface.java********/
package com.gc.dao;
public interface TimeBookInterface{
public void doAuditing(String name);
}
(3)緙栧啓鎺ュ彛瀹炵幇綾?浠g爜濡備笅
/*******TimeBook.java********/
package com.gc.dao.impl;
public class TimeBook implements TimeBookInterface{
public void doAuditing(String name){
//瀹℃煡鏁版嵁鐨勭浉鍏崇▼搴?br> ......
}
}
(4)緙栧啓嫻嬭瘯浠g爜綾?br>......
public static void main(String[] args){
//瀹炵幇浜嗗鏃ュ織綾葷殑閲嶇敤
LogProxy logProxy = new LogProxy();
TimeBookInterface timeBookInterface = (TimeBookInterface)logProxy.bind(new TimeBook());
timeBookProxy.doAuditing("寮犱笁");
}
浠g爜鏉ヨ嚜spring浠庡叆闂ㄥ埌綺鵑氫竴涔?/p>
List
<bean id="helloWorld" class="...">
<property name="message">
<list>
<value>one</value>
<value>two</value>
<value>three</value>
</list>
</property>
</bean>
Set
<bean id="helloWorld" class="...">
<property name="message">
<set>
<value>one</value>
<value>two</value>
<value>three</value>
</set>
</property>
</bean>
Map
<bean id="helloWorld" class="...">
<property name="message">
<map>
<entry key="keytwo">
<value>one</value>
</entry>
<entry key="keytwo">
<value>two</value>
</entry>
</map>
</property>
</bean>
Properties
<bean id="helloWorld" class="...">
<property name="message">
<props>
<prop key="keyone">one</prop>
<prop key="keytwo">two</prop>
</props>
</property>
</bean>
瀵瑰簲鐨刯ava綾諱負:
import java.util.TimerTask;
public class MainTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("--------------鎵ц瀹氭椂浠誨姟---------------");
}
}
------------------------------------------------------------------------------------------------------------
鏂囦歡/WEB-INF/TimerConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "<beans>
<!-- 娉ㄥ唽瀹氭椂鎵ц瀹炰綋 -->
<bean id="mainTask" class="dgut.ke.timer.MainTask"></bean>
<!-- 娉ㄥ唽瀹氭椂淇℃伅 -->
<bean id="stTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- 棣栨鎵ц浠誨姟鍓嶉渶瑕佺瓑寰?縐掗挓 -->
<property name="delay">
<value>2000</value>
</property>
<!-- 浠誨姟鎵ц鐨勫懆鏈?-->
<property name="period">
<value>4000</value>
</property>
<!-- 鍏蜂綋鐨勬墽琛屼換鍔?-->
<property name="timerTask">
<ref local="mainTask"/>
</property>
</bean>
<!-- 閰嶇疆浠誨姟璋冨害鍣?-->
<bean id="timeFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<!-- 娉ㄥ叆瀹氭椂鍣ㄥ垪琛?-->
<property name="scheduledTimerTasks">
<list>
<ref local="stTask"/>
</list>
</property>
</bean>
</beans>
------------------------------------------------------------------------------------------------------------
import java.util.Calendar;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class NotLeaveWord extends HandlerInterceptorAdapter
{
private int startTime;
private int endTime;
private String interceptorPage;
/*閫氳繃渚濊禆娉ㄥ叆*/
public void setStartTime(int startTime) {
this.startTime = startTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
public void setInterceptorPage(String interceptorPage) {
this.interceptorPage = interceptorPage;
}
public int getStartTime() {
return startTime;
}
public int getEndTime() {
return endTime;
}
public String getInterceptorPage() {
return interceptorPage;
}
/*榪涜鎷︽埅澶勭悊*/
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler)throws Exception
{
Calendar cal = Calendar.getInstance();
int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
if( hour > startTime && hour < endTime)
{
request.setAttribute("startTime", this.getStartTime());
request.setAttribute("endTime", this.getEndTime());
RequestDispatcher rd = request.getRequestDispatcher(this.getInterceptorPage());
rd.forward(request, response);
return false;
}
else
{
return true ;
}
}
}
-----------------------------------------------------------------------------------------------------------------