初識Spring AOP

什麼是AOP

  • AOP爲Aspect Oriented Programming的縮寫,意爲:面向切面編程,經過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。利用AOP能夠對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度下降,提升程序的可重用性,同時提升了開發的效率。
  • 能夠經過預編譯方式和運行期動態代理實如今不修改源代碼的狀況下給程序動態統一添加功能的一種技術.

利用配置文件實現AOP

  1. 代理目標:誰將被其它對象代理,誰就是代理目標;
  2. 代理對象:誰將代理其它對象,誰就是代理對象;
  3. 鏈接點:
    • 執行點: 任意一個能夠執行的方法均可以看成一個執行點;
    • 方位:
      1. 方法執行前 ( before )
      2. 方法執行前和後 ( around )
      3. 方法拋出異常後 ( after-throw )
      4. 方法正常返回後 ( after-return )
      5. 方法執行後 ( after )
  4. 切點:在哪裏的方位加入代碼
  5. advices:加入怎樣的代碼

書寫步奏

  1. 創個一個實體類,其中必須寫入至少一個方法(子類代理父類,也可建立接口而後建立一個實現接口的類)
  2. 建立一個advices用來放置將要動態加入的代碼
  3. 建立一個Spring 映射文件 .xml
  4. 書寫一個測試類

具體代碼(idea實現)

  1. 書寫實體類:
public class Cat {

  private String name;

  public void eat(String food){
      System.out.println(name+"喜歡吃"+food);
  }

  public int division(int a,int b){
      int c=a/b;
      return c;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }
 }

2.書寫配置文件java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:aop="http://www.springframework.org/schema/aop"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

   <!--建立代理目標,誰將被代理-->
   <bean id="cat" class="com.lining.springproxy.Cat">
   <property name="name" value="湯姆"/>
   </bean>
   <!-- 肯定須要 添加的代碼所在的 bean-->
   <bean id="advices" class="com.lining.springproxy.CatAdivices"/>

<aop:config>
   <!--定義切面-->
   <!--能夠經過ref來肯定引入代碼的位置的bean-->
<aop:aspect ref="advices">
   <!-- 經過 aop:pointcut 來聲明一個【切點】 ( 這個切點在 aop:aspect 內部,因此是局部切點 ) -->
   <!--該標籤中的expresion是經常使用切點函數,是爲了指定處理函數,-->
   <!--execution(....)參數爲:修飾符,返回類型.方法名(參數),異常,指定哪個類的哪個方法爲切點-->
   <!--在多個表達式之間使用 ||,or表示 或,使用 &&,and表示 與,!表示 非.-->
   <!-- 在 包名中使用 .. 表示 多層路徑  -->
   <!-- 在 () 以前中使用 * 表示 某個類中的全部方法  -->
   <!-- 在 參數列表中使用 .. 表示 任意多個參數 ( 能夠是 零個、一個、多個)  -->
   <!-- 在 參數列表中使用 * 表示 至少有一個參數 ( 能夠是 一個 或 多個)  -->
   <aop:pointcut id="first" expression="execution(* com.lining.springproxy.Cat.*(..))"/>
   <!-- 在  firstPointcut 所選擇 【鏈接點】( 指定的 執行點 執行以前 ) 處 加入prompter中的 before 方法 對應的代碼 -->
   <!-- 在 firstPointcut 所選擇的那些方法以前前 先執行  prompter 對象 before 方法-->
   <aop:before method="before" pointcut-ref="first" />
   <aop:after-throwing method="exe" pointcut-ref="first" />
   <aop:around method="round" pointcut-ref="first" />
   <aop:after-returning method="areturn" pointcut-ref="first"  />
   <aop:after method="after" pointcut-ref="first" />
</aop:aspect>
</aop:config>

</beans>
  1. 書寫 advice
package com.lining.springproxy;

import org.aspectj.lang.ProceedingJoinPoint;

public class CatAdivices {

    public void before(){
        System.out.println("我是before");
    }
    public void exe(){
        System.out.println("我是異常exeception");
    }
    public void areturn(){
        System.out.println("我是return");
    }
    public Object round(ProceedingJoinPoint joinPoint ) throws Throwable{
       System.out.println("我圍繞在上面");
       Object s=joinPoint.proceed();
       System.out.println("我圍繞在下面");
       return s;
    }
    public void after(){
        System.out.println("我是after");
    }
}

4.書寫測試類spring

public class CatTest {
    public static void main(String[] args) {
        AbstractApplicationContext context=new ClassPathXmlApplicationContext("com/lining/AOPproxy.xml");
        Object proxy= context.getBean("cat");
        if(proxy instanceof Cat){
            Cat s=(Cat)proxy;
            s.eat("小龍蝦");
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            s.division(6,0);
        }
        context.close();
    }
    }
  1. 輸出結果
我是before
我圍繞在上面
湯姆喜歡吃小龍蝦
我是after
我是return
我圍繞在下面
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我是before
我圍繞在上面
我是after
我是異常exeception
Exception in thread "main" java.lang.ArithmeticException: / by zero

總結

以上即是具體的實現過程,其中主要用到了動態代理的思想,在每個方位上添加代碼,這樣能夠減小之後項目維護時的複雜程度。咱們也能夠用標籤的方式來書寫AOP,可是做爲初學者仍是先弄懂配置文件的書寫過程在學習Sping時,咱們也能夠用原生的jdk來書寫動態代理實現AOP.這些方法咱們將在下一篇中講到。 ** 要先學會代理否則你是看不懂這些的**express

相關文章
相關標籤/搜索