1. 程式人生 > 其它 >SpringAOP基於xml配置入門

SpringAOP基於xml配置入門

步驟

示例

service

package com.czy.service.impl;

import com.czy.service.AccountService;

public class AccountServiceImpl implements AccountService {
    public void saveAccount() {
        System.out.println("執行了儲存");
    }

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

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

公共程式碼

package com.czy.utils;

/**
 * 用於記錄日誌的工具類,它裡面提供了公共的程式碼
 */
public class Logger {
    /**
     * 用於列印日子,計劃讓其在切入點方法執行之前執行(切入點方法就是我們的業務層方法)
     */
    public void printLog(){
        System.out.println("Logger類中的printLog方法開始記錄日誌了...");
    }
}

配置檔案

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置spring的ioc,把service物件配置-->
    <bean id="accountService" class="com.czy.service.impl.AccountServiceImpl"></bean>

    <!--spring中基於xml的AOP配置步驟
    1.把通知的bean也交給spring管理
    2.使用aop:config標籤表面開始Aop的配置
    3.使用aop:aspect標籤表面配置切面
            id屬性:給切面提供唯一標識
            ref屬性:指定通知類bean的id
    4.在aop:aspect標籤內部使用對應的標籤來配置通知的型別
            我們現在的示例是讓printLog方法在切入點方法之前執行,所以是前置通知
            aop:before : 表示前置通知
                    method屬性:用於指定logger類中哪個方法作為前置通知
                    pointcut屬性:用於指定切入點表示式,該表示式的含義指的是對業務層中哪些方法增強
            切入點表示式的寫法:
                關鍵字:execution(表示式)
                表示式:
                       訪問修飾符  返回值  包名.包名.包名.....類名.方法名(引數列表)
                標準的表示式寫法:
                        public void com.czy.service.impl.AccountServiceImpl.saveAccount()
                訪問修飾符可以省略:
                        void com.czy.service.impl.AccountServiceImpl.saveAccount()
                返回值可以使用萬用字元表示任意返回值
                        * com.czy.service.impl.AccountServiceImpl.saveAccount()
                包名可以使用萬用字元,表示任意包,但是有幾級包,就需要寫幾個*:
                        * *.*.*.*.AccountServiceImpl.saveAccount()
                包名可以使用..表示當前包及其子包:
                        * *..AccountServiceImpl.saveAccount()
                類名和方法名都可以使用*實現通配
                引數列表:
                    可以直接寫資料型別:
                            基本型別直接寫名稱
                            引用型別寫包名.類名這種全類名形式:java.lang.String
                    可以使用萬用字元表示任意型別,但必須有引數(*)
                    可以使用..表示有無引數均可,有引數可以是任意型別
                全通配寫法:
                        * * *..*.*(..)

                實際開發中切入點表示式的通常寫法:
                    切到業務層實現類的所有方法:
                        * com.czy.service.impl.*.*(..)
    -->

    <!--配置logger類-->
    <bean id="logger" class="com.czy.utils.Logger"></bean>

    <!--配置AOP-->
    <aop:config>
        <!--配置切面-->
        <aop:aspect id="logAdvice" ref="logger">
            <!--配置通知的型別並且建立通知方法和切入點方法的關聯-->
            <aop:before method="printLog" pointcut="execution( * com.czy.service.impl.*.*(..) )"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>