SpringBoot中定位切點的兩種常用方法
有時候,我們使用AOP來進行放的增強,編寫切面類的時候,需要定位在哪個方法上試用該切面進行增強,本片文章主要講解兩種在SpringBoot中定位切點的方法,一種是使用execution表達式的方法,一種則是利用自定義注解的方法。
接下來以一個簡單的例子來講解這兩種方法的使用方式。
<==========方法執(zhí)行前==========>
method();
<==========方法執(zhí)行后==========>
execution 表達式
execution表達式的方式主要是在定義切點的時候,通過表達式的方式選取到所需要增強的方法。
execution表達式解讀
execution(<修飾符模式>?<返回類型模式><方法名模式>(<參數(shù)模式>)<異常模式>?)
| 類型 | 解讀 | 是否必須 | 示例 |
|---|---|---|---|
| <修飾符模式> | 表示所選的修飾符類型 | 否 | public/private/... |
| <返回類型模式> | 表示所選的返回值類型 | 是 | void/int/... |
| <方法名模式> | 表示所選的包或者方法 | 是 | com.luke.service/com.luke.controller.*/... |
| (<參數(shù)模式>) | 表示所選方法的參數(shù) | 是 | *(..)/*(String name)/*(int size, ..)/... |
| <異常模式> | 表示所選方法的異常類型 | 否 | throws Exception/... |
// 匹配指定包中的所有方法 execution(* com.luke.service.*(..)) // 匹配當(dāng)前包中的所有public方法 execution(public * UserService.*(..)) // 匹配指定包中的所有public方法,并且返回值是int類型的方法 execution(public int com.luke.service.*(..)) // 匹配指定包中的所有public方法,并且第一個參數(shù)是String,返回值是int類型的方法 execution(public int com.luke.service.*(String name, ..))
自定義切面類:
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(* com.luke.springdata.controller.*.*(..))")
public void operationLog(){}
/**
* 這里只定義一個Around的增強做展示
*/
@Around("operationLog()")
public Object doAround(ProceedingJoinPoint joinPoint) {
Object proceed = null;
try {
System.out.println("方法執(zhí)行前");
proceed = joinPoint.proceed();
System.out.println("方法執(zhí)行后");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return proceed;
}
}
此切點的execution表達式為com.luke.springdata.controller包下的所有方法。
使用**@Around**注解表明增強的方法,并且指定切點。
測試用Controller類
@RestController
@RequestMapping("/person")
public class PersonController {
@GetMapping("/test")
public void test(){
System.out.println("方法執(zhí)行了");
}
}
運行項目,調(diào)用該方法,查看結(jié)果。
方法執(zhí)行前
方法執(zhí)行了
方法執(zhí)行后
自定義注解的方法
自定義注解的方式就是在需要增強的方法上面加上自定義的注解即可。
自定義注解類:
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log{
}
這里自定義了一個注解Log,該注解只能加在方法上。
自定義切面類:
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.luke.springdata.annotation.Log)")
public void operationLog(){}
/**
* 這里只定義一個Around的增強做展示
*/
@Around("operationLog()")
public Object doAround(ProceedingJoinPoint joinPoint) {
Object proceed = null;
try {
System.out.println("方法執(zhí)行前");
proceed = joinPoint.proceed();
System.out.println("方法執(zhí)行后");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return proceed;
}
}
這里編寫的自定義個切面類,用**@Pointcut注解定義一個切面,并且這次采用@annotation(xxx)**的方式表明如果哪個方法上添加了xxx注解,則就使用該切面做增強。
同時在每個增強的方法上使用該切面,隨后編寫正常的方法增強邏輯即可。
測試用Controller類
@RestController
@RequestMapping("/person")
public class PersonController {
@Log
@GetMapping("/test")
public void test(){
System.out.println("方法執(zhí)行了");
}
}
此時在需要使用切面的方法上加入**@Log**注解,調(diào)用該方法,查看效果。
方法執(zhí)行前
方法執(zhí)行了
方法執(zhí)行后
總結(jié)
兩種方式均能實現(xiàn)AOP的功能,在使用上,如果某個包下面的所有方法,都需要這個切面進行增強,那么使用execution表達式的方式更方便。但如果只有部分方法需要,并且分布在不同的類中,則注解的方式更靈活。
到此這篇關(guān)于SpringBoot中定位切點的兩種常用方法的文章就介紹到這了,更多相關(guān)SpringBoot 定位切點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC實現(xiàn)簡單跳轉(zhuǎn)方法(專題)
這篇文章主要介紹了SpringMVC實現(xiàn)簡單跳轉(zhuǎn)方法(專題),詳細的介紹了SpringMVC跳轉(zhuǎn)的幾種方法,非常具有實用價值,需要的朋友可以參考下2018-03-03
Java基礎(chǔ)之淺談hashCode()和equals()
今天給大家?guī)淼氖顷P(guān)于Java基礎(chǔ)的相關(guān)知識,文章圍繞著hashCode()和equals()展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Java基礎(chǔ) Servlet監(jiān)聽器詳解
這篇文章主要介紹了Java基礎(chǔ) Servlet監(jiān)聽器詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
SpringBoot實現(xiàn)RAS+AES自動接口解密
本文主要介紹了SpringBoot實現(xiàn)RAS+AES自動接口解密,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

