天天看點

Spring面向切面程式設計(AOP)1.前言說明2.應用場景3.執行個體說明4.運作結果5.參考資料

轉載請注明來源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/71747299,建議讀者閱讀原文,確定獲得完整的資訊

1.前言說明

  AOP即Aspect-Oriented Programming,面向切面的程式設計,與OOP想對應,是OOP的補充和完善,OOP橫向上區分一個個類,AOP則從縱向上考察對象。AOP是一種動态地将代碼切入到類的指定方法、指定位置上的程式設計思想。

  AOP一方面實作松耦合,使業務代碼更加純粹,即減少業務無關代碼;另一方面實作統籌管理,提升業務開發效率,即可以随時給所有業務新增附加功能,而無須修改即有業務代碼,這也展現了開閉原則,即對擴充開放,修改關閉。

2.應用場景

  • Log日志管理
  • Authentication 權限
  • Caching 緩存
  • Context passing 内容傳遞
  • Error handling 錯誤處理
  • Lazy loading 懶加載
  • Debugging 調試
  • Logging, tracing, profiling and monitoring 記錄跟蹤 優化 校準
  • Performance optimization 性能優化
  • Persistence 持久化
  • Resource pooling 資源池
  • Synchronization 同步
  • Transactions 事務

3.執行個體說明

  本文通過一個執行個體來說明SpringBoot中AOP的配置方式,至于其他的架構的配置方法,沒有什麼大差異,主要差一點會在依賴包上,Java方法類及AOP配置沒有大差别。

  本文執行個體實作在指定包中的方法執行 前後執行切面方法,記錄下方法輸入、輸出參數。

  第一步:pom.xml中添加依賴

<dependency>  
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
           

  添加完上述依賴後,maven會自動下載下傳依賴及間接依賴。如下圖所示,SpringBoot AOP依賴包含3個依賴,spring-boot-starter-aop-1.3.6.RELEASE.jar、spring-sop-4.2.7.RELEASE.jar、aspectjweaver-1.8.9.jar。

Spring面向切面程式設計(AOP)1.前言說明2.應用場景3.執行個體說明4.運作結果5.參考資料

  第二步:建立切面方法

public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    // 前置通知
    public void beforeAdvice(JoinPoint joinPoint){
        logger.info("============before advice");
        logger.info(joinPoint.getSignature().getDeclaringTypeName() + " [input parameters] " + Arrays.toString(joinPoint.getArgs()));
    }

    // 後置通知
    public void afterAdvice(JoinPoint joinPoint, Object retValue){
        logger.info("============after advice");
        logger.info(joinPoint.getSignature().getDeclaringTypeName() + " [output parameters] " + retValue);
    }

}
           

  上述類中,有兩個方法,分别對應前置和後置兩種模式執行的方法。前置方法實作輸入參數列印,後置方法實作輸出參數列印。

  第三步:新增AOP配置

  在已有的beans.xml檔案中新增aop配置,由于原來的beans.xml不支援aop配置,是以需要對該檔案做2點調整:

  • 新增頭配置xmlns:aop、xsi:schemaLocation中關于aop的配置。
  • 添加aop切面及模式配置。

  本例中的配置僅截取關鍵配置項。

<?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-3.0.xsd">

    <bean id="logAspect" class="com.loongshawn.aop.LogAspect"/>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.loongshawn.method.ces..*.*(..))" />
        <aop:aspect ref="logAspect">
            <aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
            <aop:after-returning pointcut-ref="pointcut" arg-names="joinPoint,retValue" returning="retValue" method="afterAdvice"/>
        </aop:aspect>
    </aop:config>

</beans>
           

  配置參數解釋說明:

  • aop.pointcut,橫切點,即在什麼位置執行切面方法。
  • aop.expression,橫切點規則表達式,過濾符合規則的切點。有關表達式規則将會在另外一篇文章中詳細說明。
  • aop.aspect,切面。
  • aop:before,前置模式,切面執行模式。
  • aop:after-returning,後置傳回模式,切面執行模式。

4.運作結果

Spring面向切面程式設計(AOP)1.前言說明2.應用場景3.執行個體說明4.運作結果5.參考資料

  程式正常運作,指定包中的方法在執行前後均會執行切面服務。本文僅對AOP進行了簡要說明,更加詳細深入的說明請參考網上的其他資料。

  由于剛開始梳理這塊内容,知識面還不夠寬,如果文中有錯誤之處,希望大家能夠指出,謝謝!

5.參考資料

(1).什麼是面向切面程式設計AOP:https://www.zhihu.com/question/24863332

繼續閱讀