使用AspectJ的AOP開發


AspectJ它是一個開源的第三方的AOP 框架,spring 把它集成進來了

一、Apsectj的AOP(XML方式)

1. 創建項目並導包

在這裏插入圖片描述
在這裏插入圖片描述

2. 創建目標類

在這裏插入圖片描述

3. 創建applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 導入頭文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
    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/context
         http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

4. 前置通知

  • 創建MyAdvise
    在這裏插入圖片描述
  • 配置applicationContext.xml
    在這裏插入圖片描述
  • 測試
    在這裏插入圖片描述

5. 後置通知

  • MyAdvise
    在這裏插入圖片描述
  • 配置applicationContext.xml
    在這裏插入圖片描述
  • 測試
    在這裏插入圖片描述

6. 環繞通知

 |-- 第一種環繞通知

  • MyAdvise
    在這裏插入圖片描述
  • 配置applicationContext.xml
    在這裏插入圖片描述
  • 測試
    在這裏插入圖片描述
     |-- 第二種環繞通知
  • 修改MyAdvice
    在這裏插入圖片描述
  • 配置applicationContext.xml
    在這裏插入圖片描述
  • 測試
    在這裏插入圖片描述

7. 異常通知

  • 修改MyAdvise
    在這裏插入圖片描述
  • 配置applicationContext.xml
    在這裏插入圖片描述
  • 測試
    在這裏插入圖片描述

二、Aspectj的AOP(註解方式)

1. 創建Person

在這裏插入圖片描述

2. 創建MyAdvise

在這裏插入圖片描述

3. 創建applicationContext.xml

在這裏插入圖片描述

4. 前置增強

  • 修改MyAdvise
    在這裏插入圖片描述

5. 後置增強

  • 修改MyAdvise
    在這裏插入圖片描述

6. 環繞增強

  • 修改MyAdvise
    在這裏插入圖片描述

7. 異常增強

  • 修改MyAdvise
    在這裏插入圖片描述
  • 修改Person
    在這裏插入圖片描述

8. 測試

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章