亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

1. 靜態(tài)代理與動(dòng)態(tài)代理區(qū)別

靜態(tài)代理:需要編寫批量代理類

動(dòng)態(tài)代理:不需要編寫批量代理類

2.靜態(tài)代理

靜態(tài)代理結(jié)構(gòu)如下:

編寫測(cè)試接口:

public interface TestInteface {
 /**
 * 測(cè)試方法
 */
 String runNow(String param);
}//end

編寫測(cè)試類:

public class TestProxy implements TestInteface {
 public String runNow(String param) {
 System.out.println("param:" + param);
 return "result:" + param;
 }
}//end

編寫靜態(tài)代理類:

public class StaticProxy implements TestInteface {
 private TestInteface testInteface;

 public StaticProxy(TestInteface testInteface) {
 this.testInteface = testInteface;
 }

 @Override
 public String runNow(String param) {
 System.out.println("靜態(tài)代理開始");
 String result = testInteface.runNow(param);
 System.out.println("靜態(tài)代理結(jié)束");
 return result;
 }
}//end

編寫main方法:

public static void main(String[] args) {
 TestInteface testInteface = new TestProxy();
 StaticProxy staticProxy = new StaticProxy(testInteface);
 String s = staticProxy.runNow("這是靜態(tài)代理");
 System.out.println(s);
}

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

 

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

 

 

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

動(dòng)態(tài)代理分為JDK動(dòng)態(tài)代理、CGLIB動(dòng)態(tài)代理。

JDK動(dòng)態(tài)代理結(jié)構(gòu)如下:

編寫通用代理類:

public class JdkProxy implements InvocationHandler {
 /**
 * 目標(biāo)類
 */
 private Object target;

 public JdkProxy(Object target) {
 this.target = target;
 }

 /*
 * @Param: proxy 動(dòng)態(tài)代理的實(shí)例
 * @Param: method 目標(biāo)類的方法
 * @Param: args 目標(biāo)類方法的參數(shù)
 * @Return: 目標(biāo)類方法的執(zhí)行結(jié)果
 * @Description:
 */
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 System.out.println("JDK動(dòng)態(tài)代理開始");
 Object result = method.invoke(target, args);
 System.out.println("JDK動(dòng)態(tài)代理結(jié)束");
 return result;
 }
}//end

編寫main方法:

public static void main(String[] args) {
 try {
 TestInteface testInteface = new TestProxy();
 JdkProxy jdkProxy = new JdkProxy(testInteface);
 TestInteface testProxy_ = (TestInteface) Proxy.newProxyInstance(testInteface.getClass().getClassLoader(), testInteface.getClass().getInterfaces(), jdkProxy);
 String r = testProxy_.runNow("這是JDK動(dòng)態(tài)代理");
 System.out.println(r);
 } catch (Exception e) {
 System.out.println(e);
 }
}

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

 

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

 

 

CGLIB動(dòng)態(tài)代理結(jié)構(gòu)如下:

由于JDK動(dòng)態(tài)代理是JDK原生,CGLIB動(dòng)態(tài)代理是第三方,所以需要引入如下jar包:

compile 'cglib:cglib:3.3.0'
compile group: 'org.Apache.ant', name: 'ant', version: '1.10.3'
compile group: 'org.ow2.asm', name: 'asm', version: '7.1'

編寫通用代理類:

public class CGLIBProxy implements MethodInterceptor {
 /**
 * @Param: obj 目標(biāo)對(duì)象
 * @Param: method 目標(biāo)對(duì)象方法
 * @Param: args 目標(biāo)對(duì)象方法參數(shù)
 * @Param: proxy 代理類方法
 * @Return:
 */
 @Override
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
 System.out.println("CGLIB動(dòng)態(tài)代理開始");
 Object result = proxy.invokeSuper(obj, args);
 System.out.println("CGLIB動(dòng)態(tài)代理結(jié)束");
 return result;
 }
}//end

編寫main方法:

public static void main(String[] args) {
 CGLIBProxy cglibProxy = new CGLIBProxy();
 //強(qiáng)化劑實(shí)例化
 Enhancer enhancer = new Enhancer();
 //強(qiáng)化目標(biāo)類
 enhancer.setSuperclass(TestProxy.class);
 //使用通用代理類進(jìn)行強(qiáng)化
 enhancer.setCallback(cglibProxy);
 //創(chuàng)建目標(biāo)對(duì)象
 TestProxy o = (TestProxy) enhancer.create();
 String s = o.runNow("這是CGLIB動(dòng)態(tài)代理");
 System.out.println(s);
}

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

 

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

 

 

4.總結(jié)

靜態(tài)代理和動(dòng)態(tài)代理都需要編寫代理類,不同的是動(dòng)態(tài)代理不需要批量編寫代理類,只是需要編寫一個(gè)通用代理類就行了。JDK動(dòng)態(tài)代理中實(shí)現(xiàn)類必須實(shí)現(xiàn)接口,CGLIB則沒有此限制。

代理的使用場(chǎng)景:鑒權(quán)、打印方法前后日志、減少代碼的侵入性。

分享到:
標(biāo)簽:java
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定