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