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

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

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

    城市獵人

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

    spring aop總結

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

    一、Spring AOP相關術語

    • 切面(Aspect): 一個關注點的模塊化,這個關注點可能會橫切多個對象。事務管理是J2EE應用中一個關于橫切關注點的很好的例子。 在Spring AOP中,切面可以使用通用類(基于模式的風格) 或者在普通類中以 @Aspect 注解(@AspectJ風格)來實現。

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

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

    • 切入點(Pointcut): 匹配連接點(Joinpoint)的斷言。通知和一個切入點表達式關聯,并在滿足這個切入點的連接點上運行(例如,當執行某個特定名稱的方法時)。 切入點表達式如何和連接點匹配是AOP的核心:Spring缺省使用AspectJ切入點語法。

    • 引入(Introduction): (也被稱為內部類型聲明(inter-type declaration))。聲明額外的方法或者某個類型的字段。 Spring允許引入新的接口(以及一個對應的實現)到任何被代理的對象。 例如,你可以使用一個引入來使bean實現 IsModified 接口,以便簡化緩存機制。

    • 目標對象(Target Object): 被一個或者多個切面(aspect)所通知(advise)的對象。也有人把它叫做 被通知(advised) 對象。 既然Spring AOP是通過運行時代理實現的,這個對象永遠是一個 被代理(proxied) 對象。

    • AOP代理(AOP Proxy): AOP框架創建的對象,用來實現切面契約(aspect contract)(包括通知方法執行等功能)。 在Spring中,AOP代理可以是JDK動態代理或者CGLIB代理。 注意:Spring 2.0最新引入的基于模式(schema-based)風格和@AspectJ注解風格的切面聲明,對于使用這些風格的用戶來說,代理的創建是透明的。

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

    通知的類型:

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

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

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

    • 后通知(After (finally) advice): 當某連接點退出的時候執行的通知(不論是正常返回還是異常退出)。

    • 環繞通知(Around Advice): 包圍一個連接點(join point)的通知,如方法調用。這是最強大的一種通知類型。 環繞通知可以在方法


    二、使用注解方式
    1、定義一個切面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 //一個關注點的模塊化,這個關注點可能會橫切多個對象。
    11public class SecurityHandler {
    12    
    13    /**
    14     * 定義Pointcut,Pointcut的名稱就是allAddMethod,此方法不能有返回值和參數,該方法只是一個
    15     * 標識
    16     * 
    17     * Pointcut的內容是一個表達式,描述那些對象的那些方法(訂閱Joinpoint)
    18     */

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

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

    29    
    30}

    31

    2、用戶管理業務處理接口
     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、接口實現
     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配置方式
           業務接口和業務接口實現同上面例子,需要改變的地方有兩處:
           1、切面Aspect
          
    1public class SecurityHandler {
    2    
    3    private void checkSecurity() {
    4        System.out.println("----------checkSecurity()---------------");
    5    }

    6}

         2、applicationContext.xml需要定義切面、切入點和通知
        
     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            <!-- 定義切入點 -->
    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默認情況下不用實現接口,但對于目標對象(UserManagerImpl.java),在默認情況下必須實現接口
            如果沒有實現接口必須引入CGLIB庫

            我們可以通過Advice中添加一個JoinPoint參數,這個值會由spring自動傳入,從JoinPoint中可以取得參數值、方法名等等
     

     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}

             結果顯示:
    1張三
    2123
    3addUser
    4----------checkSecurity()---------------
    5-------UserManagerImpl.addUser()----------
           
    主站蜘蛛池模板: 亚洲情A成黄在线观看动漫软件| 亚洲一级毛片在线观| 嫩草影院在线播放www免费观看| 亚洲成人在线免费观看| 日日夜夜精品免费视频| 免费看成人AA片无码视频吃奶| 亚洲自国产拍揄拍| 国产性爱在线观看亚洲黄色一级片| 精品熟女少妇a∨免费久久| 亚洲爆乳无码精品AAA片蜜桃| 亚洲色中文字幕无码AV| 成人五级毛片免费播放| 国产午夜成人免费看片无遮挡| 亚洲熟妇自偷自拍另欧美| 国产国拍亚洲精品mv在线观看| 最近免费中文字幕大全视频| 成人精品视频99在线观看免费| 亚洲va在线va天堂成人| 亚洲熟妇无码AV在线播放| 日韩成全视频观看免费观看高清| 三年片在线观看免费西瓜视频| 亚洲国产av玩弄放荡人妇| 亚洲av之男人的天堂网站| 日韩毛片免费在线观看| 最近中文字幕2019高清免费| 男女超爽视频免费播放| 亚洲国产精品成人久久久| 亚洲成AV人片在线观看WWW| 国产18禁黄网站免费观看| 免费下载成人电影| 野花香高清视频在线观看免费| 精品国产亚洲一区二区三区在线观看 | 中文字幕a∨在线乱码免费看| 在线精品亚洲一区二区| 久久久久亚洲精品美女| 亚洲高清偷拍一区二区三区| 毛片免费全部播放一级| 1000部拍拍拍18免费网站| 久久免费观看国产精品| 国产黄色片免费看| 无忧传媒视频免费观看入口|