Spring AOP中 Around增強處理

2021-10-04 11:01:08 字數 1346 閱讀 6359

初次學習spring 的aop,就寫了乙個日誌切面,對控制層的接受和返回請求進行切面。

@aspect

@component

public

class

logaspect

@before

("log()"

)public

void

dobefore()

@after

("log()"

)public

void

doafter()

@afterreturning

(returning =

"result"

,pointcut =

"log()"

)public

void

doafterreturning

(object result)

",result);}

@afterthrowing

(pointcut =

"log()"

, throwing=

"ex"

)public

void

doafterthrowing

(exception ex)

",ex.

getmessage()

);} @around

("log()"

)public

void

doaround()

}

@around作用

1.可以在目標方法之前進行增強動作,也可以執行完目標方法後執行

2.可以決定目標方法何時,以何種方式執行,甚至可以不執行

3.可以改變目標方法的引數值,也可以改變執行目標方法之後的返回值;當需要改變目標方法的返回值時只能使用@around

注意:使用@around的方法的第乙個形參必須是proceedjoinpoint型別,只有待哦用該引數的proceed()方法才會呼叫目標函式。

proceedjoinpoint是joinpoint的子類,多了proceed()方法,並可以丟擲異常。

一般的寫法如下:

@around

("log()"

)public object doaround

(proceedingjoinpoint proceedingjoinpoint) throws throwable

顯示結果如下:

參考文獻

了解掌握spring aop 中

1.前置增強,業務層和aop攔截 如下 package com.mimoprint.schedule.service import lombok.extern.slf4j.slf4j import org.springframework.stereotype.service service slf4...

Spring AOP 中 Pointcut的用法

1.格式 execution modifiers pattern?ret type pattern declaring type pattern?name pattern param pattern throws pattern?括號中各個pattern分別表示 修飾符匹配 modifier pat...

Spring AOP中的幾個概念

aop,即面向切面程式設計,是對oop的一種補充和完善,在oop中由於有大量 的重複導致不利於各個模組的重用,而aop技術利用一種稱為 橫切 的技術,剖解開封裝的物件內部,並將那些影響了多個類的公共行為封裝到乙個可重用模組,並將其命名為 aspect 即切面。所謂 切面 簡單說就是那些與業務無關,卻...