<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,  comments - 11,  trackbacks - 0
    一、AOP 概念
    Joinpoint:它定義在哪里加入你的邏輯功能,對(duì)于Spring AOP,Jointpoint指的就是Method。

    Advice:特定的Jointpoint處運(yùn)行的代碼,對(duì)于Spring AOP 來(lái)講,有Before advice、AfterreturningAdvice、ThrowAdvice、AroundAdvice(MethodInteceptor)等。

    Pointcut:一組Joinpoint,就是說(shuō)一個(gè)Advice可能在多個(gè)地方織入,

    Aspect:這個(gè)我一直迷惑,它實(shí)際是Advice和Pointcut的組合,但是Spring AOP 中的Advisor也是這樣一個(gè)東西,但是Spring中為什么叫Advisor而不叫做Aspect。

    Weaving:將Aspect加入到程序代碼的過(guò)程,對(duì)于Spring AOP,由ProxyFactory或者ProxyFactoryBean負(fù)責(zé)織入動(dòng)作。

    Target:這個(gè)很容易理解,就是需要Aspect功能的對(duì)象。

    Introduction:引入,就是向?qū)ο笾屑尤胄碌膶傩曰蚍椒?,一般是一個(gè)實(shí)例一個(gè)引用對(duì)象。當(dāng)然如果不引入屬性或者引入的屬性做了線程安全性處理或者只讀屬性,則一個(gè)Class一個(gè)引用也是可以的(自己理解)。Per-class lifecycle or per-instance life cycle

    二、AOP 種類
    1、靜態(tài)織入:指在編譯時(shí)期就織入Aspect代碼,AspectJ好像是這樣做的。

    2、動(dòng)態(tài)織入:在運(yùn)行時(shí)期織入,Spring AOP屬于動(dòng)態(tài)織入,動(dòng)態(tài)織入又分靜動(dòng)兩種,靜則指織入過(guò)程只在第一次調(diào)用時(shí)執(zhí)行;動(dòng)則指根據(jù)代碼動(dòng)態(tài)運(yùn)行的中間狀態(tài)來(lái)決定如何操作,每次調(diào)用Target的時(shí)候都執(zhí)行(性能較差)。

    三、Spring AOP 代理原理
    Spring AOP 是使用代理來(lái)完成的,Spring 會(huì)使用下面兩種方式的其中一種來(lái)創(chuàng)建代理:
    1、JDK動(dòng)態(tài)代理,特點(diǎn)只能代理接口,性能相對(duì)較差,需要設(shè)定一組代理接口。
    2、CGLIB 代理,可代理接口和類(final method除外),性能較高(生成字節(jié)碼)。


    四、Spring AOP 通知類型
    1、BeforeAdvice:前置通知需實(shí)現(xiàn)MethodBeforeAdvice,但是該接口的Parent是BeforeAdvice,致于什么用處我想可能是擴(kuò)展性需求的設(shè)計(jì)吧。或者Spring未來(lái)也并不局限于Method的JoinPoint(胡亂猜測(cè))。BeforeAdvice可以修改目標(biāo)的參數(shù),也可以通過(guò)拋出異常來(lái)阻止目標(biāo)運(yùn)行。

    2、AfterreturningAdvice:實(shí)現(xiàn)AfterreturningAdvice,我們無(wú)法修改方法的返回值,但是可以通過(guò)拋出異常阻止方法運(yùn)行。

    3、AroundAdvice:Spring 通過(guò)實(shí)現(xiàn)MethodInterceptor(aopalliance)來(lái)實(shí)現(xiàn)包圍通知,最大特點(diǎn)是可以修改返回值,當(dāng)然它在方法前后都加入了自己的邏輯代碼,因此功能異常強(qiáng)大。通過(guò)MethodInvocation.proceed()來(lái)調(diào)用目標(biāo)方法(甚至可以不調(diào)用)。

    4、ThrowsAdvice:通過(guò)實(shí)現(xiàn)若干afterThrowing()來(lái)實(shí)現(xiàn)。

    5、IntroductionInterceptor:Spring 的默認(rèn)實(shí)現(xiàn)為DelegatingIntroductionInterceptor

    五、Spring AOP Pointcut
    以上只是Advice,如果不指定切入點(diǎn),Spring 則使用所有可能的Jointpoint進(jìn)行織入(當(dāng)然如果你在Advice中進(jìn)行方法檢查除外)。因此切入點(diǎn)在AOP中扮演一個(gè)十分重要的角色。Spring 2.0 推薦使用AspectJ的Annocation的切入點(diǎn)表達(dá)式來(lái)定義切入點(diǎn),或者使用<aop:xxx/>來(lái)定義AOP,這方面本篇不做考慮。

    1、Pointcut:它是Spring AOP Pointcut的核心,定義了getClassFilter()和getMethodMatcher()兩個(gè)方法。

    2、ClassFilter:定義了matches(Class cls)一個(gè)方法。

    3、MethodMatcher() 定義了matches(Method,Class),isRuntime(),matches(Mathod,Class,Object[])三個(gè)方法,如果isRuntime()返回true則表示為動(dòng)態(tài)代理(實(shí)際是動(dòng)態(tài)代理的動(dòng)態(tài)代理),則調(diào)用第三個(gè)方法(每訪問(wèn)一次調(diào)用一次),否則調(diào)用第一個(gè)方法(并且只調(diào)用一次)

    4、Spring AOP 靜態(tài)切入點(diǎn)的幾個(gè)實(shí)現(xiàn)。
    ComposablePointcut 太復(fù)雜一個(gè)切入點(diǎn)無(wú)法表達(dá)就用這個(gè),union MethodMatcher和ClassFilter或者intersection MethodMatcher、ClassFilter和Pointcut。為什么不實(shí)現(xiàn)union Pointcut? 而只能通過(guò)Pointcuts類對(duì)Pointcut進(jìn)行union操作。

    ControlFlowPointcut 想對(duì)程序的運(yùn)行過(guò)程進(jìn)行追蹤就用這個(gè)

    DynamicMatchMatcherPointcut 想用動(dòng)態(tài)AOP 就用這個(gè)

    JdkRegexpMethodPointcut 想使用正則表達(dá)式就用這個(gè)

    Perl5RegexpMethodPointcut

    NameMatchMethodPointcut 想用方法名字來(lái)匹配就用這個(gè)

    StaticMethodMatcherPointcut 靜態(tài)切入點(diǎn)就用這個(gè)
    沒(méi)有人反對(duì)你直接實(shí)現(xiàn)Pointcut:)。

    六、Spring AOP 中的Advisor其實(shí)就是Aspect
    1、 PointcutAdvisor
    其實(shí)一般使用DefaultPointcutAdvisor就足夠了,給它Advice和Pointcut。
    當(dāng)然如果想少寫那么幾行代碼也可以使用NameMatchMethodPointcutAdvisor,RegexpMethodPointcutAdvisor等。
    更多Advisor可以查看API文檔。


    2、 IntroductionAdvisor
    默認(rèn)實(shí)現(xiàn)為DefaultIntroductionAdvisor。

    七、AOP ProxyFactory
    使用代碼實(shí)現(xiàn)AOP 可使用ProxyFactory
    聲明式AOP 可使用ProxyFactoryBean
    ProxyFactoryBean 需要設(shè)定 target,interceptorNames(可以是Advice或者Advisor,注意順序)
    對(duì)接口代理需設(shè)置proxyInterfaces

    八、自動(dòng)代理
    BeanNameAutoProxyCreator
    Java代碼
    1. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> ??
    2. ???? <property name="beanNames"><value>jdk*,onlyJdk</value></property> ??
    3. ???? <property name="interceptorNames"> ??
    4. ???????? <list> ??
    5. ???????????? <value>myInterceptor</value> ??
    6. ???????? </list> ??
    7. ???? </property> ??
    8. </bean>??

    DefaultAdvisorAutoProxyCreator
    Java代碼
    1. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> ??
    2. <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"> ??
    3. ???? <property name="transactionInterceptor" ref="transactionInterceptor"/> ??
    4. </bean> ??
    5. <bean id="customAdvisor" class="com.mycompany.MyAdvisor"/> ??
    6. <bean id="businessObject1" class="com.mycompany.BusinessObject1"> ??
    7. ???? <!-- Properties omitted --> ??
    8. </bean> ??
    9. <bean id="businessObject2" class="com.mycompany.BusinessObject2"/>??

    posted on 2009-07-20 21:44 jadmin 閱讀(152) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久草在视频免费福利| 国产亚洲日韩一区二区三区| 亚洲AV无码一区二区三区鸳鸯影院| 亚洲av无码专区在线观看素人| 免费视频成人手机在线观看网址| 亚洲a级成人片在线观看| 免费观看国产精品| 四虎在线成人免费网站| 亚洲av片在线观看| 午夜亚洲www湿好大| 永久免费毛片手机版在线看| 十八禁视频在线观看免费无码无遮挡骂过 | 国产成A人亚洲精V品无码 | 国产高清在线免费视频| 免费黄网站在线看| 黄色三级三级免费看| 综合自拍亚洲综合图不卡区| 免费真实播放国产乱子伦| 在线看片v免费观看视频777| 一级日本高清视频免费观看| 波多野结衣亚洲一级| 亚洲av中文无码乱人伦在线播放| 日本19禁啪啪无遮挡免费动图| 久久不见久久见免费视频7| 无码 免费 国产在线观看91| 一本色道久久88亚洲精品综合| 久久精品国产69国产精品亚洲| 国产成人免费福利网站| 久久经典免费视频| 一个人免费日韩不卡视频| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | AA免费观看的1000部电影| 免费A级毛片无码视频| fc2成年免费共享视频18| 亚洲av纯肉无码精品动漫| 亚洲精品日韩专区silk| 亚洲va无码手机在线电影| 亚洲女同成人AⅤ人片在线观看 | 又黄又大又爽免费视频| 大地资源在线观看免费高清| 精品免费久久久久久久|