8 基於註解形式的AOP實現

2021-10-04 11:42:37 字數 1772 閱讀 4122

1.匯入jar包

2.編寫業務類,納入容器

@component

("logannotation"

)//相當於

@aspect

public

class

logbeforeannocation

@afterreturning

("execution(public * addstudent(..))"

)//屬性:定義切點

public

void

after()

}

note:

通過註解component納入容器需要配置,掃瞄器會將指定的包中的@componment @service @respository @controller修飾的 類產生的物件增加到ioc容器中,如下:

base-

package

="com.lianqiao.dao"

>

<

/context:component-scan>

package

="com.lianqiao.aop"

>

<

/context:component-scan>

3.配置
<

!-- 開啟對註解的支援 --

>

<

/aop:aspectj-autoproxy>

note:

通過註解形式實現的aop,如果想獲取目標物件的一些引數,則需要使用乙個物件:joinpoint

@component

("logannotation"

)// 相當於

@aspect

public

class

logbeforeannocation

//後置通知

@afterreturning

(pointcut =

"execution(public * addstudent(..))"

, returning =

"returnvalue"

)// 屬性:定義切點

public

void

after

(joinpoint jp, object returnvalue)

// 環繞通知

@around

("execution(public * addstudent(..))"

)public

void

myaround

(proceedingjoinpoint jp)

catch

(throwable e)

finally

}// 異常通知:如果只捕獲特定型別的異常,則可以通過第二個引數實現

@afterthrowing

(pointcut=

"execution(public * addstudent(..))"

,throwing=

"e")

public

void

myexception

(joinpoint pj,nullpointerexception e)

// 最終通知

@after

("execution(public * addstudent(..))"

)public

void

myafter()

}

AOP基於註解實現

切面類 component 控制反轉 aspect 宣告切面類 public class forumadvisor after execution com.mitu.aspect.宣告後置增強 public void after around execution com.mitu.aspect.宣告...

AspectJ基於註解的AOP 實現

配置檔案 demo aspect public class myaspect signature signature joinpoint.getsignature system.out.println 方法的定義簽名signature signature string name joinpoint....

基於註解的AOP

aop是oop的延續,是aspect oriented programming的縮寫,意思是面向切面程式設計。可以通過預編譯方式和執行期動態 實現在不修改源 的情況下給程式動態統一新增功能的一種技術。aop實際是gof設計模式的延續,設計模式孜孜不倦追求的是呼叫者和被呼叫者之間的解耦,aop可以說也...