




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(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ù)處理,異常處理碼中劃分出來(lái),通過(guò)對(duì)這些行這些行為的時(shí)候不1.3適用對(duì)象OOP力有日志、性能、安全、異常處理1.4AOPOOP裝,以獲得更不是單一的類(lèi),不OOP不是單一的類(lèi),不OOP是處理AOP不OOP了OOP不足。OOP了問(wèn)題,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á)式來(lái)進(jìn)行匹配應(yīng)用,決定了jointpointcallexecution、target、this、within達(dá)式來(lái)進(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(){}通過(guò)2.2.4通知——pointcut通過(guò)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替換,通過(guò)一個(gè)單獨(dú)的閉包拆分來(lái)執(zhí)行JoinPoint行了代理ExampleprintLog()方法進(jìn)行代理ExampleprintLog()方法進(jìn)行g(shù)etValue()AdvisepublicclasspublicclassExample{Stringvalue="value"?publicvoidprintLog(){Stringstr=publicStringgetValue(){returnvalue?網(wǎng)上給的反編譯過(guò)程都是hotel_road_mapdebugjar里網(wǎng)上給的反編譯過(guò)程都是hotel_road_mapdebugjar里類(lèi),不知道什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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 買(mǎi)美發(fā)設(shè)備合同標(biāo)準(zhǔn)文本
- 余姚房屋拆除合同標(biāo)準(zhǔn)文本簡(jiǎn)易
- 朱自清春朗誦課件
- 代購(gòu)代付款合同標(biāo)準(zhǔn)文本
- 協(xié)會(huì)制定合同標(biāo)準(zhǔn)文本
- 2025設(shè)備采購(gòu)合同「乙種本」
- 作品署名商用合同標(biāo)準(zhǔn)文本
- 2025存量房買(mǎi)賣(mài)合同示范文本2
- 制造材料合同標(biāo)準(zhǔn)文本
- 體外試劑購(gòu)銷(xiāo)合同標(biāo)準(zhǔn)文本
- 中國(guó)各省分地市矢量地圖(做用)
- JJF(陜) 099-2022 粘結(jié)強(qiáng)度檢測(cè)儀校準(zhǔn)規(guī)范
- 企業(yè)培訓(xùn)體系建設(shè)及效果評(píng)估
- 655檔案學(xué)概論-馮惠玲-筆記
- 西安電子科技大學(xué)《科技英語(yǔ)寫(xiě)作》2021-2022學(xué)年第一學(xué)期期末試卷
- 臨床經(jīng)鼻高流量濕化氧療患者護(hù)理查房
- 2024年貴州省中考數(shù)學(xué)真題含解析
- 《鐵路軌道維護(hù)》課件-無(wú)縫線(xiàn)路脹軌跑道預(yù)防與處理
- 人工智能設(shè)計(jì)倫理(浙江大學(xué))知到智慧樹(shù)章節(jié)答案
- M7120型平面磨床電氣控制
- 【數(shù)學(xué)】2021-2024年新高考數(shù)學(xué)真題考點(diǎn)分布匯
評(píng)論
0/150
提交評(píng)論