動態代理的三種書寫方法

1.blind方法即給被代理類對象實例話,又返回代理類對象

/**
 * 代理類
 */
class MyInvocationHandler implements InvocationHandler{
    Object object;//實現了接口的被代理類對象的聲明(RealSubject)


    // 1.給被代理類的對象實例話
    //2.返回一個代理類的對象
    public Object blind(Object obj){
        this.object = obj;

        //三個參數被代理類對象的classLoader,被代理類實現的接口,實現了InvocationHandler接口的類對象
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);
    }


    //當通過代理類的對象發起對被代理類方法的調用時,都會轉爲對invoke方法的調用,進而實現動態效果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //returnVal是被代理類方法的返回值
        Object returnVal =  method.invoke(object,args);


        System.out.println("invoke方法被調用");
        return returnVal;
    }
}

調用:

public class TestDynamicAgent {
    public static void main(String[] args) {
        //1.構造被代理類的對象
        RealSubject realSubject = new RealSubject();

        //2創建一個實現了InvocationHandle接口的對象
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler();

        //3.通過myInvocationHandle的blind方法 ,返回實現了被代理類所實現的接口的代理類對象
        Object obj =  myInvocationHandler.blind(realSubject);
        Subject sub = (Subject) obj;//sub就是i代理類的對象

        sub.action();//轉到對代理類的inovke方法的調用





//        //測試動態代理
//        NikeClothFactory clothFactory = new NikeClothFactory();
//        Object demo = myInvocationHandler.blind(clothFactory);
//        ClothFactory clo = (ClothFactory) demo;
//        clo.productCloth();


    }
}

 

 

2.構建一個靜態方法返回代理類對象

//代理類
class MyInvocationHandler implements InvocationHandler{

    Object object;

    public void setObject(Object object) {
        this.object = object;
    }



    //在methoo1與method2之間插入被代理類方法
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        HumanUtil util = new HumanUtil();
        util.method1();
        Object returnVal = method.invoke(object,args);
        util.method2();
        return returnVal;
    }
}


class myProxy{
    //動態創建代理類對象
    public static Object getProxyInstance(Object obj){

        //創建代理類對象
        MyInvocationHandler myInvocationHandler = new MyInvocationHandler();
        myInvocationHandler.setObject(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),myInvocationHandler);
    }
}

調用:


public class TestAop {

    public static void main(String[] args) {
        SuperMan superMan = new SuperMan();//被代理類對象

        Object obj = myProxy.getProxyInstance(superMan);//代理類對象
        Human human = (Human) obj;
        human.info();
        System.out.println();
        human.fly();


    }

}

 

 

3.匿名內部類:

package com.spring.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by 小美女 on 2018/12/3.
 */
public class ArithmeticCalulatorLogProxy {
    private ArithmeticCalulator target;
    public  ArithmeticCalulatorLogProxy(ArithmeticCalulator target){
        this.target = target;
    }
    public ArithmeticCalulator getProxy(){
        ArithmeticCalulator proxy = null;

        InvocationHandler invocationHandler = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("invoke");
                Object returnVal = method.invoke(target,args);
                return returnVal;
            }
        };

        proxy = (ArithmeticCalulator) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),invocationHandler);
        return proxy;
    }
}

調用:

package com.spring.aop;

/**
 * @Author: wj
 * @Date: 2018/12/3 10:09
 * @Version 1.0
 */
public class Main {
    public static void main(String[] args) {
        ArithmeticCalulator arithmeticCalulator = new ArithmeticCalulatorImpl();
        ArithmeticCalulatorLogProxy arithmeticCalulatorLogProxy = new ArithmeticCalulatorLogProxy(arithmeticCalulator);
        ArithmeticCalulator a = arithmeticCalulatorLogProxy.getProxy();
        int result = a.add(1,2);
        System.out.println(result);
    }
}

 

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