



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
spring4spring4中文技術(shù)手冊在《靜態(tài)代理和動態(tài)代理》中提到了面對方面編程,主要就是基于動態(tài)代理。單獨抽象出非業(yè)務(wù)的功能,效勞于某些業(yè)務(wù)方法。SpringAdvice,分別為:BeforeAdvice,AfterReturningAdvice,AroundAdvice,AfterthrowingAdvice。都是方法級別的,就是在某個方法執(zhí)行前后插入一些非業(yè)務(wù)的操作,如打日志或者推斷權(quán)限等。advice的實現(xiàn),springxmlannotation(注釋)。BeforeAdviceAfterAdviceAroundAdvice則可以在目標(biāo)方法執(zhí)行前后同時加上相關(guān)效勞;ThrowAdvice是在特別發(fā)生后執(zhí)行某些操作。1.AdviceAspectSpring接口。BeforeAdviceorg.springframework.aop.MethodBeforeAdvice接口:1. 1. /**2.*Adviceinvokedbeforeamethodisinvoked.Suchadvicescannot3.*preventthemethodcallproceeding,unlesstheythrowaThrowable.4.*/5. publicinterfaceMethodBeforeAdviceextendsBeforeAdvice{6.7./**8.*Callbackbeforeagivenmethodisinvoked.9.*@parammethodmethodbeinginvoked10.*@paramargsargumentstothemethod11.*@paramtargettargetofthemethodinvocation.Maybe<code>null</code>.12.*@throwsThrowableifthisobjectwishestoabortthecall.13.*Anyexceptionthrownwillbereturnedtothecallerifit”s14.*allowedbythemethodsignature.Otherwisetheexception15.*willbewrappedasaruntimeexception.16.*/17.voidbefore(Methodmethod,Object[]args,Objecttarget)throwsThrowable;18.}AfterAfterAdviceorg.springframework.aop.AfterReturningAdvice接口:1. /**2.2.*Afterreturningadviceisinvokedonlyonnormalmethodreturn,notifan3.*exceptionisthrown.Suchadvicecanseethereturnvalue,butcannotchangeit.4.*/5. publicinterfaceAfterReturningAdviceextendsAfterAdvice{6./**7.*Callbackafteragivenmethodsuccessfullyreturned.8.*@paramreturnValuethevaluereturnedbythemethod,ifany9.*@parammethodmethodbeinginvoked10.*@paramargsargumentstothemethod11.*@paramtargettargetofthemethodinvocation.Maybe<code>null</code>.12.*@throwsThrowableifthisobjectwishestoabortthecall.13.*Anyexceptionthrownwillbereturnedtothecallerifit”s14.*allowedbythemethodsignature.Otherwisetheexception15.*willbewrappedasaruntimeexception.16.*/17.voidafterReturning(ObjectreturnValue,Methodmethod,Object[]args,Objecttarget)throwsThrowable;18.}AroundAroundAercept.MethodInterceptor接口:1. /**2.*<p>Theusershouldimplementthe{@link#invoke(MethodInvocation)}3.*methodtomodifytheoriginalbehavior.E.g.thefollowingclass4.*implementsatracinginterceptor(tracesallthecallsonthe5.*interceptedmethod(s)):6.*7.*<preclass=code>8.*classTracingInterceptorimplementsMethodInterceptor{9.* Objectinvoke(MethodInvocationi)throwsThrowable{10. *System.out.println(“method“+i.getMethod+“iscalledon“+11. *i.getThis+“withargs“+i.getArguments);12. *Objectret=ceed;13. *System.out.println(“method“+i.getMethod+“returns“+ret);14. *returnret;15. 15. * }16. *}17. *</pre>*/18.publicinterfaceMethodInterceptorextendsInterceptor{19.20./**21.*Implementthismethodtoperformextratreatmentsbeforeand22.*aftertheinvocation.Politeimplementationswouldcertainly23.*liketoinvoke{@linkJoinpoint#proceed}.24.*25.*@paraminvocationthemethodinvocationjoinpoint26.*@returntheresultofthecallto{@link27.*Joinpoint#proceed},mightbeinterceptedbythe28.*interceptor.29.*30.*@throwsThrowableiftheinterceptorsorthe31.*target-objectthrowsanexception.*/32.Objectinvoke(MethodInvocationinvocation)throwsThrowable;33.}類前面的注釋說明白該方法的使用,就是要在類前面的注釋說明白該方法的使用,就是要在invoke方法中調(diào)用MethodIceed,將執(zhí)行傳給下一個Interceptor,最終執(zhí)行目標(biāo)方法。在proceed方法前后加操作,到達(dá)Aroudadvice的作用。Aspectbeanorg.springframework.aop.framework.ProxyFactoryBeanAspect。如下:1. <beanid=“helloProxy“class=“org.springframework.aop.framework.ProxyFactoryBean“>2.業(yè)務(wù)接口-->3.<propertyname=“proxyInterfaces“value=“spring.advice.IHello“/>4.實現(xiàn)該接口的目標(biāo)類-->5.<propertyname=“target“ref=“helloSpeaker“/>6.Aspect7.<propertyname=“interceptorNames“>8.<list>9.<value>throwAdvice</value>10.還可以連續(xù)加,如11.<value>logBeforeAdvice</value>-->12.12.</list>13.</property>14.</bean>補充下,其中的目標(biāo)類、解釋器(Aspect)Bean定義文件中先進(jìn)展定義,然后才可以引用的。我們使用時,代碼如下:IHelloIHellohelloProxy=(IHello)context.getBean(“helloProxy“);helloProxy.hello;BeanBeanAspectjoinPoint應(yīng)用到目標(biāo)方法上。補:IHellohello方法,HelloSpeakerIHello接口。hello方法前后參與日志記錄。1. publicclassAroundAdviceimplementsMethodInterceptor{2.privateLoggerlogger=3.4.@Override5.publicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable{6.7.logger.log(Level.INFO,“methodstarts...“+methodInvocation.getMethod);8.result=methodIceed;9.ends...“+methodInvocation.getMethod);10.11.returnresult;12.}13.}配置:<?xml<?xmlversion=“1.0“encoding=“UTF-8“?>3.4.>5.<beanid=“helloSpeaker“class=“spring.advice.HelloSpeaker“/>IHelloIHellohelloProxy=(IHello)context.getBean(“helloProxy“);helloProxy.hello(“前后加日志“);就會消滅如下結(jié)果:6.<beanid=“aroundAdvice“class=“spring.advice.AroundAdvice“/>7.8.<beanid=“helloProxy“class=“org.springframework.aop.framework.ProxyFact
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 低年級數(shù)學(xué)組教研總結(jié)范文(14篇)
- 小學(xué)英語冀教版 (一年級起點)六年級下冊Lesson 17 Summer camp in Canada教案及反思
- 將養(yǎng)成學(xué)生核心素養(yǎng)融入實驗教學(xué)的“五以”策略
- 北京市大學(xué)生村干部聘用合同書(7篇)
- 中考的演講稿400字(15篇)
- 軍訓(xùn)正步心得(4篇)
- 小學(xué)畢業(yè)典禮主持稿(19篇)
- 新軍訓(xùn)閉幕式演講稿(11篇)
- 小學(xué)政治思品人教部編版六年級上冊(道德與法治)1 感受生活中的法律教案
- 小學(xué)新上崗教師培養(yǎng)計劃范文(4篇)
- 民航貴州監(jiān)管局制員工招聘筆試真題2023
- 2022版義務(wù)教育(歷史)課程標(biāo)準(zhǔn)(附課標(biāo)解讀)
- 天津市保溫裝飾板外墻外保溫系統(tǒng)技術(shù)規(guī)程
- 《 大學(xué)生軍事理論教程》全套教學(xué)課件
- CJT 526-2018 軟土固化劑 標(biāo)準(zhǔn)
- 品質(zhì)提升計劃改善報告課件
- NB-T10208-2019陸上風(fēng)電場工程施工安全技術(shù)規(guī)范
- 《跟上兔子》繪本五年級第1季A-Magic-Card
- 在線網(wǎng)課知慧《形勢與政策(吉林大學(xué))》單元測試考核答案
- 三年級必讀書課外閱讀測試(附答案)
- 市人民醫(yī)院檢驗科程序文件資料匯編
評論
0/150
提交評論