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

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

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

    城市獵人

    在一網(wǎng)情深的日子里,誰(shuí)能說(shuō)得清是苦是甜,只知道確定了就義無(wú)反顧
    posts - 1, comments - 7, trackbacks - 0, articles - 89

    spring aop總結(jié)

    Posted on 2009-04-29 09:06 sailor 閱讀(417) 評(píng)論(0)  編輯  收藏 所屬分類: spring

    一、Spring AOP相關(guān)術(shù)語(yǔ)

    • 切面(Aspect): 一個(gè)關(guān)注點(diǎn)的模塊化,這個(gè)關(guān)注點(diǎn)可能會(huì)橫切多個(gè)對(duì)象。事務(wù)管理是J2EE應(yīng)用中一個(gè)關(guān)于橫切關(guān)注點(diǎn)的很好的例子。 在Spring AOP中,切面可以使用通用類(基于模式的風(fēng)格) 或者在普通類中以 @Aspect 注解(@AspectJ風(fēng)格)來(lái)實(shí)現(xiàn)。

    • 連接點(diǎn)(Joinpoint): 在程序執(zhí)行過(guò)程中某個(gè)特定的點(diǎn),比如某方法調(diào)用的時(shí)候或者處理異常的時(shí)候。 在Spring AOP中,一個(gè)連接點(diǎn) 總是 代表一個(gè)方法的執(zhí)行。 通過(guò)聲明一個(gè)org.aspectj.lang.JoinPoint類型的參數(shù)可以使通知(Advice)的主體部分獲得連接點(diǎn)信息。

    • 通知(Advice): 在切面的某個(gè)特定的連接點(diǎn)(Joinpoint)上執(zhí)行的動(dòng)作。通知有各種類型,其中包括“around”、“before”和“after”等通知。 通知的類型將在后面部分進(jìn)行討論。許多AOP框架,包括Spring,都是以攔截器做通知模型, 并維護(hù)一個(gè)以連接點(diǎn)為中心的攔截器鏈。

    • 切入點(diǎn)(Pointcut): 匹配連接點(diǎn)(Joinpoint)的斷言。通知和一個(gè)切入點(diǎn)表達(dá)式關(guān)聯(lián),并在滿足這個(gè)切入點(diǎn)的連接點(diǎn)上運(yùn)行(例如,當(dāng)執(zhí)行某個(gè)特定名稱的方法時(shí))。 切入點(diǎn)表達(dá)式如何和連接點(diǎn)匹配是AOP的核心:Spring缺省使用AspectJ切入點(diǎn)語(yǔ)法。

    • 引入(Introduction): (也被稱為內(nèi)部類型聲明(inter-type declaration))。聲明額外的方法或者某個(gè)類型的字段。 Spring允許引入新的接口(以及一個(gè)對(duì)應(yīng)的實(shí)現(xiàn))到任何被代理的對(duì)象。 例如,你可以使用一個(gè)引入來(lái)使bean實(shí)現(xiàn) IsModified 接口,以便簡(jiǎn)化緩存機(jī)制。

    • 目標(biāo)對(duì)象(Target Object): 被一個(gè)或者多個(gè)切面(aspect)所通知(advise)的對(duì)象。也有人把它叫做 被通知(advised) 對(duì)象。 既然Spring AOP是通過(guò)運(yùn)行時(shí)代理實(shí)現(xiàn)的,這個(gè)對(duì)象永遠(yuǎn)是一個(gè) 被代理(proxied) 對(duì)象。

    • AOP代理(AOP Proxy): AOP框架創(chuàng)建的對(duì)象,用來(lái)實(shí)現(xiàn)切面契約(aspect contract)(包括通知方法執(zhí)行等功能)。 在Spring中,AOP代理可以是JDK動(dòng)態(tài)代理或者CGLIB代理。 注意:Spring 2.0最新引入的基于模式(schema-based)風(fēng)格和@AspectJ注解風(fēng)格的切面聲明,對(duì)于使用這些風(fēng)格的用戶來(lái)說(shuō),代理的創(chuàng)建是透明的。

    • 織入(Weaving): 把切面(aspect)連接到其它的應(yīng)用程序類型或者對(duì)象上,并創(chuàng)建一個(gè)被通知(advised)的對(duì)象。 這些可以在編譯時(shí)(例如使用AspectJ編譯器),類加載時(shí)和運(yùn)行時(shí)完成。 Spring和其他純Java AOP框架一樣,在運(yùn)行時(shí)完成織入。

    通知的類型:

    • 前置通知(Before advice): 在某連接點(diǎn)(join point)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)前的執(zhí)行(除非它拋出一個(gè)異常)。

    • 返回后通知(After returning advice): 在某連接點(diǎn)(join point)正常完成后執(zhí)行的通知:例如,一個(gè)方法沒有拋出任何異常,正常返回。

    • 拋出異常后通知(After throwing advice): 在方法拋出異常退出時(shí)執(zhí)行的通知。

    • 后通知(After (finally) advice): 當(dāng)某連接點(diǎn)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是異常退出)。

    • 環(huán)繞通知(Around Advice): 包圍一個(gè)連接點(diǎn)(join point)的通知,如方法調(diào)用。這是最強(qiáng)大的一種通知類型。 環(huán)繞通知可以在方法


    二、使用注解方式
    1、定義一個(gè)切面Aspect
     1package com.bjsxt.spring;
     2
     3import org.aspectj.lang.annotation.Aspect;
     4import org.aspectj.lang.annotation.Before;
     5import org.aspectj.lang.annotation.Pointcut;
     6
     7/**
     8 * 定義Aspect
     9 */

    10@Aspect //一個(gè)關(guān)注點(diǎn)的模塊化,這個(gè)關(guān)注點(diǎn)可能會(huì)橫切多個(gè)對(duì)象。
    11public class SecurityHandler {
    12    
    13    /**
    14     * 定義Pointcut,Pointcut的名稱就是allAddMethod,此方法不能有返回值和參數(shù),該方法只是一個(gè)
    15     * 標(biāo)識(shí)
    16     * 
    17     * Pointcut的內(nèi)容是一個(gè)表達(dá)式,描述那些對(duì)象的那些方法(訂閱Joinpoint)
    18     */

    19    @Pointcut("execution(* add*(..)) || execution(* del*(..)) || execution(* mod*(..))")
    20    private void allAddMethod(){};
    21    
    22    /**
    23     * 定義Advice,標(biāo)識(shí)在那個(gè)切入點(diǎn)何處織入此方法
    24     */

    25    @Before("allAddMethod()")
    26    private void checkSecurity() {
    27        System.out.println("----------checkSecurity()---------------");
    28    }

    29    
    30}

    31

    2、用戶管理業(yè)務(wù)處理接口
     1public interface UserManager {
     2
     3    public void addUser(String username, String password);
     4    
     5    public void deleteUser(int id);
     6    
     7    public void modifyUser(int id, String username, String password);
     8    
     9    public String findUserById(int id);
    10}

    3、接口實(shí)現(xiàn)
     1public class UserManagerImpl implements UserManager {
     2
     3    public void addUser(String username, String password) {
     4        System.out.println("-------UserManagerImpl.addUser()----------");
     5    }

     6
     7    public void deleteUser(int id) {
     8        System.out.println("-------UserManagerImpl.deleteUser()----------");
     9    }

    10
    11    public String findUserById(int id) {
    12        System.out.println("-------UserManagerImpl.findUserById()----------");
    13        return null;
    14    }

    15
    16    public void modifyUser(int id, String username, String password) {
    17        System.out.println("-------UserManagerImpl.modifyUser()----------");
    18    }

    19}


    4、spring bean
     1<beans xmlns="http://www.springframework.org/schema/beans"
     2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3         xmlns:aop="http://www.springframework.org/schema/aop"
     4         xmlns:tx="http://www.springframework.org/schema/tx"
     5         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
     6           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
     7           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     8    <aop:aspectj-autoproxy/>
     9    <bean id="securityHandler" class="com.bjsxt.spring.SecurityHandler"/>           
    10    <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/>
    11</beans>

    三、使用xml配置方式
           業(yè)務(wù)接口和業(yè)務(wù)接口實(shí)現(xiàn)同上面例子,需要改變的地方有兩處:
           1、切面Aspect
          
    1public class SecurityHandler {
    2    
    3    private void checkSecurity() {
    4        System.out.println("----------checkSecurity()---------------");
    5    }

    6}

         2、applicationContext.xml需要定義切面、切入點(diǎn)和通知
        
     1<beans xmlns="http://www.springframework.org/schema/beans"
     2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3         xmlns:aop="http://www.springframework.org/schema/aop"
     4         xmlns:tx="http://www.springframework.org/schema/tx"
     5         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
     6           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
     7           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     8    
     9    <bean id="securityHandler" class="com.bjsxt.spring.SecurityHandler"/>           
    10    
    11    <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/>
    12    
    13    <aop:config>
    14        <!-- 定義安全管理的方面 -->
    15        <aop:aspect id="security" ref="securityHandler">
    16            <!-- 定義切入點(diǎn) -->
    17            <aop:pointcut id="allAddMethod" expression="execution(* com.bjsxt.spring.UserManagerImpl.add*(..)))"/>
    18            <!-- 定義通知:before -->
    19            <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
    20        </aop:aspect>
    21    </aop:config>    
    22</beans>

              

            Aspect默認(rèn)情況下不用實(shí)現(xiàn)接口,但對(duì)于目標(biāo)對(duì)象(UserManagerImpl.java),在默認(rèn)情況下必須實(shí)現(xiàn)接口
            如果沒有實(shí)現(xiàn)接口必須引入CGLIB庫(kù)

            我們可以通過(guò)Advice中添加一個(gè)JoinPoint參數(shù),這個(gè)值會(huì)由spring自動(dòng)傳入,從JoinPoint中可以取得參數(shù)值、方法名等等
     

     1public class SecurityHandler {
     2    
     3    private void checkSecurity(JoinPoint joinPoint) {
     4        Object[] args = joinPoint.getArgs();
     5        for (int i=0; i<args.length; i++{
     6            System.out.println(args[i]);
     7        }

     8        
     9        System.out.println(joinPoint.getSignature().getName());
    10        System.out.println("----------checkSecurity()---------------");
    11    }

    12}

             結(jié)果顯示:
    1張三
    2123
    3addUser
    4----------checkSecurity()---------------
    5-------UserManagerImpl.addUser()----------
           
    主站蜘蛛池模板: 久久人午夜亚洲精品无码区| 亚洲国产成人99精品激情在线| 亚洲AV无码乱码在线观看富二代| 国产成人精品日本亚洲网站| 老汉色老汉首页a亚洲| 亚洲一区二区三区亚瑟| 无码亚洲成a人在线观看| 丰满少妇作爱视频免费观看| 中文字幕无码一区二区免费| 0588影视手机免费看片| 在线视频免费国产成人| 久久久久亚洲AV成人网人人软件 | 波多野结衣久久高清免费| 亚洲国产精品无码久久青草| 亚洲国产精品线在线观看| 国产午夜亚洲精品| 一区二区三区免费看| 99久久久国产精品免费牛牛四川 | 中文字幕亚洲激情| 亚洲美女色在线欧洲美女| 亚洲av午夜国产精品无码中文字 | 日本在线免费观看| 成年在线网站免费观看无广告| 亚洲精品亚洲人成在线观看下载| 亚洲2022国产成人精品无码区 | 亚洲第一综合天堂另类专 | 1000部禁片黄的免费看| 国产精品国产午夜免费福利看 | 亚洲国产精品尤物yw在线| 久久亚洲精品无码aⅴ大香| 福利片免费一区二区三区| 免费A级毛片无码A∨ | 久久伊人亚洲AV无码网站| 亚洲第一成年人网站| 九九九精品视频免费| aⅴ在线免费观看| 久久久久国产亚洲AV麻豆| 中文字幕乱码亚洲精品一区| 成人免费区一区二区三区 | 免费国产美女爽到喷出水来视频| 亚洲成年人在线观看|