Spring AOP @AspectJ 入門實例

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

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

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

 1. 一個最簡單的AOPxml

     共有2步。get

     1.1 定義一個Aspectit

    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. }io

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

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

     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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://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。

相關文章
相關標籤/搜索