SpringAOP的使用詳解

什么是AOP

創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目成都網(wǎng)站制作、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元古縣做網(wǎng)站,已為上家服務,為古縣各地企業(yè)和個人服務,聯(lián)系電話:18982081108

AOP(Aspect Oriented Programming 面向切面編程),通過預編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務邏輯的各個部分進行隔離,從而使得業(yè)務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。

常用于日志記錄,性能統(tǒng)計,安全控制,事務處理,異常處理等等。

定義AOP術(shù)語

切面(Aspect):切面是一個關(guān)注點的模塊化,這個關(guān)注點可能是橫切多個對象;

連接點(Join Point):連接點是指在程序執(zhí)行過程中某個特定的點,比如某方法調(diào)用的時候或者處理異常的時候;

通知(Advice):指在切面的某個特定的連接點上執(zhí)行的動作。Spring切面可以應用5中通知:

  • 前置通知(Before):在目標方法或者說連接點被調(diào)用前執(zhí)行的通知;
  • 后置通知(After):指在某個連接點完成后執(zhí)行的通知;
  • 返回通知(After-returning):指在某個連接點成功執(zhí)行之后執(zhí)行的通知;
  • 異常通知(After-throwing):指在方法拋出異常后執(zhí)行的通知;
  • 環(huán)繞通知(Around):指包圍一個連接點通知,在被通知的方法調(diào)用之前和之后執(zhí)行自定義的方法。

切點(Pointcut):指匹配連接點的斷言。通知與一個切入點表達式關(guān)聯(lián),并在滿足這個切入的連接點上運行,例如:當執(zhí)行某個特定的名稱的方法。

引入(Introduction):引入也被稱為內(nèi)部類型聲明,聲明額外的方法或者某個類型的字段。

目標對象(Target Object):目標對象是被一個或者多個切面所通知的對象。

AOP代理(AOP Proxy):AOP代理是指AOP框架創(chuàng)建的對對象,用來實現(xiàn)切面契約(包括通知方法等功能)

織入(Wearving):指把切面連接到其他應用出程序類型或者對象上,并創(chuàng)建一個被通知的對象?;蛘哒f形成代理對象的方法的過程。

Spring對AOP的支持

  1. 基于代理的經(jīng)典SpringAOP;
  2. 純POJO切面;
  3. @AspectJ注解驅(qū)動的切面;
  4. 注入式AspectJ切面(適用于Spring各版本);

前三種都是SpringAOP實現(xiàn)的變體,SpringAOP構(gòu)建在動態(tài)代理基礎之上,因此,Spring對AOP的支持局限于方法的攔截。

切入點表達式

使用SpringAOP

SpringAOP的支持必須呀導入spring-aspects的jar包

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.3.5.RELEASE</version>
</dependency>

使用注解定義切面

采用注解的方式定義切面以及通知

@Aspect
public class Audience {
  //使用@Pointcut注解聲明頻繁使用的切入點表達式
  @Pointcut("execution(* com.wqh.concert.Performance.perform(..))")
  public void performance(){}
  
  @Before("performance()")
  public void silenceCellPhones(){
    System.out.println("Sillencing cell phones");
  }
  @Before("performance()")
  public void takeSeats(){
    System.out.println("Task Seat");
  }
  @AfterReturning("performance()")
  public void applause(){
    System.out.println("CLAP CLAP CLAP");
  }
  @AfterThrowing("performance()")
  public void demandRefund(){
    System.out.println("Demand a Refund");
  }
}

另外需要在applicationContext.xml也就是spring的配置文件中添加配置:

<!--啟用AspectJ的自動代理-->
<aop:aspectj-autoproxy/>
<!--聲明bean-->
<bean class="com.wqh.concert.Audience"/>

在XML中聲明切面

定義pojo類,這里只是把上面定義的注解全public class AudienceXML {

public void silenceCellPhones() {
  System.out.println("Sillencing cell phones");
}
public void takeSeats() {
  System.out.println("Task Seat");
}
public void applause() {
  System.out.println("CLAP CLAP CLAP");
}
public void demandRefund() {
  System.out.println("Demand a Refund");
}

applicationContext.xml配置

<!--聲明bean-->
<bean name="audienceXML" class="com.wqh.concert.AudienceXML"/>
<aop:config>
  <!--引入bean-->
  <aop:aspect ref="audienceXML">
    <!--定義切點-->
    <aop:pointcut id="perform"
           expression="execution(* com.wqh.concert.Performance.perform(..))"/>
    <!--定義通知
      method:通知,也就是具體的方法
      pointcut-ref:引用的切點
      pointcut:切點-->
    <aop:before method="silenceCellPhones"
          pointcut-ref="perform"/>
    <aop:before method="takeSeats" pointcut-ref="perform"/>
    <aop:after-returning method="applause" pointcut-ref="perform"/>
    <aop:after-throwing method="demandRefund"
              pointcut="execution(* com.wqh.concert.Performance.perform(..))"/>
  </aop:aspect>
</aop:config>

環(huán)繞通知

在springAOP中有五種通知,環(huán)繞通知是最為強大的通知。它能夠讓你編寫的邏輯將被通知的目標方法完全包裝起來。實際上就像在一個通知方法中同時編寫前置通知和后置通知。
本片文章具體講解環(huán)繞通知的使用。

使用注解

使用環(huán)繞通知定義切面:

@Aspect
public class AudienceAround {
  //使用@Pointcut注解聲明頻繁使用的切入點表達式
  @Pointcut("execution(* com.wqh.concert.Performance.perform(..))")
  public void performance(){}
  @Around("performance()")
  public void watchPerformance(ProceedingJoinPoint joinPoint){
    try {
      System.out.println("Silencing cell phones");
      System.out.println("Taking seats");
      joinPoint.proceed();
      System.out.println("Demanding a refund");
    } catch (Throwable throwable) {
      throwable.printStackTrace();
    }
  }
}

可以看到在上面的代碼中,定義通知的時候在通知方法中添加了入?yún)ⅲ篜roceedingJoinPoint。在創(chuàng)建環(huán)繞通知的時候,這個參數(shù)是必須寫的。因為在需要在通知中使用ProceedingJoinPoint.proceed()方法調(diào)用被通知的方法。

另外,如果忘記調(diào)用proceed()方法,那么通知實際上會阻塞對被通知方法的調(diào)用。

在XML中定義

首先去掉上面類的所有注解:這里為了區(qū)別就重新創(chuàng)建一個類

public class AudienceAroundXML {
  public void watchPerformance(ProceedingJoinPoint joinPoint){
    try {
      System.out.println("Silencing cell phones");
      System.out.println("Taking seats");
      joinPoint.proceed();
      System.out.println("Demanding a refund");
    } catch (Throwable throwable) {
      throwable.printStackTrace();
    }
  }
}

配置:

<!--聲明bean-->
<bean name="audienceAroundXML" class="com.wqh.concert.AudienceAroundXML"/>
 <!--配置切面及通知-->
<aop:config>
  <aop:aspect ref="audienceAroundXML">
    <aop:pointcut id="performance"
           expression="execution(* com.wqh.concert.Performance.perform(..))"/>
    <aop:around method="watchPerformance" pointcut-ref="performance"/>
  </aop:aspect>
</aop:config>

處理通知中的參數(shù)

Spring借助AspectJ的切點表達式語言來定義Spring切面

在spring中嘗試使用其他指示器時,會拋出IllegalArgument-Exception異常。

如上的這些指示器,只有exception指示器是實際執(zhí)行匹配的,而其他都是用來限制匹配的。

切面表達式分析

帶參數(shù)的切點表達式分解

在該切點表達式中使用了args(trackNumber)限定符。表示傳遞給playTrack()方法的int類型參數(shù)也會傳遞到通知中去。參數(shù)名trackNumber也與切點方法簽名中的參數(shù)相匹配。

創(chuàng)建切面

@Aspect
public class TrackCounter {
  @Pointcut("execution(* com.wqh.aop.CompactDisc.playTrack(int))&&args(trackNumber)")
  public void trackPlayder(int trackNumber){}
  @Before("trackPlayder(trackNumber)")
  public void countTrack(int trackNumber) {
    System.out.println("前置通知:targetNumber=" + trackNumber);
  }
}

連接點類

@Service
public class CompactDisc {
  public void playTrack(int trackNumber){
    System.out.println("trackNumber =" + trackNumber);
  }
}

XML配置

<!--啟用AspectJ的自動代理-->
<aop:aspectj-autoproxy/>
<!--聲明bean-->
 <bean class="com.wqh.aop.TrackCounter"/>
 <!--自動掃描包下的類-->
<context:component-scan base-package="com.wqh.aop"/>

測試

@Test
public void testT(){
  ApplicationContext applicationContext =
      new ClassPathXmlApplicationContext(
          new String[]{"classpath:/spring/applicationContext.xml"});
  CompactDisc compactDisc = (CompactDisc) applicationContext.getBean("compactDisc");
  compactDisc.playTrack(12);
}

上面給指定方法傳入的參數(shù)是12,在通知中獲取到了該參數(shù)

另外:在xml中配置切面來處理通知中的參數(shù),其實也差不多,只是把切點表達式放到了XML配置文件中。

給類添加新的功能

引入Spring實戰(zhàn)中的知識

在SpringAOP中,我們可以為Bean引入新的方法。代理攔截器調(diào)用并委托給實現(xiàn)該方法的其他對象。

當引入接口的方法被調(diào)用時,代理會把此調(diào)用委托給實現(xiàn)了新接口的某給其他對象。

使用注解方式引入

代碼
首先是連接點的接口及其實現(xiàn)類

public interface Person {
  void say();
}
public class ChinesePerson implements Person {
  @Override
  public void say() {
    System.out.println("說中文");
  }
}

創(chuàng)建需要添加的功能,這里個人類擴展一個吃的功能

public interface Food {
  void eat();
}
public class ChineseFood implements Food {
  @Override
  public void eat() {
    System.out.println("吃中餐");
  }
}

編寫切面

@Aspect
public class addFuction {
  @DeclareParents(value = "com.wqh.addfunction.Person+",defaultImpl = ChineseFood.class)
  public static Food food;
}

注意這里的表達式使用的式@DeclareParents注解;該注解所標注的靜態(tài)屬性指明了要引入的接口。

注解中使用的value屬性指定哪種類型的bean要引入該接口,這里Person后后面的“+”號表示所有子類型,而不是該類的本身。defaultImpl,指定了為引入功能提供實現(xiàn)的類。

使用XML配置bean:

<!--啟用AspectJ的自動代理-->
<aop:aspectj-autoproxy/>
<!--聲明bean-->
<bean class="com.wqh.addfunction.addFuction"/>
<bean name="chinesePerson" class="com.wqh.addfunction.ChinesePerson"/>

測試

@Test
public void testAdd(){
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
      "classpath:spring/applicationContext.xml");
  Person person = (Person) applicationContext.getBean("chinesePerson");
  person.say();
  //這里可以將chinesePerson bean轉(zhuǎn)換為Food類,所以添加成功
  Food food = (Food) applicationContext.getBean("chinesePerson");
  food.eat();
}

在XML中引入

首先將上面的addFuction注解全部刪除,其他不變;然后在xml中添加相應的配置:

<!--啟用AspectJ的自動代理-->
<aop:aspectj-autoproxy/>
<!--聲明bean-->
<bean name="chinesePerson" class="com.wqh.addfunction.ChinesePerson"/>
 <aop:config>
   <aop:aspect>
     <aop:declare-parents types-matching="com.wqh.addfunction.Person+"
               implement-interface="com.wqh.addfunction.Food"
     default-impl="com.wqh.addfunction.ChineseFood"/>
   </aop:aspect>
 </aop:config>

這里的types-matching與上面的vale作用一樣;

default-impl與defaultImpl作用一樣,這也可以使用delegate-ref;當然如果使用delegate-ref則是要引用SpringBean;

implement-interface則是要引入的接口

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:SpringAOP的使用詳解
瀏覽路徑:http://muchs.cn/article12/gphcdc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、企業(yè)網(wǎng)站制作、外貿(mào)建站、App設計虛擬主機、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設