SpringAOP

AOP概念

來自百度百科

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

AOP相關術語

  1. Joinpoint(連接點):
    所謂連接點是指那些被攔截到的點。在 spring 中,這些點指的是方法,因爲 spring 只支持方法類型的連接點。
  2. Pointcut(切入點):
    所謂切入點是指我們要對哪些 Joinpoint 進行攔截的定義。
  3. Advice(通知/ 增強):
    所謂通知是指攔截到 Joinpoint 之後所要做的事情就是通知。
    通知的類型:前置通知,後置通知,異常通知,最終通知,環繞通知。
  4. Introduction(引介):
    引介是一種特殊的通知在不修改類代碼的前提下, Introduction 可以在運行期爲類動態地添加一些方法或 Field。
  5. Target(目標對象):
    代理的目標對象。
  6. Weaving(織入):
    是指把增強應用到目標對象來創建新的代理對象的過程。
    spring 採用動態代理織入,而 AspectJ 採用編譯期織入和類裝載期織入。
  7. Proxy (代理):
    一個類被 AOP 織入增強後,就產生一個結果代理類。
  8. Aspect(切面):
    是切入點和通知(引介)的結合。

Spring基於xml的AOP

業務層接口

/**
 * @Date 2019/7/31 - 16:47
 * <p>
 * 賬戶的業務層接口
 */
public interface IAccountService {
    /**
     * 模擬保存賬戶
     */
    void saveAccount();

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

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

業務層實現類

public class AccountServiceImpl implements IAccountService {
    @Override
    public void saveAccount() {
        System.out.println("執行了保存");
    }

    @Override
    public void updateAccount(int i) {
        System.out.println("執行了更新");
    }

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

用於模擬記錄日誌的工具類

/**
 * @Date 2019/7/31 - 16:51
 * <p>
 * 用於記錄日誌的工具類
 * 提供了公共的代碼
 */
public class Logger {
    /**
     * 前置通知
     */
    public void beforePrintLog() {
        System.out.println("前置通知,Logger類中的beforePrintLog開始記錄日誌");
    }

    /**
     * 後置通知
     */
    public void afterReturningPrintLog() {
        System.out.println("後置通知,Logger類中的afterReturningPrintLog開始記錄日誌");
    }

    /**
     * 異常通知
     */
    public void afterThrowingPrintLog() {
        System.out.println("異常通知,Logger類中的afterThrowingPrintLog開始記錄日誌");
    }

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

    /**
     * 環繞通知
     * 當配置了環繞通知後,切入點方法沒有執行,而通知方法執行了
     * <p>
     * 動態代理的環繞通知有明確的切入點方法調用而aroundPrintLog中沒有
     * <p>
     * 解決:
     * Spring提供了ProceedingJoinPoint接口,調用該接口的proceed()方法就相當於調用切入點方法
     * 該接口可以作爲環繞通知的方法參數,程序執行時Spring框架提供該接口的實現類
     * <p>
     * Spring框架提供的一種可以在代碼中手動控制增強方法何時執行的方式
     */
    public Object aroundPrintLog(ProceedingJoinPoint proceedingJoinPoint) {
        Object returnValue = null;
        try {
            //得到方法執行所需的參數
            Object[] args = proceedingJoinPoint.getArgs();

            //proceed方法之前爲前置通知

            //明確調用切入點方法
            returnValue = proceedingJoinPoint.proceed();

            //proceed方法之後爲後置通知

            return returnValue;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            //catch中爲異常通知

            throw new RuntimeException(throwable);
        } finally {
            //finally中爲最終通知
        }
    }
}

配置文件

<?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-->
    <bean id="accountService" class="com.sx.service.impl.AccountServiceImpl"></bean>

    <!--spring中基於xml的AOP配置-->
    <!--1.將通知類交給spring管理-->
    <bean id="logger" class="com.sx.utils.Logger"></bean>
    <!--2.使用aop:config標籤表明開始配置AOP-->
    <aop:config>
        <!--使用aop:aspect標籤表明配置切面-->
        <!--id屬性給切面提供一個唯一標識,ref屬性指定通知類Bean的id-->
        <!--在aop:aspect標籤中使用對應的標籤來配置通知的類型-->
        <aop:pointcut id="pc1" expression="execution(* com.sx.service.impl.*.*(..))"></aop:pointcut>
        <aop:aspect id="logAdvice" ref="logger">
            <!--pointcut屬性用於指定切入點表達式,該表達式的含義是指定業務層中哪些方法增強-->
            <!--切入點表達式寫法:
                    關鍵字:execution(表達式)
                    表達式:
                        訪問修飾符 返回值 包名.包名.包名.類名 方法名(參數列表)
                        訪問修飾符可以省略
                        可以使用通配符表示任意返回值
                        包名可以使用通配符表示任意包,有幾級包就要寫幾個*.
                        可以使用..表示當前包及其所有子包
                        類名和方法名都可以使用*實現通配
                        參數列表:
                            基本類型直接寫名稱: int
                            引用類型寫包名.類名 java.lang.String
                            可以使用通配符表示任意類型,但是必須有參數
                            可以使用..有無參數均可,有參數表示任意類型
                    全通配寫法:
                        * *..*.*(..)
                     切到業務層實現類下所有方法
                        * com.sx.service.impl.*.*(..)
            -->
            <!--配置通知的類型,並且建立通知方法和切入點的關聯-->
            <!--3.配置前置通知-->
            <!--在切入點方法執行之前執行-->
            <aop:before method="beforePrintLog" pointcut-ref="pc1"></aop:before>
            <!--4.配置後置通知-->
            <!--在切入點方法正常執行之後執行-->
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pc1"></aop:after-returning>
            <!--5.配置異常通知-->
            <!--在切入點方法執行產生異常之後執行-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pc1"></aop:after-throwing>
            <!--6.配置最終通知-->
            <!--無論切入點方法是否正常執行都會在其後執行-->
            <aop:after method="afterPrintLog" pointcut-ref="pc1"></aop:after>

            <!--配置切入點表達式-->
            <!--id屬性用於指定表達式唯一標識,expression屬性用於指定表達式內容-->
            <!--寫在aop:aspect標籤中只能當前切面使用-->
            <!--寫在aop:aspect標籤外部時,必須寫在aop:aspect標籤之前-->
            <!--<aop:pointcut id="pc1" expression="execution(* com.sx.service.impl.*.*(..))"></aop:pointcut>-->
            <!--配置環繞通知-->
            <aop:around method="aroundPrintLog" pointcut-ref="pc1"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

Spring基於註解的AOP

配置類

@Configuration
@ComponentScan("com.sx")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}

切面類

@Component("logger")
@Aspect//表示當前類是一個切面類
public class Logger {
    @Pointcut("execution(* com.sx.service.impl.*.*(..))")
    private void pc1() {
    }

    /**
     * 前置通知
     */
    @Before("pc1()")
    public void beforePrintLog() {
        System.out.println("前置通知,Logger類中的beforePrintLog開始記錄日誌");
    }

    /**
     * 後置通知
     */
    @AfterReturning("pc1()")
    public void afterReturningPrintLog() {
        System.out.println("後置通知,Logger類中的afterReturningPrintLog開始記錄日誌");
    }

    /**
     * 異常通知
     */
    @AfterThrowing("pc1()")
    public void afterThrowingPrintLog() {
        System.out.println("異常通知,Logger類中的afterThrowingPrintLog開始記錄日誌");
    }

    /**
     * 最終通知
     */
    @After("pc1()")
    public void afterPrintLog() {
        System.out.println("最終通知,Logger類中的afterPrintLog開始記錄日誌");
    }

    @Around("pc1()")
    public Object aroundPrintLog(ProceedingJoinPoint proceedingJoinPoint) {
        Object returnValue = null;
        try {
            //得到方法執行所需的參數
            Object[] args = proceedingJoinPoint.getArgs();

            //proceed方法之前爲前置通知

            //明確調用切入點方法
            returnValue = proceedingJoinPoint.proceed();

            //proceed方法之後爲後置通知

            return returnValue;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            //catch中爲異常通知

            throw new RuntimeException(throwable);
        } finally {
            //finally中爲最終通知
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章