s2sh框架-課件3-springSpring開發(fā)培訓(xùn)_第1頁
s2sh框架-課件3-springSpring開發(fā)培訓(xùn)_第2頁
s2sh框架-課件3-springSpring開發(fā)培訓(xùn)_第3頁
s2sh框架-課件3-springSpring開發(fā)培訓(xùn)_第4頁
s2sh框架-課件3-springSpring開發(fā)培訓(xùn)_第5頁
已閱讀5頁,還剩33頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

第三章:Spring

AOP目標(biāo):本章旨在向?qū)W員介紹:1)2)時間:學(xué)時2教學(xué)方法:AOP是什么面向方面編程(Aspect

Oriented

Programming)OOP的補充及延續(xù)AOP和OOD/OOP并不

,

完全可以在一個應(yīng)用系統(tǒng)中同時應(yīng)用OOD/OOP和AOP設(shè)計思想,通過OOD/OOP對系統(tǒng)中的業(yè)務(wù)對象進行建模,同時通過AOP對實體處理過程中的階段進行

處理。Spring

AOP是基于Spring

IoC基礎(chǔ)上所做的擴展把程序分解為方面或關(guān)注點,使程序員可以把精力集中在他們專注的領(lǐng)域AOP還有另外一個重要特點:源碼組成無關(guān)性。為了實現(xiàn)源碼組成無關(guān)性,AOP往往通過預(yù)編譯方式(如AspectJ)和運行期動態(tài) 模式(如Spring

AOP

和JBossAOP)實現(xiàn)。3Without

AOP4With

AOP5Without

AOP:

當(dāng)拋出異常時發(fā)送public

class

AccountManagerImpl

implements

AccountManager

{private

AccountDAO

accountDAO;private

void

sendMail(Exception

ex)

{...}public

Account

getAccount(String

accountId)throws

AccountNotFoundException,

DataAccessException

{try

{return

this.accountDAO.findAccount(accountId);}

catch(AccountNotFoundException

ex)

{sendMail(ex);throw

ex;}

catch(DataAccessException

ex)

{sendMail(ex);throw

ex;}}public

void

createAccount(Account

account)

throws

DataAccessException

{try

{this.accountDAO.saveAccount(account);}

catch(DataAccessException

ex)

{sendMail(ex);throw

ex;}6Without

AOP:當(dāng)拋出異常時發(fā)送7With

AOP:當(dāng)拋出異常時發(fā)送public

class

AccountManagerImpl

implements

AccountManager

{AccountDAO

accountDAO;public

void

setAccountDAO(AccountDAO

accountDAO)

{this.accountDAO

=

accountDAO;}public

Account

getAccount(String

accountId)throws

AccountNotFoundException,

DataAccessException

{return

this.accountDAO.findAccount(accountId);}public

void

createAccount(Account

account)

throws

DataAccessException

{this.accountDAO.saveAccount(account);}public

class

NotificationThrowsAdvice

implements

ThrowsAdvice

{private

void

sendMail(Exception

ex)

{...}publicvoid

afterThrowing(Exception

ex)

throws

Throwable

{this.sendMail(ex);}}8With

AOP:當(dāng)拋出異常時發(fā)送<bean

id="accountManager

"

class="com.trivadis.aop.AccountManagerImpl"><property

name="accountDAO"><ref

bean="accountDAO"/></property></bean><bean

id="

NotificationThrowAdvice"class="com.trivadis.aop.

NotificationThrowsAdvice"><property

name="mailSender"><ref

bean="mailSender"/></property></bean><bean

id="accountManagerBeanNameProxyCreator”class="

.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><property

name="beanNames"><value>accountManager

</value></property><property

name="interceptorNames"><list>NotificationThrowAdvice</value><value></list></property></bean>9With

AOP:當(dāng)拋出異常時發(fā)送10為何使用AOP高度模塊化,使得

的系統(tǒng)更易實現(xiàn)和更易使每個模塊承擔(dān)的責(zé)任更清晰,提高代碼的可追蹤性解決設(shè)計時兩難的局面,在不需改動原先代碼的情況下推遲不必要的需求的實現(xiàn)提高代碼的重用性加速系統(tǒng)的開發(fā)和部署,提高程序員的開發(fā)效率降低系統(tǒng)開發(fā)的成本11AOP應(yīng)用范圍,優(yōu)化,)Persistence(持久化)Transaction

management(事務(wù)管理)Security(安全)Logging,tracing,profiling and

monitoring(日志,Debugging(調(diào)試)Authentication(認(rèn)證)Context

passing(上下文傳遞)Error/Exception

handling(錯誤/異常處理)Lazy

loading(懶加載)Performance

optimization(性能優(yōu)化)Resource

pooling(資源池)Synchronization(同步)12概念Joinpoint(連接點)–接點是一個程序執(zhí)行過程中的特定點。典型的連接點包括對一個方法的調(diào)用、方法執(zhí)行的過程本身、類的初始化、對象的實例化等。–

連接點是AOP概念之一,它用來定義在程序的什么地方能通過AOP加入額外的邏輯。Advice(通知)–

在某一個特定的連接點處運行的代碼稱為“通知”。通知有很多種,比如在連接點之前執(zhí)行的前置通知(befor

advice)和在連接點之后執(zhí)行的后置通知(afteradvice)。Pointcut(切入點)切入點是用來定義某一個通知該何時執(zhí)行的一組連接點。通過創(chuàng)建切入點,可以精確地控制程序中什么組件接到什么通知。例如:之前 提到過,一個典型的連接點是方法的調(diào)用,而一個典型的切入點就是對某一個類的所有方法調(diào)用的集合。通常通過類名和方法名,或者匹配類名和方法式樣的正則表達(dá)式來指定切入點。一些AOP支持動態(tài)切入點,在運行時根據(jù)條件決定是否應(yīng)用切面,如方法參數(shù)值。Aspect(方面)通知和切入點的組合叫方面。這個組合定義了一段程序中應(yīng)該包括的邏輯以及何時應(yīng)該執(zhí)行該邏輯。13概念Weaving(織入)–織入是將方面真正加入程序代碼的過程。對于編譯時AOP方案而言,織入自然是在編譯時完成的,它通常是作為編譯過程中的一個額外步驟。類似地,對于運行時AOP方案,織入過程是在程序運行時動態(tài)執(zhí)行的。?object(目標(biāo)對象)–

如果一個對象的執(zhí)行過程受到某個AOP操作的修改,那么它就叫做一個目標(biāo)對象。目標(biāo)對象通常也稱為被通知對象。Introduction(引入)–通過引入,可以在一個對象中加入新的方法或字段,以改變它的結(jié)構(gòu)。你可以使用引入來讓任何對象實現(xiàn)一個特定接口,而不需要這個對象的類顯示地實現(xiàn)這個接口。AOP

proxy(AOP

)–是將通知應(yīng)用到目標(biāo)對象后創(chuàng)建的對象。對于客戶對象來說,目標(biāo)對象(應(yīng)用AOP之前的對象)和 對象(應(yīng)用AOP之后的對象)是一樣的。也就是,應(yīng)用系統(tǒng)的其他部分不用為了支持

對象而改變。Advisor(顧問)–

在Spring中,一個advisor就是一個aspect的完整的模塊化標(biāo)識。一般地,一個advisor包括切入點和通知,是切入點和通知的配置器。14AOP

模型ProxyObjectAdviceAdvisorPointcutJoinPoint15AOP原理(圖1-理解))示例:SpringAOP01(Without示例:SpringAOP02(靜態(tài)

示例:SpringAOP03(動態(tài)))靜態(tài)

對象與被

對象都必須實現(xiàn)同一個接口。在

對象中可以實現(xiàn)日志記錄等相關(guān)服務(wù),并在需要的時候再呼叫被 的對象;被

對象當(dāng)中就可以僅保留業(yè)務(wù)相關(guān)的職責(zé)。動態(tài)

:在程序運行時,運用反射機制動態(tài)創(chuàng)建而成。16AOP原理(圖2)Advisor/InterceptorAOPProxyreturnCallerMethodAdvisor/InterceptorCaller

invokes

proxy,notProxy

invokesinterceptorsForward

processing

of

interceptorchain

concludes

with

invocationof

method

on

objectControl

flows

backthrough

interceptor

chainto

return

result

to

caller1718通知的類型(掌握)編寫adviceBefore

advice(前置通知)After

returning

advice(返回后通知)Throws

advice(拋出通知)Around

advice(環(huán)繞通知)示例:SpringAOP04(Before

advice)示例:SpringAOP05(After

Returning

Adivce)19Around

Advice

Demo(掌握)public

class

PerformanceMonitorDetailInterceptorimplements

MethodInterceptor

{protected

final

Log

logger

=

LogFactory.getLog(getClass());public

Object

invoke(MethodInvocation

invocation)

throws

Throwable

{String

name

=invocation.getMethod().getDeclaringClass().getName()+

"."+

invocation.getMethod().getName();StopWatchsw

=

new

StopWatch(name);sw.start(name);Object

rval

=

ceed();sw.stop();(sw.prettyPrint());return

rval;}}示例:SpringAOP06_01(Around

Advice)Around

Advice

Demo(了解)<bean

id="perfMonInterceptor"class="...PerformanceMonitorDetailInterceptor"/><bean

id="performanceAdvisor"class="

.springframework.aop.support.RegexpMethodPointcutAdvisor"><property

name="advice"><ref

local="perfMonInterceptor"/></property><property

name="patterns"><list><value>.*find.*</value><value>.*save.*</value><value>.*update.*</value></list></property></bean>示例:SpringAOP06_02(Around

Advice

RegexpMethodPointcutAdvisor)20After

Throwing

Advice

Demo(掌握)public

class

ExceptionLogger

implements

ThrowAdvice

{protected

final

Log

logger

=

LogFactory.getLog(getClass());public

void

afterThrowing(Method

m,

Object[]

args,ObjectThrowable

ex)

throws

Throwable

{,logger.log(Level.INFO,

arg[0]+“執(zhí)行”+

method.getName+“時有異常拋出…”+ex);}}示例:SpringAOP07_01(Throws

Advice)21After

Throwing

Advice

Demo(Advisor)<bean

id=“exceptionLogger"class="...

ExceptionLogger

"/><bean

id=“exceptionLoggerAdvisor"class="

.springframework.aop.support.RegexpMethodPointcutAdvisor"><property

name="advice"><ref

local="exceptionLogger

"/></property><property

name="patterns"><value>.*</value></property></bean>示例:SpringAOP07_02(Throws

Advice)22切入點的接口前、編寫advisor–

之前,

所定義的Adivce都是直接織入至后或者在異常發(fā)生時;事實上還可以定義更細(xì)的織入時機:Pointcut定義了Advice的應(yīng)用時機;在Spring中,使用PointcutAdvisor將Pointcut與Advice結(jié)合成為一個對象;Spring內(nèi)建的Pointcut都有對應(yīng)的PointcutAdvisor。23切入點的類型靜態(tài)NameMat

ethodPointcut匹配方法名稱無法對重載(Overload)的方法名進行匹配,因為它僅對方法名進行匹配,不會考慮參數(shù)相關(guān)信息,而且也沒有提供可以指定參數(shù)匹配信息的途徑。–RegexpMethodPointcut根據(jù)正則表達(dá)式計算切入點,要匹配完整的類名和方法名對應(yīng)的Advisor是RegexpMethodPointcutAdvisor,會根據(jù)當(dāng)前JDK版本來決定使用JdkRegexpMethodPointcut

,還是Perl5RegexpMethodPointcut

。動態(tài)–

ControlFlowPointcut24NameMatethodPointcut

Demo(掌握)<bean

id="nameMat

ethodPointcut"ethodPointcut">class="

.springframework.aop.support.NameMat?<property

name="mappedName"><value>

o*</value></property></bean>02例子中直接把切入點放入Advisor中,不像01例子中那樣還需要單獨定義切入點。示例:SpringAOP08_01(NameMat示例:SpringAOP08_02(NameMatethodPointcut)ethodPointcutAdvisor)25RegexpMethodPointcut

Demo<bean

id="gettersAndSettersPointcut"class="

.springframework.aop.support.

RegexpMethodPointcut"><property

name="patterns"><list><value>.*\.get.*</value><value>.*\.set.*</value></list></property></bean>2627正則表達(dá)式符號說明(了解)SymbolDescriptionExample.匹配任何單個字符setFoo.匹配

setFooB,但不匹配setFoo

setFooBar+匹配前一個字符一次或多次setFoo.+匹配

setFooBar

andsetFooB,但不匹配

setFoo*匹配前一個字符零次或多次setFoo.*

匹配

setFoo,setFooB

和setFooBar\匹配任何正則表達(dá)式符號\.setFoo.匹配

.setFooB,但不匹配setFoo示例:SpringAOP09(Rege

xpMethodPointcutAdvisor)ControlFlowPointcut

Demo(了解)oFlowControlPointcut"/>class="

.springframework.aop.support.ControlFlowPointcut"><constructor-arg><value>neu.danny.Some</value></constructor-arg></bean><bean

id="

oAdvisor"class="

.springframework.aop.support.DefaultPointcutAdvisor"><property

name="advice"><ref

bean="logBeforeAdvice"/></property><property

name="pointcut"><bean

id="

oFlowControlPointcut"???????????????<ref

bean="</property></bean>示例:SpringAOP10ControlFlowPointcut是Spring所提供的類:判斷在方法的執(zhí)行堆棧中,某個指定類的某個方法中,是否曾經(jīng)要求您的目標(biāo)對象執(zhí)行某個動作,由于這是在執(zhí)行時期才會確定是否介入Advices,因此是Spring提供的動態(tài)Pointcut功能。28ProxyFactoryBean是什么創(chuàng)建AOP

proxy的基本方法,結(jié)合Spring的IoC容器一起使用使用ProxyFactoryBean最大的優(yōu)點是:通知和切入點都可以被Spring的IoC管理起來通過使用Java的動態(tài)

或CGLIB,能夠

接口或class29ProxyFactoryBean的主要屬性?–

的目標(biāo)對象proxyInterfaces–

生成的

類需要實現(xiàn)的接口列表interceptorNames–

作用于或顧問名字的列表singleton–

工廠返回的

類是否為單實例proxy–

是否Class目標(biāo)類,而不是實現(xiàn)接口30ProxyFactoryBean

Demo<!--目標(biāo)對象等--><beanid=" "

class="eg.Impl"><property

name="name"><value>Tony</value></property><property

name="age"><value>51</value></property></bean><bean

id="myAdvisor"

class="eg.MyAdvisor"><property

name="someProperty"><value>Something</value></property></bean><bean

id="debugInterceptor"

class="...erceptor.NopInterceptor"></bean>31ProxyFactoryBean

Demo(接口)<bean

id=""class="...aop.framework.ProxyFactoryBean"><property

name="proxyInterfaces"><value>eg.</property><property

name="<ref

local="</value>">"/></property><property

name="interceptorNames"><list><value>myAdvisor</value><value>debugInterceptor</value></list></property></bean>32ProxyFactoryBean

Demo(類)<bean

id="

"class="...aop.framework.ProxyFactoryBean">Class">"><property

name="proxy<value>true</value></property><property

name="<ref

local=""/></property><property

name="interceptorNames"><list><value>myAdvisor</value><value>debugInterceptor</value></list></property></bean>33自動,不需要使用ProxyFactoryBean為每個bean能夠自動創(chuàng)建生成一個避免為每個的目標(biāo)對象顯式的創(chuàng)建一個自動

的兩個默認(rèn)實現(xiàn)BeanNameAutoProxyCreatorDefaultAdvisorAutoProxyCreator3435BeanNameAutoProxyCreator

Demo<bean

id="employee1"

class="eg.Employee">...</bean><bean

id="employee2"

class="eg.Employee">...</bean><bean

id="myInterceptor"

class="eg.DebugInterceptor"/><bean

id="beanNameProxyCreator"class="...aop.framework.autoproxy.BeanNameAutoProxyCreator"><property

name="beanNames"><value>employee*</value></property>

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論