丟擲通知
org.springframework.aop.throwsadvice;
丟擲通知跟後置通知一樣是在連線點隻後執行的,不過丟擲通知只在方法丟擲乙個異常時才執行,該通知對程式執行不能做任何改變, 能做的只是改變丟擲異常的型別
public class errorclass
public void error2() throws illegalargumentexception
}
import org.springframework.aop.throwsadvice;
import org.springframework.aop.throwsadvice;
public class mythrowsadvice implements throwsadvice
public void afterthrowing(method method,object args,object target,illegalargumentexception ex) throws throwable
}
import org.springframework.aop.framework.proxyfactory;
importorg.springframework.aop.support.namematchmethodpointcutadvisor;
public class test2 catch(exception ignored)
trycatch(exception ignored)}}
對於丟擲的每乙個異常,spring只會執行乙個afterthrowing()方法, spring會選擇簽名與丟擲的異常最匹配的乙個方法
this is a test1
this is a test2
AOP 通知 筆記
說到aop肯定會想到切面 通知 切點等等。那什麼是通知呢?之前我一直以為我們所說的通知就是我們寫在切面中的方法,但是隨著aop認識的加深,現在發現所謂的通知不僅僅是我們寫在切面中的方法,通知方法只是通知的一部分。也就是說我們在切面中定義的通知方法會被包裝成乙個真正的通知物件。通知有五種型別 他們的繼...
AOP學習之五種通知
spring aop通知 advice 分成五類 前置通知before advice 在連線點前面執行,前置通知不會影響連線點的執行,除非此處丟擲異常。正常返回通知after returning advice 在連線點正常執行完成後執行,如果連線點丟擲異常,則不會執行。異常返回通知after thr...
spring(十) aop環繞通知
假如有這麼乙個場景,需要統計某個方法執行的時間,如何做呢?典型的會想到在方法執行前記錄時間,方法執行後再次記錄,得出執行的時間。如果採用spring的aop,僅僅使用前置和後置方法是無法做到的,因為他們無法共享變數。這樣通過環繞通知,就可以快捷的實現。首先在切面通知類中宣告環繞通知類 public ...