Spring框架AOP學習之配註解增強類

2021-09-25 14:33:35 字數 1558 閱讀 3389

建立乙個被增強類book2:

package aopdemo;

import org.springframework.stereotype.component;

@component("book")

public class book2

}

建立增強類mybook2:

package aopdemo;

import org.aspectj.lang.proceedingjoinpoint;

import org.aspectj.lang.annotation.after;

import org.aspectj.lang.annotation.afterreturning;

import org.aspectj.lang.annotation.afterthrowing;

import org.aspectj.lang.annotation.around;

import org.aspectj.lang.annotation.aspect;

import org.aspectj.lang.annotation.before;

import org.springframework.stereotype.service;

@service("mybook")

@aspect//增強類

public class mybook2

@afterreturning(value="execution(* aopdemo.book2.print(..))")

public void after1()

@around(value="execution(* aopdemo.book2.print(..))")

public void around1(proceedingjoinpoint proceedingjoinpoint) throws throwable

@afterthrowing(value="execution(* aopdemo.book2.print(..))")

public void exception()

@after(value="execution(* aopdemo.book2.print(..))")

public void after2()

}

配置檔案bean_aop2.xml

<?xml version="1.0" encoding="utf-8"?>

測試**:

package aopdemo;

public class test2

}

結果

方法之前//around(環繞增強)

前置增強//before(前置增強)

原方法方法之後//around(環繞增強)

最終通知//after(最終增強)

後置增強//afterreturning(後置增強)

注意:在配置檔案中不僅要開啟註解,還要開啟aop:

Spring框架系列之AOP思想

歡迎關注 互相學習,共同進步!1 什麼是 aop aop 為 aspect oriented programming 的縮寫,意為 面向切面程式設計 aop 是 oop 物件導向 的延續,可以對業務的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性和開發效率。2 aop ...

Spring框架 AOP細節

知己海記憶體 2016 11 24 10 17 1切入點表示式 1.1作用 通過表示式的方式定位乙個或多個具體的連線點。1.2語法細節 切入點表示式的語法格式 execution 許可權修飾符 返回值型別 簡單類名 全類名 方法名 引數列表 舉例說明 表示式execution com.atguigu...

Spring框架AOP原理

aop aspect oriented programming 意思就是面相切面程式設計。通俗來說就是一種在通過預編譯方式和執行期間動態 實現程式功能的統一維護的一種技術,這種在執行時,動態的將 切入到類的指定方法 指定位置上的程式設計思想就是面向切面程式設計,aop通過一系列的 來實現的。說到底,...