SpringAOP 如何通過(guò)JoinPoint獲取參數(shù)名和值
SpringAOP 通過(guò)JoinPoint獲取參數(shù)名和值
在Java8之前,代碼編譯為class文件后,方法參數(shù)的類型固定,但是方法名稱會(huì)丟失,方法名稱會(huì)變成arg0、arg1….。在Java8開(kāi)始可以在class文件中保留參數(shù)名。
public void tet(JoinPoint joinPoint) { // 下面兩個(gè)數(shù)組中,參數(shù)值和參數(shù)名的個(gè)數(shù)和位置是一一對(duì)應(yīng)的。 Object[] args = joinPoint.getArgs(); // 參數(shù)值 String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames(); // 參數(shù)名 }
注意:
IDEA 只有設(shè)置了 Java 編譯參數(shù)才能獲取到參數(shù)信息。并且jdk要在1.8及以上版本。
Maven中開(kāi)啟的辦法
增加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中開(kāi)啟的辦法
Preferences->java->Compiler下勾選Store information about method parameters選項(xiàng)。
這樣在使用eclipse編譯java文件的時(shí)候就會(huì)將參數(shù)名稱編譯到class文件中。
SpringAOP中JoinPoint對(duì)象的使用方法
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的切面方法中,
添加了以下兩個(gè)方法。
Object proceed() throws Throwable //執(zhí)行目標(biāo)方法 Object proceed(Object[] var1) throws Throwable //傳入的新的參數(shù)去執(zhí)行目標(biāo)方法
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 (和上面一樣是因?yàn)閠oString方法也被代理了)
目標(biāo)方法返回結(jié)果后...
目標(biāo)方法執(zhí)行后...
result:newSpring+newAop
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過(guò)簡(jiǎn)單方法實(shí)現(xiàn)spring boot web項(xiàng)目
這篇文章主要介紹了通過(guò)簡(jiǎn)單方法實(shí)現(xiàn)spring boot web項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Java的項(xiàng)目構(gòu)建工具M(jìn)aven的配置和使用教程
Maven是Java世界中的項(xiàng)目管理和構(gòu)建自動(dòng)化工具,基于POM項(xiàng)目對(duì)象模型的思想,下面我們就具體來(lái)看一下具體的Java的項(xiàng)目構(gòu)建工具M(jìn)aven的配置和使用教程:2016-05-05SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問(wèn)題
這篇文章主要介紹了SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java中注解@Async實(shí)現(xiàn)異步及導(dǎo)致失效原因分析
Async注解用于聲明一個(gè)方法是異步的,當(dāng)在方法上加上這個(gè)注解時(shí)將會(huì)在一個(gè)新的線程中執(zhí)行該方法,而不會(huì)阻塞原始線程,這篇文章主要給大家介紹了關(guān)于Java中注解@Async實(shí)現(xiàn)異步及導(dǎo)致失效原因分析的相關(guān)資料,需要的朋友可以參考下2024-07-07Spring中的@Transactional事務(wù)失效場(chǎng)景解讀
這篇文章主要介紹了Spring中的@Transactional事務(wù)失效場(chǎng)景解讀,如果Transactional注解應(yīng)用在非public 修飾的方法上,Transactional將會(huì)失效此方法會(huì)檢查目標(biāo)方法的修飾符是否為 public,不是 public則不會(huì)獲取@Transactional 的屬性配置信息,需要的朋友可以參考下2023-12-12java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器,模擬血壓計(jì)實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Java中websocket消息推送的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java中websocket消息推送的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02