本文介紹了SpringBoot項目中使用AOP的方法,分享給大家,具體如下:
1.概述
將通用的邏輯用AOP技術實現可以極大的簡化程序的編寫,例如驗簽、鑒權等。Spring的聲明式事務也是通過AOP技術實現的。
具體的代碼參照 示例項目 https://github.com/qihaiyan/springcamp/tree/master/spring-aop
Spring的AOP技術主要有4個核心概念:
Pointcut: 切點,用于定義哪個方法會被攔截,例如 execution(* cn.springcamp.springaop.service.*.*(..))
Advice: 攔截到方法后要執行的動作
Aspect: 切面,把Pointcut和Advice組合在一起形成一個切面
Join Point: 在執行時Pointcut的一個實例
Weaver: 實現AOP的框架,例如 AspectJ 或 Spring AOP
2. 切點定義
常用的Pointcut定義有 execution 和 @annotation 兩種。execution 定義對方法無侵入,用于實現比較通用的切面。@annotation 可以作為注解加到特定的方法上,例如Spring的Transaction注解。
execution切點定義應該放在一個公共的類中,集中管理切點定義。
示例:
1
2
3
4
|
public class CommonJoinPointConfig { @Pointcut ( "execution(* cn.springcamp.springaop.service.*.*(..))" ) public void serviceLayerExecution() {} } |
這樣在具體的Aspect類中可以通過 CommonJoinPointConfig.serviceLayerExecution()來引用切點。
1
2
3
4
5
6
7
|
public class BeforeAspect { @Before ( "CommonJoinPointConfig.serviceLayerExecution()" ) public void before(JoinPoint joinPoint) { System.out.println( " -------------> Before Aspect " ); System.out.println( " -------------> before execution of " + joinPoint); } } |
當切點需要改變時,只需修改CommonJoinPointConfig類即可,不用修改每個Aspect類。
3. 常用的切面
Before: 在方法執行之前執行Advice,常用于驗簽、鑒權等。
After: 在方法執行完成后執行,無論是執行成功還是拋出異常.
AfterReturning: 僅在方法執行成功后執行.
AfterThrowing: 僅在方法執拋出異常后執行.
一個簡單的Aspect:
1
2
3
4
5
6
7
8
9
|
@Aspect @Component public class BeforeAspect { @Before ( "CommonJoinPointConfig.serviceLayerExecution()" ) public void before(JoinPoint joinPoint) { System.out.println( " -------------> Before Aspect " ); System.out.println( " -------------> before execution of " + joinPoint); } } |
4. 自定義注解
假設我們想收集特定方法的執行時間,一種比較合理的方式是自定義一個注解,然后在需要收集執行時間的方法上加上這個注解。
首先定義一個注解TrackTime:
1
2
3
4
5
|
@Target ({ElementType.METHOD, ElementType.TYPE}) @Retention (RetentionPolicy.RUNTIME) public @interface TrackTime { String param() default "" ; } |
然后再定義一個Aspect類,用于實現注解的行為:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Aspect @Component public class TrackTimeAspect { @Around ( "@annotation(trackTime)" ) public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime) throws Throwable { Object result = null ; long startTime = System.currentTimeMillis(); result = joinPoint.proceed(); long timeTaken = System.currentTimeMillis() - startTime; System.out.println( " -------------> Time Taken by " + joinPoint + " with param[" + trackTime.param() + "] is " + timeTaken); return result; } } |
在某個方法上使用這個注解,就可以收集這個方法的執行時間:
1
2
3
4
5
|
@TrackTime (param = "myService" ) public String runFoo() { System.out.println( " -------------> foo" ); return "foo" ; } |
注意 @TrackTime(param = "myService") 注解是可以傳參的。
為了讓注解可以傳參數,需要在定義注解時指定一個參數String param() default "默認值",
同時在Aspect類中,around方法上加上相應的參數,@Around注解中也需要用參數的變量名trackTime,而不能用類名TrackTime。
1
2
|
@Around ( "@annotation(trackTime)" ) public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime) |
5.總結
在運行示例項目時,控制臺會輸出以下內容:
-------------> Before Aspect
-------------> before execution of execution(String cn.springcamp.springaop.service.MyService.runFoo())
-------------> foo
-------------> Time Taken by execution(String cn.springcamp.springaop.service.MyService.runFoo()) with param[myService] is 8
-------------> After Aspect
-------------> after execution of execution(String cn.springcamp.springaop.service.MyService.runFoo())
-------------> AfterReturning Aspect
-------------> execution(String cn.springcamp.springaop.service.MyService.runFoo()) returned with value foo
可以看出幾種 Aspect 的執行順序依次為 Before After Around AfterReturning(AfterThrowing)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/haiyan_qi/article/details/79335999