Spring AOP中JoinPoint的用法


Spring JoinPoint的用法

JoinPoint 對象

JoinPoint對象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數,就可以獲取到封裝了該方法信息的JoinPoint對象.

常用API

方法名 功能
Signature getSignature(); 獲取封裝了署名信息的對象,在該對象中可以獲取到目標方法名,所屬類的Class等信息
Object[] getArgs(); 獲取傳入目標方法的參數對象
Object getTarget(); 獲取被代理的對象
Object getThis(); 獲取代理對象

ProceedingJoinPoint對象

ProceedingJoinPoint對象是JoinPoint的子接口,該對象只用在@Around的切面方法中,
添加了以下兩個方法。

Object proceed() throws Throwable //執行目標方法 Object proceed(Object[] var1) throws Throwable //傳入的新的參數去執行目標方法 

Demo

切面類

@Aspect @Component public class aopAspect { /** * 定義一個切入點表達式,用來確定哪些類需要代理 * execution(* aopdemo.*.*(..))代表aopdemo包下所有類的所有方法都會被代理 */ @Pointcut("execution(* aopdemo.*.*(..))") public void declareJoinPointerExpression() {} /** * 前置方法,在目標方法執行前執行 * @param joinPoint 封裝了代理方法信息的對象,若用不到則可以忽略不寫 */ @Before("declareJoinPointerExpression()") public void beforeMethod(JoinPoint joinPoint){ System.out.println("目標方法名為:" + joinPoint.getSignature().getName()); System.out.println("目標方法所屬類的簡單類名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); System.out.println("目標方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("目標方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); //獲取傳入目標方法的參數 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("第" + (i+1) + "個參數為:" + args[i]); } System.out.println("被代理的對象:" + joinPoint.getTarget()); System.out.println("代理對象自己:" + joinPoint.getThis()); } /** * 環繞方法,可自定義目標方法執行的時機 * @param pjd JoinPoint的子接口,添加了 * Object proceed() throws Throwable 執行目標方法 * Object proceed(Object[] var1) throws Throwable 傳入的新的參數去執行目標方法 * 兩個方法 * @return 此方法需要返回值,返回值視為目標方法的返回值 */ @Around("declareJoinPointerExpression()") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; try { //前置通知 System.out.println("目標方法執行前..."); //執行目標方法 //result = pjd.proeed(); //用新的參數值執行目標方法 result = pjd.proceed(new Object[]{"newSpring","newAop"}); //返回通知 System.out.println("目標方法返回結果后..."); } catch (Throwable e) { //異常通知 System.out.println("執行目標方法異常后..."); throw new RuntimeException(e); } //后置通知 System.out.println("目標方法執行后..."); return result; } } 

被代理類

/** * 被代理對象 */ @Component public class TargetClass { /** * 拼接兩個字符串 */ public String joint(String str1, String str2) { return str1 + "+" + str2; } } 

測試類

public class TestAop { @Test public void testAOP() { //1、創建Spring的IOC的容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml"); //2、從IOC容器中獲取bean的實例 TargetClass targetClass = (TargetClass) ctx.getBean("targetClass"); //3、使用bean String result = targetClass.joint("spring","aop"); System.out.println("result:" + result); } } 

輸出結果

目標方法執行前...
目標方法名為:joint 目標方法所屬類的簡單類名:TargetClass 目標方法所屬類的類名:aopdemo.TargetClass 目標方法聲明類型:public 第1個參數為:newSpring 第2個參數為:newAop 被代理的對象:aopdemo.TargetClass@4efc180e 代理對象自己:aopdemo.TargetClass@4efc180e (和上面一樣是因為toString方法也被代理了) 目標方法返回結果后... 目標方法執行后... result:newSpring+newAop 

聲明

本文章屬於筆者的學習筆記,若內容有誤,還請賜教!謝謝

參考文章:
http://blog.csdn.net/ochangwen/article/details/52557724
http://blog.csdn.net/a9529lty/article/details/7031070



作者:zFxiang_
鏈接:https://www.jianshu.com/p/90881bfc3241
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM