第2章 Aop簡介_第1頁
第2章 Aop簡介_第2頁
第2章 Aop簡介_第3頁
第2章 Aop簡介_第4頁
第2章 Aop簡介_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、第2章 AOPSpring不是因?yàn)闀?huì)了才去做,而是因?yàn)樽隽瞬拍軙?huì)!AOPAOP技術(shù)概述AOP概念1.面向方面編程2.作為OO面向?qū)ο蟮难a(bǔ)充,而不是替代。AOP目標(biāo)1.不改變現(xiàn)有代碼2.給系統(tǒng)動(dòng)態(tài)增加橫切關(guān)注點(diǎn)。1.OCP開閉原則2.針對接口/抽象編程AOPAOP基本概念核心關(guān)注點(diǎn)u業(yè)務(wù)功能。切面/橫切關(guān)注點(diǎn)u對日志安全等公共服務(wù)模塊化連接點(diǎn)u程序執(zhí)行中的某個(gè)特定點(diǎn)。切入點(diǎn)u設(shè)置加入切面位置的表達(dá)式,來匹配連接點(diǎn)。通知/Adviceu將切面在某個(gè)連接點(diǎn)執(zhí)行。顧問Advisoru切面+切入點(diǎn)AOPAOP實(shí)現(xiàn)方式JDK動(dòng)態(tài)代理uJDK1.4后增加的內(nèi)容。u只能對實(shí)現(xiàn)了接口的類生成代理,而不能針對類。

2、CGLIB字節(jié)碼增強(qiáng)u是針對類實(shí)現(xiàn)代理,主要是對指定的類生成一個(gè)子類,覆蓋其中的方法。因?yàn)槭抢^承,所以該類或方法最好不要聲明成finalApectJ支持方法攔截與屬性修改Spring AOP只支持方法攔截。springspring對AOPAOP的支持如果目標(biāo)對象實(shí)現(xiàn)了接口,默認(rèn)情況下會(huì)采用JDK的動(dòng)態(tài)代理實(shí)現(xiàn)AOP目標(biāo)對象實(shí)現(xiàn)了接口,也可以強(qiáng)制使用CGLIB實(shí)現(xiàn)AOPu添加CGLIB庫,SPRING_HOME/cglib/*.jar u在spring配置文件中加入如果目標(biāo)對象沒有實(shí)現(xiàn)接口,必須采用CGLIB庫spring會(huì)自動(dòng)在JDK動(dòng)態(tài)代理和CGLIB之間轉(zhuǎn)換常見橫切關(guān)注點(diǎn)日志事務(wù)安全異常處

3、理性能檢測AdviceAdvicelMethodBeforeAdvicel前置通知l在業(yè)務(wù)方法執(zhí)行之前執(zhí)行l(wèi)AfterReturningAdvicel后置通知l在業(yè)務(wù)方法執(zhí)行之后執(zhí)行l(wèi)MethodInterceptorl環(huán)繞通知l在業(yè)務(wù)方法執(zhí)行前后執(zhí)行,甚至替代攔截的方法。lThrowsAdvicel拋出異常時(shí)執(zhí)行。核心關(guān)注點(diǎn)public class AopDemo implements IAopDemopublic void doSomething() throws ExceptionSystem.out.println(Process Buniess!);package com.lxt00

4、8.aop;public interface IAopDemopublic void doSomething() throws Exception;客戶端ApplicationContext ctx=new ClassPathXmlApplicationContext(applicationContext.xml);IAopDemo demo=(IAopDemo)ctx.getBean(aopDemo);trydemo.doSomething();catch(Exception e)System.out.println(Found Exception);MethodBeforeAdviceMe

5、thodBeforeAdvicepublic class SecrityManagerimplements MethodBeforeAdvicepublic void before(Method m,Object args,Object target)System.out.println(before Check!);applicationContext.xmlapplicationContext.xml被代理目標(biāo):被代理目標(biāo):安全通知安全通知:ProxyFactoryBeanProxyFactoryBeancom.lxt008.aop.IAopDemo secrityAdvice After

6、ReturningAdviceAfterReturningAdvicepublic class LogManagerimplements AfterReturningAdvice public void afterReturning(Object returnValue,Method method,Object args, Object target) throws Throwable System.out.println(After Log!);MethodInterceptorMethodInterceptorpublic class PerformanceAroundAdvice imp

7、lements MethodInterceptorpublic Object invoke(MethodInvocation invocation) throws Throwable Object returnValue=null;/開始計(jì)時(shí)開始計(jì)時(shí)StopWatch sw = new StopWatch();sw.start(invocation.getMethod().getName();returnValue = ceed();/結(jié)束計(jì)時(shí)結(jié)束計(jì)時(shí)sw.stop();Method m = invocation.getMethod();Object target

8、= invocation.getThis();Object args = invocation.getArguments();MethodInterceptoMethodInterceptor rSystem.out.println();System.out.println(執(zhí)行方法名執(zhí)行方法名: + m.getName();System.out.println(目標(biāo)類目標(biāo)類: + target.getClass().getName();if(args!=null) System.out.println(參數(shù)列表參數(shù)列表:);for (int i = 0; i args.length; i+)

9、 System.out.print(第第 + i + 個(gè)參數(shù)個(gè)參數(shù): + argsi);System.out.println();System.out.println(總時(shí)間總時(shí)間: + sw.getTotalTimeMillis() + ms);return returnValue;ThrowsAdviceThrowsAdvicepublic class MyThrowsAdvice implements ThrowsAdvice public void afterThrowing(Method method, Object args, Object target, Exception ex

10、) System.out.println(捕獲其它不明異常捕獲其它不明異常);System.out.println(異常發(fā)生方法異常發(fā)生方法: + method.getName();System.out.println();修改doSomethingdoSomething()()public void doSomething() throws ExceptionSystem.out.println(Process Buniess!);throw new Exception(unkown exception);加入異常測試。加入異常測試。增加業(yè)務(wù)方法增加業(yè)務(wù)方法引入引入AdvisorAdvisorAdvisordoSomethingRegexpMethodPointcutAdvisorRegexpMethodPointcutAdvisor.*doSomething.*RegexpMethodPointcutAdvisorRegexpMethodPointcutAdvisor applicatio

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論