Springboot純註解Aop示例

2021-10-07 04:33:53 字數 2183 閱讀 7126

準備好介面和實現類

@service

public

inte***ce

aninmalservice

@service

("dogservice"

)public

class

dogserviceimpl

implements

aninmalservice

}

切面類

@aspect

public

class

myaspect

@before

("pointcut()"

)public

void

before()

@after

("pointcut()"

)public

void

after()

@afterreturning

("pointcut()"

)public

void

afterreturning()

@afterthrowing

("pointcut()"

)public

void

afterthrowing()

}

配置類

@configuration

@componentscan

(basepackages =

)@enableaspectjautoproxy

@propertysource

(value=

)public

class

bootconfig

}

測試類

public

class

mytest

}

測試afterretruning結果

測試afterthrowing結果

6.1 構造異常

@service

("dogservice"

)public

class

dogserviceimpl

implements

aninmalservice

}

6.2 測試類

public

class

mytest

}

6.3 再次執行測試類的結果

7. 測試傳參

7.1 介面實現類

@service

("dogservice"

)public

class

dogserviceimpl

implements

aninmalservice

}

7.2 切面類

@before

("pointcut() && args(cat)"

)//args(引數名)方式來引入引數, 引數名需要與下面宣告的引數名稱一致

public

void

before

(joinpoint joinpoint,cat cat)

7.3 測試結果

注意事項:

8.1 必須在configuration的配置類中加入@enableaspectjautoproxy否則aop不會生效

8.2 在configuration配置類注入myaspect bean的原因是方便快速找到aop 的bean的配置。

當然取消在configuration配置類中的bean注入,然後在myaspect 類上方加入component註解一樣可以將切面類注入到spring容器 【已測試】。

Spring Anno AOP 基於註解的AOP)

參考文章 aspect註解 package com.itheima.utils import org.aspectj.lang.proceedingjoinpoint import org.aspectj.lang.annotation.import org.springframework.ster...

spring之基於aspectj註解aop使用

在配置檔案中開啟aop自動 1 在增強類上面使用 aspect註解 2 在增強方法上面配置不同型別通知 增強類 aspect public class myuser 後置通知 afterreturning value execution cn.aop.user.update public void ...

springboot 註解總結

springboot註解知識點歸納 當實現rest ful web services時,response將一直通過response body傳送。controller 用於定義控制器類,在spring 專案中由控制器負責將使用者發來的url請求 到對應的服務介面 service層 restcontr...