基于SpringAop中JoinPoint對(duì)象的使用說(shuō)明
JoinPoint 對(duì)象
JoinPoint對(duì)象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數(shù),就可以獲取到封裝了該方法信息的JoinPoint對(duì)象。
常用api:
| 方法名 | 功能 |
|---|---|
| Signature getSignature(); | 獲取封裝了署名信息的對(duì)象,在該對(duì)象中可以獲取到目標(biāo)方法名,所屬類的Class等信息 |
| Object[] getArgs(); | 獲取傳入目標(biāo)方法的參數(shù)對(duì)象 |
| Object getTarget(); | 獲取被代理的對(duì)象 |
| Object getThis(); | 獲取代理對(duì)象 |
ProceedingJoinPoint對(duì)象
ProceedingJoinPoint對(duì)象是JoinPoint的子接口,該對(duì)象只用在@Around的切面方法中。
添加了
Object proceed() throws Throwable //執(zhí)行目標(biāo)方法 Object proceed(Object[] var1) throws Throwable //傳入的新的參數(shù)去執(zhí)行目標(biāo)方法
兩個(gè)方法.
Demo
切面類
@Aspect
@Component
public class aopAspect {
/**
* 定義一個(gè)切入點(diǎn)表達(dá)式,用來(lái)確定哪些類需要代理
* 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;
}
}
被代理類
/**
* 被代理對(duì)象
*/
@Component
public class TargetClass {
/**
* 拼接兩個(gè)字符串
*/
public String joint(String str1, String str2) {
return str1 + "+" + str2;
}
}
測(cè)試類
public class TestAop {
@Test
public void testAOP() {
//1、創(chuàng)建Spring的IOC的容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");
//2、從IOC容器中獲取bean的實(shí)例
TargetClass targetClass = (TargetClass) ctx.getBean("targetClass");
//3、使用bean
String result = targetClass.joint("spring","aop");
System.out.println("result:" + result);
}
}
輸出結(jié)果
目標(biāo)方法執(zhí)行前...
目標(biāo)方法名為:joint
目標(biāo)方法所屬類的簡(jiǎn)單類名:TargetClass
目標(biāo)方法所屬類的類名:aopdemo.TargetClass
目標(biāo)方法聲明類型:public
第1個(gè)參數(shù)為:newSpring
第2個(gè)參數(shù)為:newAop
被代理的對(duì)象:aopdemo.TargetClass@4efc180e
代理對(duì)象自己:aopdemo.TargetClass@4efc180e
目標(biāo)方法返回結(jié)果后...
目標(biāo)方法執(zhí)行后...
result:newSpring+newAop
SpringAop 通知參數(shù)JoinPoint的幾個(gè)常用API
1.獲取類名
String className = joinPoint.getSignature().getDeclaringTypeName();
2.獲取方法名
String methodName = joinPoint.getSignature().getName();
3.獲取返回值類型
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature(); Class clazz = methodSignature.getReturnType();
如果是環(huán)繞通知參數(shù)ProceedingJoinPoint則有
4.執(zhí)行目標(biāo)方法
joinPoint.proceed();
5.執(zhí)行目標(biāo)方法,替換入?yún)?/h3>
proceed(java.lang.Object[] args)
proceed(java.lang.Object[] args)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jdbc鏈接遠(yuǎn)程數(shù)據(jù)庫(kù)進(jìn)行修改url操作
這篇文章主要為大家詳細(xì)介紹了jdbc鏈接遠(yuǎn)程數(shù)據(jù)庫(kù)進(jìn)行修改url操作,感興趣的小伙伴們可以參考一下2016-06-06
Java中HashMap和Hashtable的區(qū)別淺析
這篇文章主要介紹了Java中HashMap和Hashtable的區(qū)別淺析,本文總結(jié)了6條它們之間的不同之處,需要的朋友可以參考下2015-03-03
詳解MyBatis中Executor執(zhí)行SQL語(yǔ)句的過(guò)程
MyBatis中獲取SqlSession時(shí)會(huì)創(chuàng)建執(zhí)行器Executor并存放在SqlSession中,本篇文章將以MapperMethod的execute() 方法作為起點(diǎn),對(duì)MyBatis中的一次實(shí)際執(zhí)行請(qǐng)求進(jìn)行說(shuō)明,并結(jié)合源碼對(duì)執(zhí)行器Executor的原理進(jìn)行闡釋2023-07-07
SpringBoot2整合JTA組件實(shí)現(xiàn)多數(shù)據(jù)源事務(wù)管理
這篇文章主要介紹了SpringBoot2整合JTA組件實(shí)現(xiàn)多數(shù)據(jù)源事務(wù)管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java使用自定義注解實(shí)現(xiàn)函數(shù)測(cè)試功能示例
這篇文章主要介紹了Java使用自定義注解實(shí)現(xiàn)函數(shù)測(cè)試功能,結(jié)合實(shí)例形式分析了java自定義注解在函數(shù)測(cè)試過(guò)程中相關(guān)功能、原理與使用技巧,需要的朋友可以參考下2019-10-10
Java LinkedList的實(shí)現(xiàn)原理圖文詳解
今天小編就為大家分享一篇關(guān)于Java LinkedList的實(shí)現(xiàn)原理圖文詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Java list.remove( )方法注意事項(xiàng)
這篇文章主要介紹了Java list.remove( )方法注意事項(xiàng),非常簡(jiǎn)單易懂,需要的朋友可以參考下2018-08-08
SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例
這篇文章主要介紹了SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法
這篇文章主要介紹了Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法,本文給出了根據(jù)定義去求解、平方根、找規(guī)律三種解法,需要的朋友可以參考下2015-03-03

