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é)果如下:
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é)果如下:
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é)果如下:
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)、打印方法前后日志、減少代碼的侵入性。






