(5)spring常用模式--------代理模式

代理模式:為其他的對(duì)象提供代理控制這個(gè)對(duì)象的訪問(wèn)
代理模式的關(guān)鍵點(diǎn)是:代理對(duì)象目標(biāo)對(duì)象.代理對(duì)象是對(duì)目標(biāo)對(duì)象的擴(kuò)展,并會(huì)調(diào)用目標(biāo)對(duì)象.
代理模式的作用:aop實(shí)現(xiàn)攔截器、中介、媒婆、解耦、專人做專事
spring的體現(xiàn): Spring 的 Proxy 模式在 AOP 中有體現(xiàn), 比如 JdkDynamicAopProxyCglib2AopProxy

1.代理模式分類(lèi)

在介紹分類(lèi)之前,先了解一下代理模式的UML圖


代理模式.png

靜態(tài)代理的特點(diǎn):在代理之前,所有的東西都是已知的(人工)
動(dòng)態(tài)代理的特點(diǎn):在代理之后,所有的東西都是未知的(自動(dòng)化)

一般的實(shí)現(xiàn)分為:

  • 靜態(tài)代理(目標(biāo)對(duì)象和代理對(duì)象實(shí)現(xiàn)相同的父類(lèi)或者接口)
  • 動(dòng)態(tài)代理 (具有動(dòng)態(tài)性實(shí)現(xiàn)方法調(diào)用)
     jdk動(dòng)態(tài)代理 (基于接口的形式)
     cglib動(dòng)態(tài)代理 (基于繼承的形式)

2.靜態(tài)代理實(shí)現(xiàn)

一個(gè)人通用的接口

/**
 * @Project: spring
 * @description: 人的接口
 * @author: sunkang
 * @create: 2018-08-28 20:49
 * @ModificationHistory who      when       What
 **/
public interface Person {
    //找對(duì)象
    void findLove();
}

目標(biāo)對(duì)象

/**
 * @Project: spring
 * @description:  目標(biāo)對(duì)象  
 * 男人想找對(duì)象
 * @author: sunkang
 * @create: 2018-08-28 20:54
 * @ModificationHistory who      when       What
 **/
public class Man implements Person {
    //找對(duì)象
    @Override
    public void findLove() {
        System.out.println("我是一個(gè)男人,想找一位女朋友");
    }
}

靜態(tài)代理實(shí)現(xiàn) 需要實(shí)現(xiàn)跟目標(biāo)對(duì)象一樣的接口,重寫(xiě)findlove方法完成代理

/**
 * @Project: spring
 * @description:  靜態(tài)代理   實(shí)現(xiàn)目標(biāo)對(duì)象的代理
 * @author: sunkang
 * @create: 2018-09-02 22:30
 * @ModificationHistory who      when       What
 **/
public class StaticProxy implements Person {

    private Person person;

    public StaticProxy(Person person) {
        this.person = person;
    }
    public void  findLove(){
        System.out.println("根據(jù)你的要求物色");
        this.person.findLove();
        System.out.println("雙方父母是不是同意");
    }
    //靜態(tài)代理測(cè)試
    public static void main(String[] args) {
        StaticProxy staticProxy = new StaticProxy(new Man());
        staticProxy.findLove();
    }
}

3.jdk動(dòng)態(tài)代理實(shí)現(xiàn)

jdk動(dòng)態(tài)代理對(duì)象 媒婆

/**
 * @Project: spring
 * @description:  代理對(duì)象  媒婆
 * @author: sunkang
 * @create: 2018-08-28 20:49
 * @ModificationHistory who      when       What
 **/
public class MeiPo implements InvocationHandler {

    private Person person;

    public Person getInstance(Person person){
        this.person = person;
        Class clazz = person.getClass();
        System.out.println("原有的person的類(lèi)為:"+clazz.getName());
        //基于jdk 中Proxy類(lèi)用來(lái)生成一個(gè)新的對(duì)象(字節(jié)碼重組來(lái)實(shí)現(xiàn))
        return (Person) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理類(lèi)為:"+proxy.getClass());
        System.out.println("我是媒婆");
        //這樣就會(huì)循環(huán)觸發(fā)調(diào)用
        //method.invoke(proxy,args);
        method.invoke(person,args);
            System.out.println("正在幫你找");
        return null;
    }
}

jdk動(dòng)態(tài)方法測(cè)試

/**
 * @Project: spring
 * @description:
 * @author: sunkang
 * @create: 2018-08-28 20:55
 * @ModificationHistory who      when       What
 **/
public class Test {

    public static void main(String[] args) {

        Person   xiaoming = new Man();

        Person  meipo  = new MeiPo().getInstance(xiaoming);

        meipo.findLove();

        //原理:
        //1、拿到被代理對(duì)象的引用,并且獲取到它的所有的接口,反射獲取 (獲取接口)
        //2、JDK Proxy類(lèi)重新生成一個(gè)新的類(lèi)、同時(shí)新的類(lèi)要實(shí)現(xiàn)被代理類(lèi)所有實(shí)現(xiàn)的所有的接口 (實(shí)現(xiàn)接口)
        //3、動(dòng)態(tài)生成Java代碼,把新加的業(yè)務(wù)邏輯方法由一定的邏輯代碼去調(diào)用(在代碼中體現(xiàn))(生成java代碼)
        //4、編譯新生成的Java代碼.class (編譯)
        //5、再重新加載到JVM中運(yùn)行 (加載)
        
        //這里是接口的代碼生成器
        byte[]    data = ProxyGenerator.generateProxyClass("$Proxy0",new Class[]{Person.class});
        try {
            //此為jdk 1.7發(fā)行的nio的類(lèi)  作用是把  data 數(shù)據(jù)寫(xiě)到 文件 $Proxy0.class中 
            Files.newOutputStream(Paths.get("$Proxy0.class")).write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

根據(jù)ProxyGenerator 生成代碼 如下:
可以發(fā)現(xiàn)該動(dòng)態(tài)代理類(lèi)實(shí)現(xiàn)了 Person 的接口,然后根據(jù)接口動(dòng)態(tài)生成了$Proxy0 類(lèi),比如當(dāng)代理對(duì)象調(diào)用findLove的方法實(shí),由具體的InvocationHandler 進(jìn)行調(diào)用invoke方法,而invoke方法實(shí)現(xiàn)了目標(biāo)的對(duì)象的具體調(diào)用,從而實(shí)現(xiàn)動(dòng)態(tài)調(diào)用的作用。

public final class $Proxy0 extends Proxy implements Person {
    private static Method m1;
    private static Method m3;
    private static Method m2;
    private static Method m0;

    public $Proxy0(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {
            return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final void findLove() throws  {
        try {
            super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final String toString() throws  {
        try {
            return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final int hashCode() throws  {
        try {
            return (Integer)super.h.invoke(this, m0, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m3 = Class.forName("com.spring.designPattern.proxy.jdk.Person").getMethod("findLove");
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

4.cglib動(dòng)態(tài)代理實(shí)現(xiàn)

目標(biāo)對(duì)象

/**
 * @Project: spring
 * @description:  cglib  目標(biāo)對(duì)象
 * @author: sunkang
 * @create: 2018-08-28 22:30
 * @ModificationHistory who      when       What
 **/
public class CgMan {
    public void findLove() {
        System.out.println("我是一個(gè)男人,想找一位女朋友");
    }
}

cglib動(dòng)態(tài)代理 媒婆對(duì)象

/**
 * @Project: spring
 * @description: cglib動(dòng)態(tài)代理   媒婆
 * 需要實(shí)現(xiàn) MethodInterceptor接口
 * @author: sunkang
 * @create: 2018-08-28 22:31
 * @ModificationHistory who      when       What
 **/
public class CgMeiPo implements MethodInterceptor {
    
    public Object getInstance(Class clazz){
        Enhancer enhancer = new Enhancer();
        //cglib 主要根據(jù)繼承來(lái)生成代碼的關(guān)系
        //設(shè)置父類(lèi)
        enhancer.setSuperclass(clazz);
        //設(shè)置回調(diào)  就是代理類(lèi)通過(guò)  MethodInterceptor 找到 intercept方法進(jìn)行回調(diào)
        enhancer.setCallback(this);
        //crate 方法
        //第一步、生成源代碼
        //第二步、編譯成class文件
        //第三步、加載到JVM中,并返回被代理對(duì)象
        return  enhancer.create();
    }
    
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("我是媒婆,通過(guò)cglib 來(lái)實(shí)現(xiàn)的");
        //這樣就會(huì)循環(huán)觸發(fā)調(diào)用
        //cglib的代理實(shí)際上原有的對(duì)象的子類(lèi),通過(guò)繼承來(lái)實(shí)現(xiàn),這里默認(rèn)調(diào)用父類(lèi)的方法,obj 為cglib代理的子類(lèi)
        //切記不要寫(xiě)成        methodProxy.invoke(obj,args);  這里面就會(huì)存在循環(huán)調(diào)用的問(wèn)題,這里會(huì)調(diào)用cglib代理類(lèi),然后循環(huán)調(diào)用
       //調(diào)用父類(lèi)
        methodProxy.invokeSuper(obj,args);
        System.out.println("正在幫你找");
        return null;
    }
}

cglib動(dòng)態(tài)代理測(cè)試類(lèi)


/**
 * @Project: spring
 * @description:  cglib 動(dòng)態(tài)代理測(cè)試類(lèi)
 * @author: sunkang
 * @create: 2018-08-28 22:40
 * @ModificationHistory who      when       What
 **/
public class CgTest {

    public static void main(String[] args) {
        CgMan xiaoming = new CgMan();
        CgMan  cgmeipo  = (CgMan) new CgMeiPo().getInstance(xiaoming.getClass());
        cgmeipo.findLove();
    }
}

5.自定義實(shí)現(xiàn)動(dòng)態(tài)代理

思路: 主要根據(jù)ProxyGenerator類(lèi)生成傳入的接口的字節(jié)碼,然后根據(jù)自定義的類(lèi)加載器加載字節(jié)碼得到具體的代理類(lèi),通過(guò)反射得到代理類(lèi)的構(gòu)造方法,最后通過(guò)反射構(gòu)造新對(duì)象返回對(duì)象

具體的代碼如下:

自定義的Person接口

/**
 * @Project: spring
 * @description:  自定義的Person接口
 * @author: sunkang
 * @create: 2018-08-29 21:40
 * @ModificationHistory who      when       What
 **/
public interface CuPerson {
    void findLove();
}

自定義的目標(biāo)對(duì)象

/**
 * @Project: spring
 * @description: 自定義的目標(biāo)對(duì)象
 * @author: sunkang
 * @create: 2018-08-29 21:41
 * @ModificationHistory who      when       What
 **/
public class CuMan  implements CuPerson {
    @Override
    public void findLove() {
        System.out.println("我是一個(gè)男人,想找一位女朋友");
    }
}

自定義的媒婆對(duì)象

/**
 * @Project: spring
 * @description: 自定義的媒婆對(duì)象
 * @author: sunkang
 * @create: 2018-08-29 20:33
 * @ModificationHistory who      when       What
 **/
public class CuMeipo implements InvocationHandler {

    private CuPerson person;

    public CuPerson getInstance(CuPerson person){
        this.person = person;
        Class clazz = person.getClass();
        System.out.println("原有的person的類(lèi)為:"+clazz.getName());
        return (CuPerson) CuProxy.newProxyInstance(clazz.getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理類(lèi)為:"+proxy.getClass());
        System.out.println("我是媒婆");
        //這樣就會(huì)循環(huán)觸發(fā)調(diào)用
        //method.invoke(proxy,args);
        method.invoke(person,args);
        System.out.println("正在幫你找");
        return null;
    }
}

自定義的生成代理類(lèi)的實(shí)現(xiàn)

/**
 * @Project: spring
 * @description:  自定義的生成代理類(lèi)的實(shí)現(xiàn)
 * @author: sunkang
 * @create: 2018-08-29 20:19
 * @ModificationHistory who      when       What
 **/
public class CuProxy {
    public static Object newProxyInstance(
            Class<?>[] interfaces,
            InvocationHandler h) {
        //1.根據(jù)接口生成class文件
        byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0", interfaces);
        try {
            //寫(xiě)入一個(gè)文件可以查看生成的源代碼
            OutputStream os = Files.newOutputStream(Paths.get("$Proxy0.class"));
            os.write(bytes);
            //2.通過(guò)自定義類(lèi)加載器動(dòng)態(tài)加載代理類(lèi)
            CuClassLoader loader = new CuClassLoader(bytes);
            Class proxyClazz = loader.loadClass("$Proxy0");
            //3.得到代理類(lèi)的構(gòu)造方法
            System.out.println(proxyClazz.getName());
            Constructor construct = proxyClazz.getConstructor(InvocationHandler.class);
            //4.返回代理對(duì)象
            return construct.newInstance(h);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

自定義的類(lèi)加載器

/**
 * @Project: spring
 * @description: 自定義的類(lèi)加載器
 * 要實(shí)現(xiàn)  findClass對(duì)象  實(shí)現(xiàn)自定義類(lèi)的加載
 * 最后通過(guò)findclass 的native方法加載返回類(lèi)
 *
 * @author: sunkang
 * @create: 2018-08-29 20:25
 * @ModificationHistory who      when       What
 **/
public class CuClassLoader extends ClassLoader {

    private byte[] bytes ;

    public CuClassLoader(byte[] bytes) {
        this.bytes = bytes;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {

        String className = CuClassLoader.class.getPackage().getName() + "." + name;

        return  defineClass(name,bytes,0,bytes.length);
    }
}

自定義的測(cè)試方法

/**
 * @Project: spring
 * @description:  自定義的測(cè)試方法
 * @author: sunkang
 * @create: 2018-08-29 20:33
 * @ModificationHistory who      when       What
 **/
public class CuTest {
    public static void main(String[] args) {

        CuPerson xiaoming = new CuMan();

        CuPerson  meipo  = new CuMeipo().getInstance(xiaoming);

        meipo.findLove();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容