Spring AOP 的@Aspect

轉自:http://blog.csdn.net/tanghw/article/details/3862987

從Spring 2.0開始,可以使用基於schema及@AspectJ的方式來實現AOP,本文以一個簡單的實例介紹瞭如何以@AspectJ方式在Spring中實現AOP。由於@Aspect是基於註解的,因此要求支持註解的5.0版本以上的JDK。

 環境要求:
    1. Web應用
    2. 有一個專門提供系統服務的Service層

 我們的目標是,如果用戶調用Service層中任一方法,都在其插入一個記錄信息的功能。

 1. 一個最簡單的AOP

     共有2步。

     1.1 定義一個Aspect

    1.  package com.sarkuya.aop.aspect;
    2.  import org.aspectj.lang.annotation.Aspect;
    3.  import org.aspectj.lang.annotation.Before;
    4.  @Aspect
    5.  public class SampleAspect {
    6.      @Before("execution(* com.sarkuya.service..*.*(..))")
    7.      public void doBeforeInServiceLayer() {
    8.          System.out.println("=====================================");
    9.          System.out.println("Aop: do before in Service layer");
    10.         System.out.println("=====================================");
    11.   }
    12. }

    第4行,必須使用@Aspect在類名之前註解。

    第6行,當用戶調用com.sarkuya.service包中任一類的任一方法,在調用前,Spring將自動執行下面的doBeforeInServiceLayer()方法,此方法只是簡單地打印一些信息。

     1.2 在Spring配置文件applicationContext.xml中配置

     <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

         <aop:aspectj-autoproxy />
        <bean class="com.sarkuya.aop.aspect.SampleAspect" />

         <!-- ================ YOUR CONTENTS GOES BELOW =================== -->
    </bean>

     就這麼簡單。

 2. 將Pointcut及Advice分開

     上面的Aspect中混雜了Pointcut及Advice,因此最好將其分開。共有3步。

     2.1 定義Pointcut

     1.  package com.sarkuya.aop.aspect;
    2.  import org.aspectj.lang.annotation.Aspect;
    3.  import org.aspectj.lang.annotation.Pointcut;
    4.  @Aspect
    5.  public class SampleAspect {
    6.      @Pointcut("execution(* com.sarkuya.service..*.*(..))")
    7.      public void inServiceLayer() {
    8.      }
    9.  }

    Pointcut是植入Advice的觸發條件。每個Pointcut的定義包括2部分,一是表達式,如第6行;二是方法簽名,如第7行。方法簽名必須是 public及void型。可以將Pointcut中的方法看作是一個被Advice引用的助記符,因爲表達式不直觀,因此我們可以通過方法簽名的方式爲 此表達式命名。因此Pointcut中的方法只需要方法簽名,而不需要在方法體內編寫實際代碼。

     2.2 定義Advice

    1.  package com.sarkuya.aop.advice;
    2.  import org.aspectj.lang.annotation.Aspect;
    3.  import org.aspectj.lang.annotation.Before;
    4.  @Aspect
    5.  public class SampleAdvice {
    6.      @Before("com.sarkuya.aop.aspect.SampleAspect.inServiceLayer()")
    7.      public void logInfo() {
    8.          System.out.println("=====================================");
    9.          System.out.println("Aop: do before in service layer");
    10.         System.out.println("=====================================");
    11.     }
    12. }

     第4行,對於Advice,也只能使用@Aspect來註解。

    第6行,與第1.1節中第6行不同,這次不是直接使用Pointcut的表達式,而是使用了Pointcut中的方法簽名。

    單獨定義Pointcut的好處是,一是通過使用有意義的方法名,而不是難讀的Pointcut表達式,使代碼更加直觀;二是Pointcut可以實現共 享,被多個Advice直接調用。若有多個Advice調用某個Pointcut,而這個Pointcut的表達式在將來有改變時,只需修改一個地方,維 護更加方便。

    第7行,我們將Advice的方法法改爲logInfo(),以更加明確此Advice的作用。

     2.3 配置文件

    <aop:aspectj-autoproxy />
    <bean class="com.sarkuya.aop.advice.SampleAdvice" />

     只需配置SampleAdvice,無需配置SampleAspect。

 3. 重構:明確Pointcut職責

     對於SampleAspect來說,其主要職責是定義Pointcut,可以在此類中同時定義多個Pointcuts。但其類名反映不出這個特點,因此,應將其重構以明確其職責。

    package com.sarkuya.aop.pointcut;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    @Aspect
    public class PointcutsDefinition {
        @Pointcut("execution(* com.sarkuya.service..*.*(..))")
        public void inServiceLayer() {
        }
    }

     將SampleAspect重命名爲PointcutsDefinition,並移到com.sarkuya.aop.pointcut包中。

     對於SampleAdvice來說,只需改變@Before()的註解,指向
    @Before("com.sarkuya.aop.pointcut.PointcutsDefinition.inServiceLayer()")

     而Spring配置文件保持不變。

 小結:
    我們先從一個最簡單的Aspect實例開始,瞭解AOP的作用及最基本的要求,再重構爲更有意義的代碼,明確了AOP中的Pointcut及Advice的概念,有助於我們構建更復雜的Aspect。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章