日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@Pointcut的用法

發布時間:2023/12/19 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @Pointcut的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Pointcut 是指那些方法需要被執行"AOP",是由"Pointcut Expression"來描述的.
Pointcut可以有下列方式來定義或者通過&& || 和!的方式進行組合.?
args()
@args()
execution()
this()
target()
@target()
within()
@within()
@annotation
其中execution 是用的最多的,其格式為:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必須的.
ret-type-pattern:可以為*表示任何返回值,全路徑的類名等.
name-pattern:指定方法名,*代表所以,set*,代表以set開頭的所有方法.
parameters pattern:指定方法參數(聲明的類型),(..)代表所有參數,(*)代表一個參數,(*,String)代表第一個參數為任何值,第二個為String類型.
舉例說明:
任意公共方法的執行:
execution(public * *(..))
任何一個以“set”開始的方法的執行:
execution(* set*(..))
AccountService 接口的任意方法的執行:
execution(* com.xyz.service.AccountService.*(..))
定義在service包里的任意方法的執行:
execution(* com.xyz.service.*.*(..))
定義在service包和所有子包里的任意類的任意方法的執行:
execution(* com.xyz.service..*.*(..))
定義在pointcutexp包和所有子包里的JoinPointObjP2類的任意方法的執行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的為方法名,靠近.*(..))的為類名或者接口名,如上例的JoinPointObjP2.*(..))

pointcutexp包里的任意類.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意類.
within(com.test.spring.aop.pointcutexp..*)
實現了Intf接口的所有類,如果Intf不是接口,限定Intf單個類.
this(com.test.spring.aop.pointcutexp.Intf)
***> 當一個實現了接口的類被AOP的時候,用getBean方法必須cast為接口類型,不能為該類的類型.

帶有@Transactional標注的所有類的任意方法.
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
帶有@Transactional標注的任意方法.
@annotation(org.springframework.transaction.annotation.Transactional)
***> @within和@target針對類的注解,@annotation是針對方法的注解

參數帶有@Transactional標注的方法.
@args(org.springframework.transaction.annotation.Transactional)
參數為String類型(運行是決定)的方法.
args(String)
Pointcut 可以通過Java注解和XML兩種方式配置,如下所示: [html]?view plaincopy
  • <aop:config>??
  • ????<aop:aspectrefaop:aspectref="aspectDef">??
  • ????????<aop:pointcutidaop:pointcutid="pointcut1"expression="execution(*?com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))"/>??
  • ????????<aop:before?pointcut-ref="pointcut1"?method="beforeAdvice"?/>??
  • ????</aop:aspect>??
  • </aop:config>??
  • ??
  • @Component??
  • @Aspect??
  • public?class?AspectDef?{??
  • ????//@Pointcut("execution(*?com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")??
  • ????//@Pointcut("within(com.test.spring.aop.pointcutexp..*)")??
  • ????//@Pointcut("this(com.test.spring.aop.pointcutexp.Intf)")??
  • ????//@Pointcut("target(com.test.spring.aop.pointcutexp.Intf)")??
  • ????//@Pointcut("@within(org.springframework.transaction.annotation.Transactional)")??
  • ????//@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")??
  • ????@Pointcut("args(String)")??
  • ????public?void?pointcut1()?{??
  • ????}??
  • ????@Before(value?=?"pointcut1()")??
  • ????public?void?beforeAdvice()?{??
  • ????????System.out.println("pointcut1?@Before...");??
  • ????}??
  • 22

    總結

    以上是生活随笔為你收集整理的@Pointcut的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。