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á)式,用來(lái)確定哪些類(lèi)需要代理
* execution(* aopdemo.*.*(..))代表aopdemo包下所有類(lèi)的所有方法都會(huì)被代理
*/
@Pointcut("execution(* aopdemo.*.*(..))")
public void declareJoinPointerExpression() {}
/**
* 前置方法,在目標(biāo)方法執(zhí)行前執(zhí)行
* @param joinPoint 封裝了代理方法信息的對(duì)象,若用不到則可以忽略不寫(xiě)
*/
@Before("declareJoinPointerExpression()")
public void beforeMethod(JoinPoint joinPoint){
System.out.println("目標(biāo)方法名為:" + joinPoint.getSignature().getName());
System.out.println("目標(biāo)方法所屬類(lèi)的簡(jiǎn)單類(lèi)名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());
System.out.println("目標(biāo)方法所屬類(lèi)的類(lèi)名:" + joinPoint.getSignature().getDeclaringTypeName());
System.out.println("目標(biāo)方法聲明類(lèi)型:" + 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)是通過(guò)
@Pointcut注解和切點(diǎn)表達(dá)式定義的。@Pointcut注解可以在一個(gè)切面內(nèi)定義可重用的切點(diǎn)。
execute表達(dá)式
*代表匹配任意修飾符及任意返回值,參數(shù)列表中..匹配任意數(shù)量的參數(shù)
可以使用&&、||、!、三種運(yùn)算符來(lái)組合切點(diǎn)表達(dá)式,表示與或非的關(guān)系
- 1.攔截任意公共方法
execution(public * *(..)) - 2.攔截以set開(kāi)頭的任意方法
execution(* set*(..)) - 3.攔截類(lèi)或者接口中的方法
攔截AccountService(類(lèi)、接口)中定義的所有方法 execution(* com.xyz.service.AccountService.*(..))
4.攔截包中定義的方法,不包含子包中的方法
攔截com.xyz.service包中所有類(lèi)中任意方法,**不包含**子包中的類(lèi) execution(* com.xyz.service.*.*(..))
5.攔截包或者子包中定義的方法
攔截com.xyz.service包或者子包中定義的所有方法 execution(* com.xyz.service..*.*(..))
通知分類(lèi)
@Before
- 前置通知: 在方法執(zhí)行之前執(zhí)行
- 前置通知使用
@Before注解 將切入點(diǎn)表達(dá)式值作為注解的值

@After
- 后置通知, 在方法執(zhí)行之后執(zhí)行
- 后置通知使用
@After注解 ,在后置通知中,不能訪問(wèn)目標(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è)類(lèi)聲明為一個(gè)切面
//1.需要將該類(lèi)放入到IOC 容器中
@Component
//2.再聲明為一個(gè)切面
@Aspect
public class LoggingAspect {
//聲明該方法是一個(gè)前置通知:在目標(biāo)方法開(kāi)始之前執(zhí)行 哪些類(lèi),哪些方法
//作用:@before 當(dāng)調(diào)用目標(biāo)方法,而目標(biāo)方法與注解聲明的方法相匹配的時(shí)候,aop框架會(huì)自動(dòng)的為那個(gè)方法所在的類(lèi)生成一個(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í)行的代碼,無(wú)論該方法是否出現(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í)行代碼,放回通知是可以訪問(wèn)到方法的返回值
*
* @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í)行代碼,可以訪問(wèn)到異常對(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 類(lèi)型的參數(shù)
* 環(huán)繞通知 類(lèi)似于 動(dòng)態(tài)代理的全過(guò)程
* 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中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶(hù)端
本文主要介紹了java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶(hù)端,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
java微信公眾號(hào)支付開(kāi)發(fā)之現(xiàn)金紅包
這篇文章主要為大家詳細(xì)介紹了java微信公眾號(hào)支付開(kāi)發(fā)之現(xiàn)金紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Java使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求
這篇文章主要介紹了Java使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求,HTTP請(qǐng)求,在日常開(kāi)發(fā)中,還是比較常見(jiàn)的,今天給大家分享HttpUtils如何使用,需要的朋友可以參考下2023-05-05
java基于mongodb實(shí)現(xiàn)分布式鎖的示例代碼
本文主要介紹了java基于mongodb實(shí)現(xiàn)分布式鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
SpringBoot靜態(tài)函數(shù)無(wú)法自動(dòng)注入Bean的原因分析與解決方案
在 Spring Boot 項(xiàng)目中,開(kāi)發(fā)者常遇到一個(gè)典型問(wèn)題:在靜態(tài)方法或靜態(tài)變量中嘗試使用 @Autowired 注入 Bean 時(shí),始終得到 null 值,本文將深入剖析這一問(wèn)題的根源,并提供多種可靠解決方案,需要的朋友可以參考下2025-08-08
如何利用Stream改變list中特定對(duì)象的某一屬性
這篇文章主要介紹了如何利用Stream改變list中特定對(duì)象的某一屬性問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

