Spring中基於xml的AOP

1、Aop 全程是Aspect Oriented Programming 即面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的同一維護的一種技術。Aop是oop的延續,是軟件開發中的 一個熱點,也是Spring框架中一個重要的內容。是函數式編程的一個衍生範例,利用Aop可以對業務邏輯各個部分進行分割,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用行,提高了開發效率。簡單的說就是把我們程序中的重複代碼抽取出來,在需要執行的時候,使用動態代理的技術,在不修改源碼的基礎上已有的方法進行增強,(使用動態代理的方式實現)

相關術語

JoinPoint:鏈接點 那些被攔截到的點,在spring中,這些點指的是方法,因為spring只支持方法類型的連接點

Pointcut:切入點   是指我們要對哪些JoinPont進行攔截的定義

Advice:通知/增強  攔截到Joinpoint之後所要做的事情就是通知

通知類型:前置通知、後置通知、異常通知、最終通知、環繞通知

Introduction:引介   是一種特殊的通知,在不修改類代碼的前提下,Introduction可以在運行期為類動態的添加一些方法或field

Target:目標對象,代理的目標對象

Weaving 織入   是指把增強應用到目標對象來創建新的代理對象的過程,spring採用動態代理織入,而AspectJ採用編譯期織入和類裝載期織入

Proxy:代理,一類類被Aop織入增強后,就產生一個結果代理類

Aspect:切面   是切入點和通知(引介)的結合

在 spring 中,框架會根據目標類是否實現了接口來決定採用哪種動態代理的方式。

基於XMl的AOP步驟

1、創建Maven項目引入spring坐標

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mingqi</groupId>
    <artifactId>SpringIOC</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2、創建業務層接口:

package com.mingqi.services;
public interface IAccountService {
    /**
     * 模擬登陸賬戶
     */
    void saveAccount();

    /**
     * 模擬更新賬戶
     * @param id
     */
    void updateAccount(int id);

    /**
     * 模擬刪除賬戶
     * @return
     */
    int deleteAccount();

}

3.創建業務層實現類

package com.mingqi.services.impl;
import com.mingqi.services.IAccountService;
public class AccountServicesImpl implements IAccountService {
    public void saveAccount() {
        System.out.println("執行了保存");
    }

    public void updateAccount(int id) {
        System.out.println("執行了更新"+id);
    }

    public int deleteAccount() {
        System.out.println("執行了刪除");
        return 0;
    }
}

4、創建工具類

package com.mingqi.utils;
import org.aspectj.lang.ProceedingJoinPoint;
/**
 * 用戶記錄日誌的工具類,裏面提供公共的代碼
 */
public class Logger {
    /**
     * 用於打印日誌:計劃讓其在切入點方法執行前執行(切入點方法就是業務層方法)
     */
    public  void beforePrintLog(){
        System.out.println("Logger類中的pringLog方法開始記錄日誌了。。。");
    }
    public  void afterReturningPrintLog()
    {
        System.out.println("後置通知Logger類中的beforePrintLog方法開始記錄日誌了。。。");
    }
    /**
     * 異常通知
     */
    public void afterThrowingPrintLog()
    {
        System.out.println("異常通知Logger類中的afterThrowingPrintLog方法開始記錄日誌了。。。");

    }
    /**
     * 最終通知
     */
    public void afterPrintLog()
    {
        System.out.println("最終通知Logger類中的afterPrintLog方法開始記錄日誌了。。。");
    }

    /**
     * 環繞通知
     * 問題  當我們配置了環繞通知以後,切入點方法沒有執行,而通知方法執行了
     * 分析: 通過對比動態代理中的環繞通知代碼,發現動態代理中的環繞通知有明確的切入點方法調用,而我們的代碼中沒有
     * 解決: Spring 框架為我們提供了一個接口:ProceedingJoinPoint。該接口有一個方法proceed(),此方法就相當於明確調用切入點的方法
     *        該接口可以作為環繞通知的參數方法,在程序執行時,spring框架會為我們提供該接口的實現類供我們使用
     * spring中的環繞通知
     *      他是spring框架為我們提供的一種可以在代碼中手動控制增強方法何時會執行的方式
     * @param pjp
     * @return
     */
    public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try{
            Object[] args = pjp.getArgs();//得到方法執行所需的參數

            System.out.println("Logger類中的aroundPringLog方法開始記錄日誌了。。。前置");

            rtValue = pjp.proceed(args);//明確調用業務層方法(切入點方法)

            System.out.println("Logger類中的aroundPringLog方法開始記錄日誌了。。。後置");

            return rtValue;
        }catch (Throwable t){
            System.out.println("Logger類中的aroundPringLog方法開始記錄日誌了。。。異常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logger類中的aroundPringLog方法開始記錄日誌了。。。最終");
        }
    }
}

4、創建bean配置文件

<?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">
       <!-- 配置spring的IOC,把service對象配置進來-->
       <bean id="accountSevice" class="com.mingqi.services.impl.AccountServicesImpl"></bean>
       <!-- spring 中基於xml的Aop配置步驟
         1、把通知Bean也交給spring來管理
         2、使用aop:config標籤表名開始aop的配置
         3、使用aop:aspect標籤表明配置切面
             id屬性:是給切面提供一個唯一標識
             ref屬性:是指定通知類的id
         4、在aop:aspect標籤的內部使用對應的標籤來配置通知的類型
             我們現在的示例是讓printlog方法在切入點方法執行之前執行,所以是前置通知
             aop:before:標識前置通知
                method屬性: 用於指定Logger類中的方法哪個是前置通知
                pointcut屬性: 用於指定切入點表達式,該表達式的含義指的是對業務層中的哪些方法增強
                切入點表達式的寫法:
                   關鍵字:execution(表達式)
                   表達式:  訪問修飾符 返回值 包名.包名.包名....類名.方法名(參數列表)
                   標準的寫法: public void com.mingqi.service.impl.AccountServiceImpl.saveAccount()
                   訪問修飾符可以省略:void com.mingqi.service.impl.AccountServiceImpl.saveAccount()
                   返回值可以使用通配符,標識任意返回值:* com.mingqi.service.impl.AccountServiceImpl.saveAccount()
                   包名可以使用通配符,表示任意包,但是有幾級包就需要寫幾個*  *.*.*.*.*.AccountServiceImpl.saveAccount()
                   包名可以使用..代表當前包及其子包:* *.AccountServiceImpl.saveAccount()
                   類名和方法名都可以使用*來實現統配 * *..*.*();
                   參數列表: 可以直接寫數據類型:
                                 基本類型直接寫名稱:int
                                 引用類型寫包名.類名的方式: java.lang.String
                                可以使用通配符來標識任意類型,單必須有參數
                                可以使用..標識有無參數均可,有參數可以是任意類型

                      全通配寫法:
                    * *..*.*(..)
                   實際開發中 切入點表達式的通常寫法:
                          切到業務層實現類的所有方法,* com.mingqi.service.impl.*.*(..);
         -->
       <!-- 配置Logger類-->
       <bean id="logger" class="com.mingqi.utils.Logger"></bean>
       <!--使用aop:config標籤表名開始aop的配置-->
       <aop:config>
              <aop:pointcut id="pt1" expression="execution(* com.mingqi.services.impl.*.*(..))"></aop:pointcut>
              <!--使用aop:aspect標籤表明配置切面-->
              <aop:aspect id="LogAdvice" ref="logger">
                     <!-- 配置前置通知:在切入點方法執行之前執行
                     <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>-->

                     <!-- 配置後置通知:在切入點方法正常執行之後值。它和異常通知永遠只能執行一個
                          <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->
                     <!-- 配置異常通知:在切入點方法執行產生異常之後執行。它和後置通知永遠只能執行一個
                         <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->
                     <!-- 配置最終通知:無論切入點方法是否正常執行它都會在其後面執行
                        <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->
                     <!-- 配置環繞通知 詳細的註釋請看Logger類中-->
                        <aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
                    </aop:aspect>
             </aop:config>
       </beans>

6、創建測試類

package com.mingqi.test;
import com.mingqi.services.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringIoc {
    @Test
    public void TestAccount()
    {
        ApplicationContext ac= new ClassPathXmlApplicationContext("beam.xml");
        IAccountService accountService=(IAccountService) ac.getBean("accountSevice");
        accountService.saveAccount();
        accountService.updateAccount(22);
        accountService.deleteAccount();
    }
}

 

 

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

網頁設計公司推薦不同的風格,搶佔消費者視覺第一線

※Google地圖已可更新顯示潭子電動車充電站設置地點!!

※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

※別再煩惱如何寫文案,掌握八大原則!

網頁設計最專業,超強功能平台可客製化

您可能也會喜歡…