版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
AOPOOPSpring重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利AOP部分進(jìn)行各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了AOPOOPSpring重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利AOP部分進(jìn)行各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了1.1主要功能日志記錄,性能統(tǒng)計(jì),安全控制,事務(wù)處理,異常處理1.2主要目標(biāo)將日志記錄,性能統(tǒng)計(jì),安全控制,事務(wù)處理,異常處理碼中劃分出來,通過對(duì)這些行這些行為的時(shí)候不1.3適用對(duì)象OOP力有日志、性能、安全、異常處理1.4AOPOOP裝,以獲得更不是單一的類,不OOP不是單一的類,不OOP是處理AOP不OOP了OOP不足。OOP了問題,AOPAOP.1意思就是意思就是ajc?Java?.ajaspectj在Java編譯?的基礎(chǔ)上增加了ajc?Java?.ajaspectj在Java編譯?的基礎(chǔ)上增加了weaver織入?:為了在java編譯?上使用AspectJ而不Ajc?,aspectJ5了@AspectJ,AspectJJava?AndroidStudioajc?Android@AspectJ(SpringAOP)目標(biāo)程序,根據(jù)切點(diǎn)(PointCut)Aspect(Weave)到目標(biāo)程序的.class了重構(gòu)(象、方法、參數(shù)等上下文信息),AOP2.1Gradle配置示AspectJAndroidGradle行classpath'org.aspectj:aspectjtools:1.8.9'import行classpathcompileimportorg.aspectj.bridge.IMessageimportorg.aspectj.bridge.IMessageimportorg.aspectj.bridge.MessageHandlerimportorg.aspectj.tools.ajc.Main//gradleandroid.libraryVariants.all{variantLibraryPluginplugin=project.plugins.getPlugin(LibraryPlugin)JavaCompilejavaCompile=variant.javaCompilejavaCompile.doLast{String[]args=["showWeaveInfo","inpath",javaCompile.destinationDir.toString(),"aspectpath",javaCompile.classpath.asPath,"d",javaCompile.destinationDir.toString(),"classpath",javaCompile.classpath.asPath,MessageHandlerhandler=newMessageHandler(true)?newMain().run(args,handler)deflog=for(IMessagemessage:handler.getMessages(null,true)){switch(message.getKind()){casecasecaselog.errormessage.message,message.thrownmessage.message,message.throwncaselog.debugmessage.message,message.thrown2.2基本概念2.2.1切面——實(shí)現(xiàn)crosscuttinglogging2.2基本概念2.2.1切面——實(shí)現(xiàn)crosscuttinglogging法執(zhí)行AOP了,我們可以針對(duì)不同的需求做出不//aspectj{disableWhenDebugtruejavartNeededtrueexcludegroup:'xxxx',module:'xxxx'compileOptions{defaultJavaVersion=buildscript{repositories{dependenciesclasspath'com.meituan.gradle:roboaspectj:0.9.2'classpath'jaop.gradle.plugin:gradleplugin:1.0.2'//Excludetheversionthattheandroidplugindepends2.2.2連接點(diǎn)——findInfo(String)AspectJ行JoinPoint2.2.2連接點(diǎn)——findInfo(String)AspectJ行JoinPoint行下面列AspectJjoinpoint2.2.3切點(diǎn)——pointcutadvicejointpoint達(dá)式來進(jìn)行匹配應(yīng)用,決定了jointpointcallexecution、target、this、within達(dá)式來進(jìn)行匹配應(yīng)用,決定了jointpointcallexecution、target、this、within1.JoinPointpointcutsJoinpointJPointJPoint,每一個(gè)函數(shù)執(zhí)行找到目標(biāo)的表2列出了JPoint表2列出了JPoint使用且call(<列arg()call(<列arg()@Around(value="execution(*BitmapFacade.picasso.init(java.lang.String,java.lang.String))&&publicObjectaroundArgs(Stringarg1,Stringarg2,ProceedingJoinPointjoinPoint){System.out.println("aspectsarg="+arg1.toString()+""+arg2)?ObjectresutObject=null?try{resutObject=joinPceed(new}catch(Throwablee){@Pointcut("execution(**..Activity+.onStart(..))&&within(com.meituan.hotel.roadmap.*)")publicvoidonStart(){}@Pointcut("call(@Describepublicvoidcom.davidkuper.MainActivity.*(..))")publicvoidpointCut(){}@Pointcut("call(*android.widget.Toast+.show(..))&&(within(com.meituan..*)||publicvoidtoastShow()@Pointcut("call(@Describepublicvoidcom.davidkuper.MainActivity.init(Context))")publicvoidpointCut(){}通過2.2.4通知——pointcut通過2.2.4通知——pointcutadvice(包括:beforeafter、around)(1)@Before、例privatestaticfinalStringINSTANCE_METHOD_CALL@paramepublicvoidhandle(Exceptione){}@Around("execution(static*tBitmapFacade.picasso.init(..))&&!within(aspectj.*)")publicvoidpointCutAround(ProceedingJoinPointjoinPoint){ObjectresutObject=null?try{Object[]args=joinPoint.getArgs()?resutObject=joinPceed(args)?}catch(Throwablee){returnreturn@param@parame@param@parame@AfterThrowing(pointcut="afterThrow()",throwing="e")publicvoidafterThrowing(JoinPointjoinPoint,Exceptione){Log.e(TAG,joinPoint.getTarget().getClass().getSimpleName()+"afterThrowing():"+privatestaticfinalStringPOINTCUT_METHOD"(execution(*android.app.Activity+.*(..))||execution(**..Layout+.*(..)))&&@Pointcut(POINTCUT_METHOD)publicvoidmethodAnnotated(){@Around("methodAnnotated()")publicObjectweaveJoinPoint(ProceedingJoinPointjoinPoint)throwsThrowable{行Objectresult=joinPceed()?returnresult?"call(!static*com.meituan.hotel.roadmap..*.*(..))&&target(Object)"?@Pointcut(INSTANCE_METHOD_CALL)publicvoidinstanceMethodCall(){@Before("instanceMethodCall()")publicvoidbeforInstanceCall(JoinPointjoinPoint){printLog(joinPoint,"beforeinstancecall")?@After("instanceMethodCall()")publicvoidafterInstanceCall(JoinPointjoinPoint){printLog(joinPoint,"afterinstancecall")?2.3執(zhí)行原AspectJ.class2.3執(zhí)行原AspectJ.class行代碼注入的方式將通知(Advise)pointCutAdviseJoinPoint這樣在程序運(yùn)行AdviseJoinPointBeforeAfterBeforeAfterJoinPointBefore\AfterAdviseJoinPoint行2.3.2Around2.3.2Around替換,通過一個(gè)單獨(dú)的閉包拆分來執(zhí)行JoinPoint行了代理ExampleprintLog()方法進(jìn)行代理ExampleprintLog()方法進(jìn)行g(shù)etValue()AdvisepublicclasspublicclassExample{Stringvalue="value"?publicvoidprintLog(){Stringstr=publicStringgetValue(){returnvalue?網(wǎng)上給的反編譯過程都是hotel_road_mapdebugjar里網(wǎng)上給的反編譯過程都是hotel_road_mapdebugjar里類,不知道什publicclassExampleprivatestaticfinalStaticPartajc$tjp_0=null?privatestaticfinalStaticPartajc$tjp_1=privatestaticfinalStringINSTANCE_METHOD_EXECUTING="execution(!static*@Pointcut(INSTANCE_METHOD_EXECUTING)publicvoidinstanceMethodExecuting(){行@Around("instanceMethodExecuting()")publicObjectInstanceMethodExecutingAround(ProceedingJoinPointjoinPoint){Objectresult=printLog(joinPoint,"instancereturn例privatestaticfinalStringINSTANCE_METHOD_CALL="call(!static*@Pointcut(INSTANCE_METHOD_CALL)publicvoidinstanceMethodCall(){@Before("instanceMethodCall()")publicvoidbeforInstanceCall(JoinPointjoinPoint){printLog(joinPoint,"beforeinstancecall")?@After("instanceMethodCall()")publicvoidafterInstanceCall(JoinPointjoinPoint){printLog(joinPoint,"afterinstancecall")?privatestaticprivatestaticfinalStaticPartajc$tjp_2=StringTAG="Example"?Stringvalue="value"?static{列privatestaticvoidajc$preClinit()Factoryfactory=newFactory("Example.java",ajc$tjp_0=factory.makeSJP(JoinPoint.METHOD_CALL,factory.makeMethodSig("1","getValue","com.meituan.hotel.roadmap.Ex")?ajc$tjp_1=factory.makeSJP(JoinPoint.METHOD_EXECUTION,factory.makeMethodSig("1","printLog","com.meituan.hotel.roadm")?ajc$tjp_2=factory.makeSJP(JoinPoint.METHOD_EXECUTION,factory.makeMethodSig("1","getValue","com.meituan.hotel.roadm")?publicclassAjcClosure1extendsAroundClosure{publicAjcClosure1(Object[]objArr){publicObjectrun(Object[]objArr){Object[]objArr2=this.state?Example.printLog_aroundBody0((Example)objArr2[0],(JoinPoint)objArr2[1])?returnnull?staticfinalvoidprintLog_aroundBody0(Exampleajc$this,JoinPointjoinPoint){JoinPointmakeJP=Factory.makeJP(ajc$tjp_0,ajc$this,ajc$this)?tryStringstr=ajc$this.getValue()?}finally行Before正的方法體被放在了新的方法中。Before\AfterPointCutJoinPointBefore\AfterPointCutJoinPointBefore\AfterJoinPointAOPstaticfinalvoidprintLog_aroundBody0(Exampleajc$this,JoinPointjoinPoint){JoinPointmakeJP=Factory.makeJP(ajc$tjp_0,ajc$this,ajc$this)?tryStringstr=ajc$this.getValue()?}finally行publicvoidprintLog()JoinPointmakeJP=Factory.makeJP(ajc$tjp_1,this,LogAspect.aspectOf().InstanceMethodExecutingAround(newAjcClosure1(newObject[]{this,makeJP}).linkClosureAndJoinPoint(publicStringgetValue()JoinPointmakeJP=Factory.makeJP(ajc$tjp_2,this,this)?return(String)LogAspect.InstanceMethodExecutingAround(newAjcClosure3(newObject[]{this,Around為Around為printLogXXX_aroundBodyN(args)的方法體被放在了新的方法中。publicvoidprintLog()JoinPointmakeJP=Factory.makeJP(ajc$tjp_1,this,joinPceed()調(diào)用閉包執(zhí)行原方法。LogAspect.aspectOf().InstanceMethodExecutingAround(newAjcClosure1(newObject[]{this,列privatestaticvoidajc$preClinit()Factoryfactory=newFactory("Example.java",ajc$tjp_0=factory.makeSJP(JoinPoint.METHOD_CALL,factory.makeMethodSig("1","getValue","com.meituan.hotel.roadmap.Exa")?ajc$tjp_1=factory.makeSJP(JoinPoint.METHOD_EXECUTION,factory.makeMethodSig("1","printLog","com.meituan.hotel.roadma")?ajc$tjp_2=factory.makeSJP(JoinPoint.METHOD_EXECUTION,factory.makeMethodSig("1","getValue","com.meituan.hotel.roadma")?publicJoinPoint.StaticPartmakeSJP(Stringkind,Stringmodifiers,StringmethodName,StringdeclaringType,StringparamTypes,StSignaturesig=this.makeMethodSig(modifiers,methodName,declaringType,paramTypes,paramNames,"",returnType)?returnnewJoinPointImpl.StaticPar
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 信用卡擔(dān)保人還款協(xié)議
- 植物園租賃經(jīng)營合同
- 港口航道疏浚履約擔(dān)保格式細(xì)則
- 期權(quán)轉(zhuǎn)讓協(xié)議參考模板
- 壓力罐施工合同環(huán)保工程專用
- 航空安全管理辦法
- 醫(yī)囑管理與醫(yī)療效率
- 國際貿(mào)易法律顧問聘用合同
- 教育動(dòng)畫片監(jiān)制合作協(xié)議模板
- 運(yùn)動(dòng)場(chǎng)設(shè)施施工圍擋施工合同
- 學(xué)校體育學(xué)智慧樹知到答案2024年湖南科技大學(xué)
- 英語完形填空練習(xí)題20篇
- 農(nóng)業(yè)農(nóng)村基礎(chǔ)知識(shí)考試復(fù)習(xí)題庫寶典(600多題)
- 綠色港口等級(jí)評(píng)價(jià)指南
- “非遺”之首-昆曲經(jīng)典藝術(shù)欣賞智慧樹知到期末考試答案章節(jié)答案2024年北京大學(xué)
- DL∕T 1704-2017 脫硫濕磨機(jī)石灰石制漿系統(tǒng)性能測(cè)試方法
- 高二物理寒假作業(yè)
- 2024中考英語試題研究《漢堡包寫作法助力中考英語書面表達(dá)》課件
- 建筑公司績(jī)效考核辦法1
- 服務(wù)質(zhì)量保障措施及進(jìn)度保障措施
- 燃?xì)廨斉湫袠I(yè)經(jīng)營分析報(bào)告
評(píng)論
0/150
提交評(píng)論