JAVA中靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別有哪些-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)JAVA中靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別有哪些,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、寶清網(wǎng)絡(luò)推廣、小程序制作、寶清網(wǎng)絡(luò)營銷、寶清企業(yè)策劃、寶清品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供寶清建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn

代理模式是java中最常用的設(shè)計(jì)模式之一,尤其是在spring框架中廣泛應(yīng)用。對于java的代理模式,一般可分為:靜態(tài)代理、動(dòng)態(tài)代理、以及CGLIB實(shí)現(xiàn)動(dòng)態(tài)代理。

對于上述三種代理模式,分別進(jìn)行說明。

1.靜態(tài)代理

靜態(tài)代理其實(shí)就是在程序運(yùn)行之前,提前寫好被代理方法的代理類,編譯后運(yùn)行。在程序運(yùn)行之前,class已經(jīng)存在。
下面我們實(shí)現(xiàn)一個(gè)靜態(tài)代理demo:

JAVA中靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別有哪些

靜態(tài)代理

定義一個(gè)接口Target

package com.test.proxy;

public interface Target {

  public String execute();
}

TargetImpl 實(shí)現(xiàn)接口Target

package com.test.proxy;

public class TargetImpl implements Target {

  @Override
  public String execute() {
    System.out.println("TargetImpl execute!");
    return "execute";
  }
}

代理類

package com.test.proxy;

public class Proxy implements Target{

  private Target target;

  public Proxy(Target target) {
    this.target = target;
  }

  @Override
  public String execute() {
    System.out.println("perProcess");
    String result = this.target.execute();
    System.out.println("postProcess");
    return result;
  }
}

測試類:

package com.test.proxy;

public class ProxyTest {

  public static void main(String[] args) {

    Target target = new TargetImpl();
    Proxy p = new Proxy(target);
    String result = p.execute();
    System.out.println(result);
  }

}

運(yùn)行結(jié)果:

perProcess
TargetImpl execute!
postProcess
execute

靜態(tài)代理需要針對被代理的方法提前寫好代理類,如果被代理的方法非常多則需要編寫很多代碼,因此,對于上述缺點(diǎn),通過動(dòng)態(tài)代理的方式進(jìn)行了彌補(bǔ)。

2.動(dòng)態(tài)代理

動(dòng)態(tài)代理主要是通過反射機(jī)制,在運(yùn)行時(shí)動(dòng)態(tài)生成所需代理的class.

JAVA中靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別有哪些

動(dòng)態(tài)代理

接口

package com.test.dynamic;

public interface Target {

  public String execute();
}

實(shí)現(xiàn)類

package com.test.dynamic;

public class TargetImpl implements Target {

  @Override
  public String execute() {
    System.out.println("TargetImpl execute!");
    return "execute";
  }
}

代理類

package com.test.dynamic;


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

public class DynamicProxyHandler implements InvocationHandler{

  private Target target;

  public DynamicProxyHandler(Target target) {
    this.target = target;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("========before==========");
    Object result = method.invoke(target,args);
    System.out.println("========after===========");
    return result;
  }
}

測試類

package com.test.dynamic;

import java.lang.reflect.Proxy;

public class DynamicProxyTest {

  public static void main(String[] args) {
    Target target = new TargetImpl();
    DynamicProxyHandler handler = new DynamicProxyHandler(target);
    Target proxySubject = (Target) Proxy.newProxyInstance(TargetImpl.class.getClassLoader(),TargetImpl.class.getInterfaces(),handler);
    String result = proxySubject.execute();
    System.out.println(result);
  }

}

運(yùn)行結(jié)果:

========before==========
TargetImpl execute!
========after===========
execute

無論是動(dòng)態(tài)代理還是靜態(tài)帶領(lǐng),都需要定義接口,然后才能實(shí)現(xiàn)代理功能。這同樣存在局限性,因此,為了解決這個(gè)問題,出現(xiàn)了第三種代理方式:cglib代理。

3.cglib代理

CGLib采用了非常底層的字節(jié)碼技術(shù),其原理是通過字節(jié)碼技術(shù)為一個(gè)類創(chuàng)建子類,并在子類中采用方法攔截的技術(shù)攔截所有父類方法的調(diào)用,順勢織入橫切邏輯。JDK動(dòng)態(tài)代理與CGLib動(dòng)態(tài)代理均是實(shí)現(xiàn)Spring AOP的基礎(chǔ)。

JAVA中靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別有哪些

cglib動(dòng)態(tài)代理

目標(biāo)類

package com.test.cglib;

public class Target {

  public String execute() {
    String message = "-----------test------------";
    System.out.println(message);
    return message;
  }
}

通用代理類:

package com.test.cglib;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MyMethodInterceptor implements MethodInterceptor{

  @Override
  public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    System.out.println(">>>>MethodInterceptor start...");
    Object result = proxy.invokeSuper(obj,args);
    System.out.println(">>>>MethodInterceptor ending...");
    return "result";
  }
}

測試類

package com.test.cglib;

import net.sf.cglib.proxy.Enhancer;

public class CglibTest {

  public static void main(String ... args) {
    System.out.println("***************");
    Target target = new Target();
    CglibTest test = new CglibTest();
    Target proxyTarget = (Target) test.createProxy(Target.class);
    String res = proxyTarget.execute();
    System.out.println(res);
  }

  public Object createProxy(Class targetClass) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(targetClass);
    enhancer.setCallback(new MyMethodInterceptor());
    return enhancer.create();
  }

}

執(zhí)行結(jié)果:

***************
>>>>MethodInterceptor start...
-----------test------------
>>>>MethodInterceptor ending...
result

代理對象的生成過程由Enhancer類實(shí)現(xiàn),大概步驟如下:

1、生成代理類Class的二進(jìn)制字節(jié)碼;

2、通過Class.forName加載二進(jìn)制字節(jié)碼,生成Class對象;

3、通過反射機(jī)制獲取實(shí)例構(gòu)造,并初始化代理類對象。

上述就是小編為大家分享的JAVA中靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別有哪些了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:JAVA中靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別有哪些-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://muchs.cn/article22/dejocc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、網(wǎng)站建設(shè)全網(wǎng)營銷推廣、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)