AOP(註解) 理解 應用 重點

2021-07-31 02:38:22 字數 1236 閱讀 5229

1.aop註解配置流程

a.開啟aop配置支援註解@aspectj

核心配置檔案中新增以下配置,功能等同於註解配置bean的自動掃瞄路徑

b.將所有參與aop配置的類宣告為spring控制的bean

可以使用xml配置格式或註解格式

c.在切面類的類定義上方新增切面的宣告

@aspect

public class myadvice

d.將切面類中的方法配置為指定型別的通知,配置時指定其切入點

@before("execution(* cn.itcast.aop.annotation.userimpl.add())")

public void before(joinpoint jp)

2.配置公共的切入點

a.在切面類中宣告乙個方法(私有的),將該方法配置為切入點

@pointcut("execution(* cn.itcast.aop.annotation.userimpl.add())") private void pt(){}

b.使用配置的切入點

@before("引用切入點")

格式:切面類名.方法名()

範例:@before("myadvice. pt ()")

3.註解開發通知的通知類別

前置通知 @before(value="execution(* *..*.*(..))")

後置通知 @after(value="execution(* *..*.*(..))")

丟擲異常通知 @afterthrowing(value="execution(* *..*.*(..))",throwing="ex") 返回後通知 @afterreturning(value="execution(* *..*.*(..))",returning="ret") 環繞通知 @around(value="execution(* *..*.*(..))")

4.註解格式aop順序

總體順序由上到下為下列描述順序

around before

before

around after

after

afterreturning

實際開發以最終執行順序為準

5.返回值與異常物件的獲取方式

@afterreturning(value="myadvice.pt()",returning="aa")

public void afterreturning(joinpoint jp,object aa)

自定義註解理解梳理

自定義註解的使用很簡單,通過 inte ce修飾就可以稱之為註解,但是在使用的時候,卻有很多門道。限制輸入 首先,自定義註解可以用來限制輸入。例如 target elementtype.method retention retentionpolicy.runtime documented publi...

aop 註解方式

直接 demo 1.介面 package com.bird.service public inte ce personserver 2.實現類 package com.bird.service.impl import com.bird.service.personserver public clas...

註解實現AOP

使用註解實現aop,注意版本問題,使用註解報錯要匯入m en依賴 dependency groupid j ax.annotation groupid artifactid j ax.annotation api artifactid version 1.3.2 version dependency...