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

 AspectJ使用org.aspectj.lang.JoinPoint接口表示目標類連接點對象,如果是環繞增強時,使用org.aspectj.lang.ProceedingJoinPoint表示連接點對象,該類是JoinPoint的子接口。任何一個增強方法都可以通過將第一個入參聲明爲JoinPoint訪問到連接點上下文的信息。我們先來了解一下這兩個接口的主要方法: 
1)JoinPoint 
 java.lang.Object[] getArgs():獲取連接點方法運行時的入參列表; 
 Signature getSignature() :獲取連接點的方法簽名對象; 
 java.lang.Object getTarget() :獲取連接點所在的目標對象; 
 java.lang.Object getThis() :獲取代理對象本身; 
2)ProceedingJoinPoint 
ProceedingJoinPoint繼承JoinPoint子接口,它新增了兩個用於執行連接點方法的方法: 
 java.lang.Object proceed() throws java.lang.Throwable:通過反射執行目標對象的連接點處的方法; 
 java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通過反射執行目標對象連接點處的方法,不過使用新的入參替換原來的入參。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章