SpringAOP 如何通過JoinPoint獲取參數(shù)名和值
SpringAOP 通過JoinPoint獲取參數(shù)名和值
在Java8之前,代碼編譯為class文件后,方法參數(shù)的類型固定,但是方法名稱會丟失,方法名稱會變成arg0、arg1….。在Java8開始可以在class文件中保留參數(shù)名。
public void tet(JoinPoint joinPoint) {
// 下面兩個數(shù)組中,參數(shù)值和參數(shù)名的個數(shù)和位置是一一對應(yīng)的。
Object[] args = joinPoint.getArgs(); // 參數(shù)值
String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames(); // 參數(shù)名
}
注意:
IDEA 只有設(shè)置了 Java 編譯參數(shù)才能獲取到參數(shù)信息。并且jdk要在1.8及以上版本。

Maven中開啟的辦法
增加compilerArgs 參數(shù)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven_compiler_plugin_version}</version>
<configuration>
<source>${java_source_version}</source>
<target>${java_target_version}</target>
<encoding>${file_encoding}</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
Eclipse中開啟的辦法
Preferences->java->Compiler下勾選Store information about method parameters選項(xiàng)。
這樣在使用eclipse編譯java文件的時候就會將參數(shù)名稱編譯到class文件中。
SpringAOP中JoinPoint對象的使用方法
JoinPoint 對象
JoinPoint對象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數(shù),就可以獲取到封裝了該方法信息的JoinPoint對象.
常用API
| 方法名 | 功能 |
|---|---|
| Signature getSignature(); | 獲取封裝了署名信息的對象,在該對象中可以獲取到目標(biāo)方法名,所屬類的Class等信息 |
| Object[] getArgs(); | 獲取傳入目標(biāo)方法的參數(shù)對象 |
| Object getTarget(); | 獲取被代理的對象 |
| Object getThis(); | 獲取代理對象 |
ProceedingJoinPoint對象
ProceedingJoinPoint對象是JoinPoint的子接口,該對象只用在@Around的切面方法中,
添加了以下兩個方法。
Object proceed() throws Throwable //執(zhí)行目標(biāo)方法 Object proceed(Object[] var1) throws Throwable //傳入的新的參數(shù)去執(zhí)行目標(biāo)方法
Demo
切面類
@Aspect
@Component
public class aopAspect {
/**
* 定義一個切入點(diǎn)表達(dá)式,用來確定哪些類需要代理
* execution(* aopdemo.*.*(..))代表aopdemo包下所有類的所有方法都會被代理
*/
@Pointcut("execution(* aopdemo.*.*(..))")
public void declareJoinPointerExpression() {}
/**
* 前置方法,在目標(biāo)方法執(zhí)行前執(zhí)行
* @param joinPoint 封裝了代理方法信息的對象,若用不到則可以忽略不寫
*/
@Before("declareJoinPointerExpression()")
public void beforeMethod(JoinPoint joinPoint){
System.out.println("目標(biāo)方法名為:" + joinPoint.getSignature().getName());
System.out.println("目標(biāo)方法所屬類的簡單類名:" + 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) + "個參數(shù)為:" + args[i]);
}
System.out.println("被代理的對象:" + joinPoint.getTarget());
System.out.println("代理對象自己:" + joinPoint.getThis());
}
/**
* 環(huán)繞方法,可自定義目標(biāo)方法執(zhí)行的時機(jī)
* @param pjd JoinPoint的子接口,添加了
* Object proceed() throws Throwable 執(zhí)行目標(biāo)方法
* Object proceed(Object[] var1) throws Throwable 傳入的新的參數(shù)去執(zhí)行目標(biāo)方法
* 兩個方法
* @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;
}
}
被代理類
/**
* 被代理對象
*/
@Component
public class TargetClass {
/**
* 拼接兩個字符串
*/
public String joint(String str1, String str2) {
return str1 + "+" + str2;
}
}
測試類
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)方法所屬類的簡單類名:TargetClass
目標(biāo)方法所屬類的類名:aopdemo.TargetClass
目標(biāo)方法聲明類型:public
第1個參數(shù)為:newSpring
第2個參數(shù)為:newAop
被代理的對象:aopdemo.TargetClass@4efc180e
代理對象自己:aopdemo.TargetClass@4efc180e (和上面一樣是因?yàn)閠oString方法也被代理了)
目標(biāo)方法返回結(jié)果后...
目標(biāo)方法執(zhí)行后...
result:newSpring+newAop
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過簡單方法實(shí)現(xiàn)spring boot web項(xiàng)目
這篇文章主要介紹了通過簡單方法實(shí)現(xiàn)spring boot web項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java的項(xiàng)目構(gòu)建工具M(jìn)aven的配置和使用教程
Maven是Java世界中的項(xiàng)目管理和構(gòu)建自動化工具,基于POM項(xiàng)目對象模型的思想,下面我們就具體來看一下具體的Java的項(xiàng)目構(gòu)建工具M(jìn)aven的配置和使用教程:2016-05-05
SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題
這篇文章主要介紹了SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java中注解@Async實(shí)現(xiàn)異步及導(dǎo)致失效原因分析
Async注解用于聲明一個方法是異步的,當(dāng)在方法上加上這個注解時將會在一個新的線程中執(zhí)行該方法,而不會阻塞原始線程,這篇文章主要給大家介紹了關(guān)于Java中注解@Async實(shí)現(xiàn)異步及導(dǎo)致失效原因分析的相關(guān)資料,需要的朋友可以參考下2024-07-07
Spring中的@Transactional事務(wù)失效場景解讀
這篇文章主要介紹了Spring中的@Transactional事務(wù)失效場景解讀,如果Transactional注解應(yīng)用在非public 修飾的方法上,Transactional將會失效此方法會檢查目標(biāo)方法的修飾符是否為 public,不是 public則不會獲取@Transactional 的屬性配置信息,需要的朋友可以參考下2023-12-12
java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器,模擬血壓計(jì)實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Java中websocket消息推送的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java中websocket消息推送的實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02

