您好,登錄后才能下訂單哦!
靜態(tài)代理
第一種實(shí)現(xiàn)(基于接口):
1》接口
public interface Hello { void say(String msg); }
2》目標(biāo)類,至少實(shí)現(xiàn)一個(gè)接口
public class HelloImpl implements Hello { public void say(String msg) { System.out.println("Hi,"+msg); } }
3》代理類(與目標(biāo)類實(shí)現(xiàn)相同接口,從而保證功能一致)
public class HelloProxy implements Hello{ private Hello hello; public HelloProxy(Hello hello){ this.hello = hello; } public void say(String msg){ before(); hello.say(msg); after(); } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } }
3》測(cè)試
/** * @Author LZHL * @Create 2017-02-19 10:26 * @Description */ public class Main { public static void main(String[] args) throws Exception { HelloImpl target = new HelloImpl(); HelloProxy proxy = new HelloProxy(target); proxy.say("LZHL"); } }
第二種實(shí)現(xiàn)(基于目標(biāo)類):
1>目標(biāo)類
public class HelloTarget { public void sayHello(String name){ System.out.println("Hi,"+name); } }
2>代理類(通過繼承目標(biāo)類,保證功能一致)
public class HelloProxy extends HelloTarget{ private HelloTarget target; public HelloProxy(HelloTarget target){ this.target = target; } @Override public void sayHello(String name) { this.before(); target.sayHello(name); this.after(); } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } }
3>測(cè)試
public class Main { public static void main(String[] args) throws Exception { HelloTarget target = new HelloTarget(); HelloProxy proxy= new HelloProxy(target); proxy.sayHello("LZHL"); } }
動(dòng)態(tài)代理
動(dòng)態(tài)代理的代理類是在程序運(yùn)行期間動(dòng)態(tài)生成的,也有兩種實(shí)現(xiàn),一種是JDK動(dòng)態(tài)代理,一種是CGLib動(dòng)態(tài)代理
1》JDK動(dòng)態(tài)代理(基于接口實(shí)現(xiàn),與目標(biāo)類實(shí)現(xiàn)相同接口,從而保證功能一致)
/** * @Author LZHL * @Create 2017-02-19 12:46 * @Description */ public class Main { public static void main(String[] args){ final HelloImpl target = new HelloImpl(); Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { /* * proxy: 代理對(duì)象 * method: 目標(biāo)對(duì)象的方法對(duì)象 * args: 目標(biāo)對(duì)象方法的參數(shù) * return: 目標(biāo)對(duì)象方法的返回值 */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before"); Object retValue = method.invoke(target, args); System.out.println("after"); return retValue; } }); Hello proxy = (Hello) proxyInstance; proxy.say("LYX"); //可以把InvocationHandler提取出來,單獨(dú)寫一個(gè)類,為了方便大家看,這里我用內(nèi)部類的形式 class JDKProxy implements InvocationHandler { private Object target; public JDKProxy(Object target){ this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { before(); Object result = method.invoke(target, args); after(); return result; } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } } InvocationHandler ih = new JDKProxy(target); Object proxyInstance2 = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), ih); Hello proxy2 = (Hello) proxyInstance2; proxy2.say("LZHL"); } }
2》CGLib動(dòng)態(tài)代理(基于目標(biāo)類,通過繼承目標(biāo)類,從而保證功能一致),需要導(dǎo)入cglib-3.2.4.jar包
pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.4</version> </dependency> </dependencies>
1)目標(biāo)類
public class Hi { public void sayHi(String msg){ System.out.println("Hi,"+msg); } }
2)測(cè)試
/** * @Author LZHL * @Create 2017-02-19 13:19 * @Description */ public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); //設(shè)置父類 enhancer.setSuperclass(Hi.class); //設(shè)置回調(diào)函數(shù) enhancer.setCallback(new MethodInterceptor() { public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("before"); Object retValue = methodProxy.invokeSuper(target, args); System.out.println("after"); return retValue; } }); Object proxy = enhancer.create(); Hi hi = (Hi) proxy; hi.sayHi("LXY"); //可以把MethodInterceptor提取出來,單獨(dú)寫一個(gè)類,為了方便大家看,這里我用內(nèi)部類的形式 class CGLibProxy implements MethodInterceptor { public <T> T getProxy(Class<T> clazz){ return (T) Enhancer.create(clazz, this); } public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { before(); Object result = proxy.invokeSuper(target, args); after(); return result; } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } } CGLibProxy cgLibProxy = new CGLibProxy(); Hi hi2 = cgLibProxy.getProxy(Hi.class); hi2.sayHi("LZHL"); } }
以上所述是小編給大家介紹的Java靜態(tài)代理和動(dòng)態(tài)代理總結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。