AOP小結

  • AOP概述:

           AOP(Aspect Oriented Programming),即面向切面編程。可以說是OOP(Object Oriented Programming,面向對象編程的切入點)的補充和完善。

        OOP引入封裝、繼承、多態等概念來建立一種對象層次結構,用於模擬公共行爲的一個集合,不過在OOP允許開發者自定義縱向的關係,但並不適合橫向的關係,例如日誌功能。日誌代碼往往橫向的散步在所有對象層次中,而與他對應的對象的核心功能毫無關係對於其他類型的代碼,如安全性、異常處理和透明的持續性也是如此,這種散佈在各處的無關的代碼被稱爲橫切(Cross cutting),在OOP設計中,它會導致大量重複代碼,並不利於各模塊的重用。

        AOP技術與之相反,它利用一種稱爲“橫切”的技術,剖解開封裝的對象內部,並將那些影響了多個類的公共行爲封裝到一個可重用模塊,並將其命名爲“Aspect”,即切面。所謂切面,簡單說就是將那些與業務無關,卻爲業務模塊所共同調用的邏輯或責任封裝起來,便於減少系統的重用代碼,降低模塊之間的耦合度,並利於未來的可操作性和可維護性。

        使用“橫切”技術,AOP把軟件分爲兩個部分:“核心關注點”和“橫切關注點”,業務處理的主要流程是核心關注點,與之關係不大的是橫切關注點。橫切關注點的一個特點是:他們經常發生在覈心關注點的多處,而各處基本相似,比如權限認證、日誌、事務,AOP的作用在於分離系統中的各個關注點,將核心關注點和橫切關注點分離開來。

  • AOP核心概念

1.橫切關注點:
         對哪些方法進行攔截,攔截後怎麼處理,這些關注點稱爲橫切關注點。

2.切面(Aspect):

         類是對事物特徵的抽象,切面就是對橫切關注點的抽象。

3.連接點(joinpoint)

         被攔截到的點,因爲Spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或構造器。

4.切入點(pointcut):

         對連接點進行攔截的定義。

5.通知(advice):

所謂通知指的就是指攔截到連接點之後要執行的代碼,通知分爲前置、後置、異常、最終、環繞通知五類。

6.目標對象

         代理的目標對象。

7.織入(weave)

         將切面應用到目標對象並導致代理對象創建的過程。

8.引入(introduction)

         在不修改代碼的前提下,引入可以在運行期爲類動態地添加一些方法或字段。

  • Spring對AOP的支持

        Spring中AOP代理由Spring的IOC容器負責生成,管理,其依賴關係也是由IOC容器負責管理。因此,AOP代理可以直接使用容器中的其他bean實例作爲目標,這種關係可由IOC容器的依賴注入提供。

Spring創建代理的規則爲:

  1. 默認使用Java動態代理來創建AOP代理,這樣就可以爲任何接口創建代理了。
  2. 當需要代理的類不是代理接口時,Spring會切換成使用cglib代理,也可強制使用cglib

AOP編程:我們需要參與的部分:

  1. 定義普通業務組件。
  2. 定義切入點,一個切入點可能橫切多個業務組件。
  3. 定義增強處理,增強處理就是在AOP框架爲普通業務組件織入的處理動作。

       所有AOP編程的關鍵就是定義切入點和定義增強處理,一旦定義了合適的切入點和增強處理,AOP框架將自動生成AOP代理,即:代理對象的方法=增強處理+被代理對象的方法。

  • 基於Spring的AOP的簡單實現

前提:需要spring的jar包+aopalliance.jar+aspectjweaver.jar

先定義一個接口:

public interface HelloWorld
{
   void printHelloWorld();
   void doPrint();
}

再定義兩個實現類:

public class HelloWorldImpl1 implements HelloWorld
{
   public void printHelloWorld()
   {
       System.out.println("Enter HelloWorldImpl1.printHelloWorld()");
   }
   
   public void doPrint()
   {
       System.out.println("Enter HelloWorldImpl1.doPrint()");
       return ;
   }
}
public class HelloWorldImpl2 implements HelloWorld
{
   public void printHelloWorld()
   {
       System.out.println("Enter HelloWorldImpl2.printHelloWorld()");
   }
   
   public void doPrint()
   {
       System.out.println("Enter HelloWorldImpl2.doPrint()");
       return ;
   }
}

橫切關注點,這裏是打印時間:

public class TimeHandler
{
   public void printTime()
   {
       System.out.println("CurrentTime = " + System.currentTimeMillis());
   }
}

有這三個類就可以實現一個簡單的SpringAOP了,看一下xml 配置:

<?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"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
       
       <bean id="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" />
       <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" />
       <bean id="timeHandler" class="com.xrq.aop.TimeHandler" />
       
       <aop:config>
           <aop:aspect id="time" ref="timeHandler">
               <aop:pointcut id="addAllMethod" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
               <aop:before method="printTime" pointcut-ref="addAllMethod" />
               <aop:after method="printTime" pointcut-ref="addAllMethod" />
           </aop:aspect>
       </aop:config>
</beans>

寫個main函數調用一下:

public static void main(String[] args)
{
   ApplicationContext ctx = 
           new ClassPathXmlApplicationContext("aop.xml");
       
   HelloWorld hw1 = (HelloWorld)ctx.getBean("helloWorldImpl1");
   HelloWorld hw2 = (HelloWorld)ctx.getBean("helloWorldImpl2");
   hw1.printHelloWorld();
   System.out.println();
   hw1.doPrint();
   
   System.out.println();
   hw2.printHelloWorld();
   System.out.println();
   hw2.doPrint();
}

運行結果:

CurrentTime = 1446129611993
Enter HelloWorldImpl1.printHelloWorld()
CurrentTime = 1446129611993

CurrentTime = 1446129611994
Enter HelloWorldImpl1.doPrint()
CurrentTime = 1446129611994

CurrentTime = 1446129611994
Enter HelloWorldImpl2.printHelloWorld()
CurrentTime = 1446129611994

CurrentTime = 1446129611994
Enter HelloWorldImpl2.doPrint()
CurrentTime = 1446129611994

看到給helloworld接口的兩個實現類的所有方法都加上了代理,代理內容就是打印時間。

  • 基於Spring的AOP的其他細節:

增加一個橫切關注點,打印日誌,java類爲:

public class LogHandler
{
   public void LogBefore()
   {
       System.out.println("Log before method");
   }
   
   public void LogAfter()
   {
       System.out.println("Log after method");
   }
}

Xml配置爲:

<?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"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
       
       <bean id="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" />
       <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" />
       <bean id="timeHandler" class="com.xrq.aop.TimeHandler" />
       <bean id="logHandler" class="com.xrq.aop.LogHandler" />
       
       <aop:config>
           <aop:aspect id="time" ref="timeHandler" order="1">
               <aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
               <aop:before method="printTime" pointcut-ref="addTime" />
               <aop:after method="printTime" pointcut-ref="addTime" />
           </aop:aspect>
           <aop:aspect id="log" ref="logHandler" order="2">
               <aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
               <aop:before method="LogBefore" pointcut-ref="printLog" />
               <aop:after method="LogAfter" pointcut-ref="printLog" />
           </aop:aspect>
       </aop:config>
</beans>

測試類不變,打印結果爲:

CurrentTime = 1446130273734
Log before method
Enter HelloWorldImpl1.printHelloWorld()
Log after method
CurrentTime = 1446130273735

CurrentTime = 1446130273736
Log before method
Enter HelloWorldImpl1.doPrint()
Log after method
CurrentTime = 1446130273736

CurrentTime = 1446130273736
Log before method
Enter HelloWorldImpl2.printHelloWorld()
Log after method
CurrentTime = 1446130273736

CurrentTime = 1446130273737
Log before method
Enter HelloWorldImpl2.doPrint()
Log after method
CurrentTime = 1446130273737

要想讓logHandler在timeHandler前使用有兩個辦法:

  1. aspect裏面有一個order屬性,order屬性的數字就是橫切關注點的順序。
  2. 把logHandler定義在timehandler前面,spring默認以aspect的定義順序作爲織入順序

 

若只想織入接口中的某些方法:

修改一些pointcut的expressin即可:

<?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"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
       
       <bean id="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" />
       <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" />
       <bean id="timeHandler" class="com.xrq.aop.TimeHandler" />
       <bean id="logHandler" class="com.xrq.aop.LogHandler" />
       
       <aop:config>
           <aop:aspect id="time" ref="timeHandler" order="1">
               <aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.print*(..))" />
               <aop:before method="printTime" pointcut-ref="addTime" />
               <aop:after method="printTime" pointcut-ref="addTime" />
           </aop:aspect>
           <aop:aspect id="log" ref="logHandler" order="2">
               <aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.do*(..))" />
               <aop:before method="LogBefore" pointcut-ref="printLog" />
               <aop:after method="LogAfter" pointcut-ref="printLog" />
           </aop:aspect>
       </aop:config>
</beans>

表示timehandler只會織入helloworld接口print開頭的方法,logHandler只會織入helloworld接口do開頭的方法。

  1. 強制使用cglib生成代理:

Proxy-target-class屬性,如果此屬性設置爲true,那麼基於類的代理將起作用,如果爲false或者省略這個屬性,那麼基於接口的代理將起作用。

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