锛?锛?spring 浣撶郴缁撴瀯
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了锛?锛?spring 浣撶郴缁撴瀯相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95' title='瑙e喅鏂规硶'>瑙e喅鏂规硶
odi 鍏朵粬 xsd err 鐜板湪 闈㈠悜 ext nal涓€銆係pring妗嗗浘锛?/h2>
1.杩欎釜妗嗘灦鍥炬槸浠庝笅寰€涓婄湅鐨勶紝鍙互鐪嬪嚭娴嬭瘯鐨勯噸瑕佹€с€?/p>
2.servlet浼氳繘琛屽鍣ㄧ鐞嗭紝controler锛屼笉浼氬啀鏈夐噸瀹氬瀷鍜岃烦杞晫闈紝鍙細杩涜浼犺緭json鏁版嵁銆傚彲浠ヨВ鍐冲绔棶棰樸€?/p>
3.鍦ㄦ祴璇曠殑鍩虹涓婏紝鏍稿績瀹瑰櫒Core Container瀵硅薄锛屼袱涓綔鐢紝涓€锛氬彲浠ュ府鎴戜滑鐢熸垚鎺т欢锛屼簩锛屽彲浠ュ府鎴戜滑缁存姢瀵硅薄涔嬮棿鐨勫叧绯汇€?/p>
4.瀹瑰櫒甯垜浠垱寤虹殑瀵硅薄鏄甫鏈変笂涓嬫枃锛坈ontext锛夌殑锛屽氨鏄繍琛岀幆澧冿紝servlet璁块棶涓婁笅鏂囨槸閫氳繃application锛宎pplication鏄痵ervletcontext鐨勩€?/p>
5.Core Container涔熷彨鍋歩oc锛氭帶鍒跺弽杞紝涔熷彨鍋氭敞鍏ヤ緷璧栵紱
6.
Spring鏄潰鐩窧ean鐨勭紪绋?/p>
Spring鐨勪紭鐐癸細
- 浣庝镜鍏ュ紡璁捐銆傦紙1.涓嶇敤缁ф壙Spring浠讳綍鐨勭埗绫汇€?.鍦ㄥ鍒癝pringMVC鏃讹紝鎴戜滑瑕佷娇鐢–ontroler浠f浛servlet銆?.Controler灏辨槸javaBean锛?/li>
- 鐙珛浜庡悇绉嶅簲鐢ㄦ湇鍔″櫒
- 娉ㄥ叆渚濊禆鐗规€х粍浠跺叧绯婚€忔槑鍖栵紝闄嶄綆鑰﹀悎搴?/li>
- 闈㈠悜鍒囬潰缂栫▼鐗瑰緛鍏佽灏嗛€氱敤浠诲姟杩涜闆嗕腑寮忓鐞?/li>
- 涓庣涓夋柟妗嗘灦鐨勮壇濂芥暣鍚?/li>
ioc锛氭帶鍒跺弽杞篃绉颁负渚濊禆娉ㄥ叆
鎺у埗鍙嶈浆鐨勬寮忓畾涔夛細灏嗗璞$殑鎺у埗鏉冧粠浠g爜鏈韩杞Щ鍒板閮ㄥ鍣ㄣ€?/strong>
鎺у埗鍙嶈浆锛氬師鏈嚜宸辨墜宸ew瀵硅薄鐜板湪杞崲涓哄鍣ㄥ府蹇欏垱寤猴紝鎵嬪姩鎺у埗鍙樹负瀹瑰櫒鎺у埗銆傚浜哠Pring涔嬪悗瀛︿細璁╁鍣ㄥ垱寤哄璞★紝鎶婅嚜宸卞垱寤哄璞′氦缁欏鍣ㄧ鐞嗐€?/p>
渚濊禆娉ㄥ叆锛氭垜浠渶瑕佷娇鐢ˋ鏂规硶锛岃€屼娇鐢ˋ鏂规硶闇€瑕佸疄渚嬪寲骞惰皟鐢˙鏂规硶锛屾垜浠A绫讳緷璧朆绫汇€備絾鏄垱寤哄拰璋冪敤B鍧囨槸鐢卞鍣ㄥ畬鎴愮殑锛岃€屾垜浠笉鍏冲績B锛屾垜浠彧鍏冲績A锛岃繖涓氨鏄疊灏辨槸娉ㄥ叆銆?/p>
缁勪欢鍖栫殑鎬濇兂锛氬垎绂诲叧娉ㄧ偣锛屼娇鐢ㄦ帴鍙o紝涓嶅啀鍏虫敞瀹炵幇銆傝鐐癸細鏄庣‘瀹氫箟缁勪欢闂寸殑鎺ュ彛銆?/p>
Aop:闈㈠悜鍒囬潰缂栫▼銆?/h3>
Aop鐨勭洰鏍?璁╂垜浠彲浠?ldquo;涓撳績鍋氫簨”
Aop鐨勫師鐞嗭細
- 灏嗗鏉傜殑闇€姹傚垎瑙e嚭涓嶅悓鐨勬柟闈紝灏嗘暎甯冨湪绯荤粺涓殑鍏叡鍔熻兘闆嗕腑瑙e喅銆傦紙鎶藉嚭鍏叡鍔熻兘褰㈡垚鍒囬潰锛?/li>
- 閲囩敤浠g悊鏈哄埗缁勮璧锋潵杩愯锛屽湪涓嶆敼鍙樺師绋嬪簭鐨勫熀纭€涓婂浠g爜娈佃繘琛?strong>澧炲己澶勭悊锛屽鍔犳柊鐨勫姛鑳姐€傦紙澧炲己锛氭妸鎶藉嚭鏉ョ殑鍔熻兘鍐嶆斁鍥炲幓锛夛紙閫氳繃浠g悊瀵硅薄鏉ヨ皟鐢ㄥ璞$殑鏂规硶锛屼唬鐞嗗璞″墠鍚庨兘鍙彃鍏ヤ唬鐮侊紝杩欎簺浠g爜灏辨槸澧炲己澶勭悊銆傦級
- 鎵€璋撻潰鍚戝垏闈細鏄互涓€绉嶉€氳繃棰勭紪璇?/strong>鍜?strong>杩愯鏈熷姩鎬佷唬鐞?/strong>鐨勬柟寮忓疄鐜板湪涓嶄慨鏀规簮浠g爜鐨勬儏鍐典笅缁欑▼搴忓姩鎬佹坊鍔犲姛鑳界殑鎶€鏈€?/li>
- 涓庨潰鍚戝垏闈㈢殑涓€浜?strong>鐩稿叧鏈
锛氬寮哄鐞嗭紙Advice锛夛紙鏈夊墠缃寮猴紝鍚庣疆澧炲己锛岀幆缁曞寮猴紝寮傚父鎶涘嚭澧炲己锛屾渶缁堝寮虹瓑绫诲瀷銆傦級锛屽垏鍏ョ偣锛圥ointcut锛?杩炴帴鐐癸紙Join Point锛夛紝鍒囬潰锛圓spect锛夛紝鐩爣瀵硅薄锛圱arget objec锛夛紝Aop浠g悊锛圓OP proxy锛?缁囧叆锛圵eaving锛?
AOP闈㈠悜鍒囬潰锛?.鏁e竷鍦ㄤ笟鍔$郴缁熶腑鐨勫叡鎬у唴瀹瑰彇鍑烘潵褰㈡垚鍒囬潰锛?/p>
銆€銆€銆€銆€銆€銆€銆€銆€2.閫氳繃棰勭紪璇?浠g悊鎶€鏈湪绋嬪簭杩愯鏃跺皢鍒囬潰鍐呭鍒囧叆涓氬姟绯荤粺銆?nbsp;
Spring寮€鍙戝紑鍙戞祦绋嬶細
涓€銆傚垱寤烘柊鐨勪竴涓ā鍧楋紝鐒跺悗寮曞叆jar鍖咃紙all鎵€鏈夌殑jar鍖咃級锛?/p>
鐒跺悗璁板緱鍦ㄩ」鐩粨鏋勪腑娣诲姞椤圭洰鐨刯ar鍖呬互鍙妕omcat鏈嶅姟鍣ㄣ€?/p>
褰撴垜浠妸涔嬪墠鐨勬墍鏈夊紩鍏ュ埌鏂扮殑spring01妯″潡鐨勬椂鍊欙紝鍙兘浼氭姤閿欙紝姝ゆ椂鐨勫師鍥犲彲鑳芥槸lib鏈紩鍏ャ€?/p>
姝ゆ椂鍙充笅瑙掓湁涓€涓猣ix(淇)锛岀偣鍑?ldquo;搴旂敤”锛岀劧鍚巘omcat鏈嶅姟鍣ㄦ坊鍔犺繖涓」鐩紝椤圭洰鍙兘浼氭姤閿欙紝濡傛灉鎶ラ敊锛岃繍琛屼箣鍚庡啀鐐瑰嚮椤圭洰鍙抽敭閫夋嫨redeploy銆?/p>
浜岋細閫氳繃瀹炰緥澧炲姞瀵筍pring鐨勭悊瑙o細
1.1鍒涘缓涓€涓狿erson绫伙紝瑕唗oString鏂规硶.
package com.service; public class Person { private String name; @Override public String toString() { return "鎴戠殑鍚嶅瓧鏄細" + name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
1.2鍐嶅垱寤轰竴涓猦elloPerson,姣忎釜HellowPerson绫诲綋涓兘鏈変竴涓狿erson绫汇€?/p>
package com.service; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloPerson { private Person person; public void show() { System.out.println(person); } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public static void main(String[] args) { //<bean class="com.service.Person" id="person"> Person person = new Person(); person.setName("榻愬痉榫?); HelloPerson helloPerson = new HelloPerson(); helloPerson.setPerson(person); // <property name="person" ref="person"/> // 璋冪敤helloPerson鐨勬柟娉?/span> helloPerson.show(); // 鍒涘缓宸ュ巶锛屼笅闈㈡槸閫氳繃spring瀹屾垚锛屼笂闈㈡槸甯歌鍒涘缓 ApplicationContext ctx = new ClassPathXmlApplicationContext("core.xml"); // 浠庡伐鍘傝幏鍙栧璞?/span> HelloPerson hp = (HelloPerson) ctx.getBean("helloPerson"); hp.show(); } }
1.3鍒涘缓spring閰嶇疆鏂囦欢銆傦紙鍦╯rc涓嬪垱寤轰竴涓獂ml涓殑spring config閰嶇疆鏂囦欢 core.xml锛夈€€
鍒氬垱寤烘椂鏄繖鏍风殑锛?/p>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
瑙i噴锛歜ean涓涓€琛岋細xmlns鏄懡鍚嶇┖闂淬€傛槸涓婚厤缃紝鍙互鍦ㄦ祻瑙堝櫒涓墦寮€缃戝潃锛屾槸涓€浜涢厤缃ā鏉裤€傞粯璁ょ殑鍛藉悕绌洪棿
銆€銆€銆€銆€绗簩琛岋細
銆€銆€銆€銆€绗笁琛岋細鍚庨潰缃戝潃鏄褰曠殑鐗堟湰鍙凤紝鍙互鍙傝€冩ā鏉匡紝濡傛灉涓嶇煡閬撶増鏈彿锛屽彲浠ョ洿鎺pring-beans.xsd銆倄sd瀹氫箟鏍囩鐨勪綔鐢ㄣ€?/p>
寮曞叆鍏朵粬鍛藉悕绌洪棿姝ラ锛?/p>
銆€銆€1.鍦ㄧ浜岃涓嬶紝寮曞叆鎵€闇€瑕佺殑鍛藉悕绌洪棿锛宔g:xmlns="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/mvc"
銆€銆€2.鍦ㄧ涓夎閲屽紩鍏ワ細eg:http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd(榛戣壊鏄渶鏇存敼)
涔嬪悗鎴戜滑瀹屾垚娉ㄥ叆渚濊禆涓庢帶鍒跺弽杞細
eg:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/context/spring-mvc.xsd"> <bean class="com.service.Person" id="person"> <property name="name" value="閽变笢寮?/> </bean> <bean class="com.service.HelloPerson" id="helloPerson"> <property name="person" ref="person"/> </bean> </beans>
璇存槑锛氫竴涓猵roperty瀵瑰簲涓€涓睘鎬э紝鐢变簬name鏄痵tring绫诲瀷锛屼娇鐢╲alue銆傝€岀被绫诲瀷鏄紩鐢ㄧ被鍨嬶紝鍙傛暟绫诲瀷璁句负ref;
鑰屽湪spring涓垜浠兂瑕佽皟鐢紝棣栧厛闇€瑕佸垱寤哄伐鍘傦細锛堟敞鎰忚繖閲岀殑bean瀹炰緥鍖栧弬鏁颁笌xml鏂囦欢涓璪ean鐨刬d鏄竴鑷寸殑锛?/p>
ApplicationContext ctx = new ClassPathXmlApplicationContext("core.xml"); // 浠庡伐鍘傝幏鍙栧璞?/span> HelloPerson hp = (HelloPerson) ctx.getBean("helloPerson"); hp.show();
宸╁浐瀵箂pring鐨勭悊瑙o細
spring涓厤缃繃鐨勭被閮藉彲浠ラ€氳繃宸ュ巶杩涜浣跨敤锛?/p>
Spring宸ュ巶锛氥€€銆€
銆€銆€1.ClasspathXmlApplicationContext classpath涓悳绱pring閰嶇疆鏂囦欢
銆€銆€2.FileSystemXmlApplicationContext 鏂囦欢绯荤粺
銆€銆€3.XmlWebApplicationContext web
Bean鏍囩锛氳繘琛屾帶鍒跺弽杞紝骞朵笖鍙互鎺у埗浣滅敤鍩?閫氳繃scope锛氭湁鍥涗釜鍙栧€笺€傚崟渚嬶細
銆€銆€1.singleton锛堟墍鏈夌殑閮芥槸鍚屼竴涓紝榛樿锛?/p>
銆€銆€2.prototype锛堟瘡娆¤鍙栭兘浼氳繑鍥炴柊鐨勶紝鍍忔瘡娆ew鏂板璞′竴鏍凤級
銆€銆€3.request 锛堜竴涓姹備竴涓紝鍦╳eb涓嬫墠鏈夋晥鏋滐級
銆€銆€4.session 锛堜竴涓細璇濆唴鍚屼竴涓級
AOP:闈㈠悜鍒囬潰缂栫▼锛?/h3>
1.浣跨敤Spring AOP瀹炵幇鏃ュ織杈撳嚭,姝ラ:
- 鍦ㄩ」鐩腑娣诲姞Spring AOP鐨刯ar鏂囦欢
- 缂栧啓鍓嶇疆澧炲己鍜屽悗缃寮哄疄鐜版棩蹇楀姛鑳?/li>
- 缂栧啓Spring閰嶇疆鏂囦欢锛屽涓氬姟鏂规硶杩涜澧炲己澶勭悊
- 缂栧啓浠g爜鑾峰彇甯︽湁澧炲己澶勭悊鐨勪笟鍔″璞?/li>
銆€銆€瀵瑰簲绗簩姝ワ細锛堟湰渚嬪瓙鏄墦鍗版満锛屽搴擯rint绫诲綋涓殑print鏂规硶锛?nbsp;
package com.service; import org.aspectj.lang.JoinPoint; import java.util.Arrays; import java.util.logging.Logger; // 鏃ュ織鍒囬潰 public class LogAdvice { static final Logger log = Logger.getLogger("LogAdvice"); // 鍓嶇疆澧炲己 public void before(JoinPoint jp){ //JoinPoint寮曞叆鐐? String info = String.format("寮€濮?mdash;—鐩爣瀵硅薄锛?s——鐩爣鏂规硶锛?s——鍙傛暟鍒楄〃锛? + "%s",jp.getTarget(),jp.getSignature().getName(), Arrays.toString(jp.getArgs())); log.info(info); } // 鍚庣疆澧炲己 public void after(JoinPoint jp, Object result){ String info = String.format("缁撴潫——鐩爣瀵硅薄锛?s——鐩爣鏂规硶锛?s——鍙傛暟鍒楄〃锛? + "%s——杩斿洖鍊硷細%s",jp.getTarget(),jp.getSignature().getName(), Arrays.toString(jp.getArgs()),result); log.info(info); } }
銆€銆€瀵瑰簲绗?姝ワ細姒傚康锛?/p>
銆€銆€瀹炰緥锛?/p>
<bean class="com.service.Printer" id="printer"><!--鎺у埗鍙嶈浆--> <property name="ink"> <!--渚濊禆娉ㄥ叆--> <bean class="com.service.ColorInk"/> </property> <property name="pagers"> <list> <bean class="com.service.A4Paper"/> <bean class="com.service.B5Paper"/> </list> </property> </bean> <!--瀹氫箟鍒囬潰瀵硅薄--> <bean class="com.service.LogAdvice" id="logAdvice"/> <!--鍒囬潰閰嶇疆--> <aop:config> <aop:pointcut id="pc" expression="execution(* com.service.Printer.print(..))"/> <aop:aspect ref="logAdvice"><!--寮曞叆鍒囬潰--> <aop:before method="before" pointcut-ref="pc"/><!--鏂规硶锛岄厤缃垏鍏ョ偣--> <aop:after-returning method="after" pointcut-ref="pc" returning="result"/> </aop:aspect> </aop:config>
鍗曞厓娴嬭瘯锛?/p>
package com.service; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class PrinterTest { static ApplicationContext ctx = null; @BeforeClass public static void load(){ ctx = new ClassPathXmlApplicationContext("core.xml"); } @Test public void print(){ IPrinter printer = (IPrinter) ctx.getBean("printer"); printer.print("杩欐槸涓€娈佃鎵撳嵃鐨勮瘽锛侊紒锛?,3, Paper.A4); printer.print("杩欐槸涓€娈佃鎵撳嵃鐨勮瘽锛侊紒锛?,3, Paper.B5); } }
鍦╣etbean浣跨敤浠g悊澧炲己鐨勬椂鍊欙紝杩斿洖鐨勫簲璇ユ槸print鐨勫瓙绫伙紝濡傛灉鍔ㄦ€佺敓鎴愪粬鐨勫瓙绫伙紝閭d箞搴旇寮曞叆涓€浜涚被搴撱€傝В鍐虫柟娉曪細鎴戜滑鍙互缁欐墦鍗版満print鍒涘缓涓€涓帴鍙o紝鐒跺悗鎶妏rint瀹炵幇鎺ュ彛锛屾槸鍏朵腑涓€绉嶉潰鍚戝垏闈㈢殑鏂规硶銆?/p>
鎺ュ彛锛?/p>
package com.service; public interface IPrinter { void print(String msg, int count, Paper.Size size); }
缁囧叆锛氬皢閫氱煡涓庡墠缃寮轰笌鍚庣疆澧炲己鐩搁厤缃氨鏄粐鍏ャ€?/p>
鍦ㄤ笂闈腑鍏充簬鎺ュ彛鐨勯棶棰橈紝鍏跺疄鏄娇鐢ㄤ簡浠g悊妯″紡锛屾槸鍔ㄦ€佺紪璇戯紝濡備笅涓洪潤鎬佺殑锛屽彲渚涘弬鑰冿細
package com.service; public class $Proxy5Test implements IPrinter { private IPrinter target; // 鐩爣瀵硅薄 private LogAdvice logAdvice; // Advice @Override public void print(String msg, int count, Paper.Size size) { // 1銆佹墽琛屽墠 logAdvice.before(null); // 2銆佹墽琛?/span> target.print(msg,count,size); // 3銆佹墽琛屽悗 logAdvice.after(null,null); } }
鑷充簬濡傛灉閫氳繃棰勭紪璇戝鐞嗭紝鍒欎細閫氳繃淇敼婧愪唬鐮佽繘琛屽鐞嗐€?/p>
以上是关于锛?锛?spring 浣撶郴缁撴瀯的主要内容,如果未能解决你的问题,请参考以下文章