SpringBoot?AOP中JoinPoint的使用方式和通知切點表達式
更新時間:2024年05月13日 09:16:40 作者:程序員三時
這篇文章主要介紹了SpringBoot?AOP中JoinPoint的使用方式和通知切點表達式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
JoinPoint和ProceedingJoinPoint對象
JoinPoint
對象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數(shù),就可以獲取到封裝了該方法信息的JoinPoint對象.ProceedingJoinPoint
對象是JoinPoint的子接口,該對象只用在@Around的切面方法中
@Aspect @Component public class aopAspect { /** * 定義一個切入點表達式,用來確定哪些類需要代理 * execution(* aopdemo.*.*(..))代表aopdemo包下所有類的所有方法都會被代理 */ @Pointcut("execution(* aopdemo.*.*(..))") public void declareJoinPointerExpression() {} /** * 前置方法,在目標方法執(zhí)行前執(zhí)行 * @param joinPoint 封裝了代理方法信息的對象,若用不到則可以忽略不寫 */ @Before("declareJoinPointerExpression()") public void beforeMethod(JoinPoint joinPoint){ System.out.println("目標方法名為:" + joinPoint.getSignature().getName()); System.out.println("目標方法所屬類的簡單類名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); System.out.println("目標方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("目標方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); //獲取傳入目標方法的參數(shù) Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("第" + (i+1) + "個參數(shù)為:" + args[i]); } System.out.println("被代理的對象:" + joinPoint.getTarget()); System.out.println("代理對象自己:" + joinPoint.getThis()); } /** * 環(huán)繞方法,可自定義目標方法執(zhí)行的時機 * @param pjd JoinPoint的子接口,添加了 * Object proceed() throws Throwable 執(zhí)行目標方法 * Object proceed(Object[] var1) throws Throwable 傳入的新的參數(shù)去執(zhí)行目標方法 * 兩個方法 * @return 此方法需要返回值,返回值視為目標方法的返回值 */ @Around("declareJoinPointerExpression()") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; try { //前置通知 System.out.println("目標方法執(zhí)行前..."); //執(zhí)行目標方法 //result = pjd.proeed(); //用新的參數(shù)值執(zhí)行目標方法 result = pjd.proceed(new Object[]{"newSpring","newAop"}); //返回通知 System.out.println("目標方法返回結果后..."); } catch (Throwable e) { //異常通知 System.out.println("執(zhí)行目標方法異常后..."); throw new RuntimeException(e); } //后置通知 System.out.println("目標方法執(zhí)行后..."); return result; } }
切點表達式
- 在Spring AOP中,連接點始終代表方法的執(zhí)行。切入點是與連接點匹配的,切入點表達語言是以編程方式描述切入點的方式。
- 切入點(Poincut)是定義了在“什么地方”進行切入,哪些連接點會得到通知。顯然,切點一定是連接點
- 切點是通過
@Pointcut
注解和切點表達式
定義的。@Pointcut注解可以在一個切面內定義可重用
的切點。
execute表達式
*
代表匹配任意修飾符及任意返回值,參數(shù)列表中..
匹配任意數(shù)量的參數(shù)
可以使用&&、||、!、三種運算符來組合切點表達式,表示與或非的關系
- 1.攔截任意公共方法
execution(public * *(..))
- 2.攔截以set開頭的任意方法
execution(* set*(..))
- 3.攔截類或者接口中的方法
攔截AccountService(類、接口)中定義的所有方法 execution(* com.xyz.service.AccountService.*(..))
4.攔截包中定義的方法,不包含子包中的方法
攔截com.xyz.service包中所有類中任意方法,**不包含**子包中的類 execution(* com.xyz.service.*.*(..))
5.攔截包或者子包中定義的方法
攔截com.xyz.service包或者子包中定義的所有方法 execution(* com.xyz.service..*.*(..))
通知分類
@Before
- 前置通知: 在方法執(zhí)行之前執(zhí)行
- 前置通知使用
@Before
注解 將切入點表達式值作為注解的值
@After
- 后置通知, 在方法執(zhí)行之后執(zhí)行
- 后置通知使用
@After
注解 ,在后置通知中,不能訪問目標方法執(zhí)行的結果
@AfterRunning
- 返回通知, 在方法返回結果之后執(zhí)行
- 返回通知使用
@AfterRunning
注解
@AfterThrowing
- 異常通知, 在方法拋出異常之后執(zhí)行
- 異常通知使用
@AfterThrowing
注解
@Around
- 環(huán)繞通知, 圍繞著方法執(zhí)行
- 環(huán)繞通知使用
@Around
注解
package com.jason.spring.aop.impl; import java.util.Arrays; import java.util.List; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; //把這個類聲明為一個切面 //1.需要將該類放入到IOC 容器中 @Component //2.再聲明為一個切面 @Aspect public class LoggingAspect { //聲明該方法是一個前置通知:在目標方法開始之前執(zhí)行 哪些類,哪些方法 //作用:@before 當調用目標方法,而目標方法與注解聲明的方法相匹配的時候,aop框架會自動的為那個方法所在的類生成一個代理對象,在目標方法執(zhí)行之前,執(zhí)行注解的方法 //支持通配符 //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))") @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))") public void beforeMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins " + args); } /** * @Description: 在方法執(zhí)行后執(zhí)行的代碼,無論該方法是否出現(xiàn)異常 * @param joinPoint */ @After("execution(* com.jason.spring.aop.impl.*.*(int, int))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " end " + args); } /** * * @Description: 在方法正常結束后執(zhí)行代碼,放回通知是可以訪問到方法的返回值 * * @param joinPoint */ @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result") public void afterReturning(JoinPoint joinPoint ,Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " end with " + result); } /** * * @Description: 在目標方法出現(xiàn)異常時會執(zhí)行代碼,可以訪問到異常對象,且,可以指定出現(xiàn)特定異常時執(zhí)行通知代碼 * * @param joinPoint * @param ex */ @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex") public void afterThrowting(JoinPoint joinPoint, Exception ex){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occurs exceptions " + ex); } /** * * @Description: 環(huán)繞通知需要攜帶 ProceedingJoinPoint 類型的參數(shù) * 環(huán)繞通知 類似于 動態(tài)代理的全過程 * ProceedingJoinPoint:可以決定是否執(zhí)行目標方法 * 環(huán)繞通知必須有返回值,返回值即為目標方法的返回值 * * @param proceedingJoinPoint */ @Around("execution(* com.jason.spring.aop.impl.*.*(..))") public Object around(ProceedingJoinPoint proceedingJoinPoint){ Object result = null; String methodName = proceedingJoinPoint.getSignature().getName(); //執(zhí)行目標方法 try { //前置通知 System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs())); result = proceedingJoinPoint.proceed(); //后置通知 System.out.println("The method " + methodName + "end with" + result); } catch (Throwable e) { //異常通知 System.out.println("The method occurs exception : " + e); throw new RuntimeException(); } //后置通知 System.out.println("The method " + methodName + "end with" + result); return result; } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java的Hibernate框架中Criteria查詢使用的實例講解
這篇文章主要介紹了Java的Hibernate框架中Criteria查詢使用的實例講解,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01Java異常 Factory method''sqlSessionFactory''rew exception;este
這篇文章主要介紹了Java異常 Factory method ‘sqlSessionFactory‘ threw exception; nested exception is java.lang.NoSuchMethodError:,本文介紹了springboot 引入mybatis-plus后報錯的解決方案,以下就是詳細內容,需要的朋友可以參考下2021-07-07登錄EasyConnect后無法通過jdbc訪問服務器數(shù)據(jù)庫問題的解決方法
描述一下近期使用EasyConnect遇到的問題,下面這篇文章主要給大家介紹了關于登錄EasyConnect后無法通過jdbc訪問服務器數(shù)據(jù)庫問題的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-02-02關于Spring MVC同名參數(shù)綁定問題的解決方法
Spring MVC中的參數(shù)綁定還是蠻重要的,最近在使用中遇到了同名參數(shù)綁定的問題,想著總結分享出來,下面這篇文章主要給大家介紹了關于Spring MVC同名參數(shù)綁定問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08Java前端開發(fā)之HttpServletRequest的使用
service方法中的request的類型是ServletRequest,而doGet/doPost方法的request的類型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加強大2023-01-01