1. 程式人生 > 實用技巧 >spring中基於XML的AOP配置步驟

spring中基於XML的AOP配置步驟

/**
 * 業務層介面
 * @date 2019/9/24 15:05
 */
public interface AccountService {
    /**
     * 模擬儲存賬戶
     */
    void saveAccount();

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

    /**
     * 刪除賬戶
     *
     * @return
     */
    int deleteAccount();
}
/**
 * 賬戶的業務層實現類
 
*/ public class AccountServiceImpl implements AccountService { @Override public void saveAccount() { System.out.println("執行了儲存"); } @Override public void updateAccount(int i) { System.out.println("執行了更新" + i); } @Override public int deleteAccount() { System.out.println(
"執行了刪除"); return 0; } }
/**
 * 用於記錄日誌的工具類
 */
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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置srping的Ioc,把service物件配置進來--> <bean id="accountService" class="com.xcu.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.itheima.service.impl.AccountServiceImpl.saveAccount() 訪問修飾符可以省略 void com.itheima.service.impl.AccountServiceImpl.saveAccount() 返回值可以使用萬用字元,表示任意返回值 * com.itheima.service.impl.AccountServiceImpl.saveAccount() 包名可以使用萬用字元,表示任意包。但是有幾級包,就需要寫幾個*. * *.*.*.*.AccountServiceImpl.saveAccount()) 包名可以使用..表示當前包及其子包 * *..AccountServiceImpl.saveAccount() 類名和方法名都可以使用*來實現通配 * *..*.*() 引數列表: 可以直接寫資料型別: 基本型別直接寫名稱 int 引用型別寫包名.類名的方式 java.lang.String 可以使用萬用字元表示任意型別,但是必須有引數 可以使用..表示有無引數均可,有引數可以是任意型別 全通配寫法: * *..*.*(..) 實際開發中切入點表示式的通常寫法: 切到業務層實現類下的所有方法 * com.itheima.service.impl.*.*(..) --> <!-- 配置Logger類 --> <bean id="logger" class="com.xcu.utils.Logger"></bean> <!--配置AOP--> <aop:config> <!--配置切面 --> <aop:aspect id="logAdvice" ref="logger"> <!-- 配置通知的型別,並且建立通知方法和切入點方法的關聯--> <aop:before method="printLog" pointcut="execution(* com.xcu.service.impl.*.*(..))"></aop:before> </aop:aspect> </aop:config> </beans>

測試

/**
 * 測試AOP的配置
 */
public class AOPTest {
    public static void main(String[] args) {
        //1.獲取容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        //2.獲取物件
        AccountService accountService = applicationContext.getBean("accountService",AccountService.class);
        //3.執行方法
        accountService.saveAccount();
        accountService.updateAccount(5);
        accountService.deleteAccount();
    }
}