本文實(shí)例講述了java動態(tài)代理的兩種實(shí)現(xiàn)方式。分享給大家供大家參考,具體如下:
一說到動態(tài)代理,我們第一個(gè)想到肯定是大名鼎鼎的spring aop了。在aop的源碼中用到了兩種動態(tài)代理來實(shí)現(xiàn)攔截切入功能:jdk動態(tài)代理和cglib動態(tài)代理。兩種方法同時(shí)存在,各有優(yōu)劣。jdk動態(tài)代理是由java內(nèi)部的反射機(jī)制來實(shí)現(xiàn)的,cglib動態(tài)代理是通過繼承來實(shí)現(xiàn)的,底層則是借助asm(java 字節(jié)碼操控框架)來實(shí)現(xiàn)的(采用字節(jié)碼的方式,給a類創(chuàng)建一個(gè)子類b,子類b使用方法攔截的技術(shù)攔截所以父類的方法調(diào)用)。總的來說,反射機(jī)制在生成類的過程中比較高效,而asm在生成類之后的相關(guān)執(zhí)行過程中比較高效(可以通過將asm生成的類進(jìn)行緩存,這樣解決asm生成類過程低效問題)。還有一點(diǎn)必須注意:jdk動態(tài)代理的應(yīng)用前提,必須是目標(biāo)類基于統(tǒng)一的接口。如果沒有上述前提,jdk動態(tài)代理不能應(yīng)用。由此可以看出,jdk動態(tài)代理有一定的局限性,cglib這種第三方類庫實(shí)現(xiàn)的動態(tài)代理應(yīng)用更加廣泛,且在效率上更有優(yōu)勢。。
公用的接口和實(shí)現(xiàn)類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public interface userservice { public string getname( int id); public integer getage( int id); } public class userserviceimpl implements userservice { @override public string getname( int id) { system.out.println( "------getname------" ); return "tom" ; } @override public integer getage( int id) { system.out.println( "------getage------" ); return 10 ; } } |
jdk的動態(tài)代理實(shí)現(xiàn)
jdk的動態(tài)代理,依賴的是反射包下的invocationhandler接口,我們的代理類實(shí)現(xiàn)invocationhandler,重寫invoke()方法,每當(dāng)我們的代理類調(diào)用方法時(shí),都會默認(rèn)先經(jīng)過invoke()方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class userinvocationhandler implements invocationhandler { private object target; userinvocationhandler() { super (); } userinvocationhandler(object target) { super (); this .target = target; } @override public object invoke(object o, method method, object[] args) throws throwable { if ( "getname" .equals(method.getname())){ system.out.println( "++++++before " + method.getname() + "++++++" ); object result = method.invoke(target, args); system.out.println( "++++++after " + method.getname() + "++++++" ); return result; } else { object result = method.invoke(target, args); return result; } } } |
測試類
1
2
3
4
5
6
7
8
9
10
11
12
|
public class m { public static void main(string[] args) { userservice userservice = new userserviceimpl(); invocationhandler invocationhandler = new userinvocationhandler(userservice); userservice userserviceproxy = (userservice) proxy.newproxyinstance( userservice.getclass().getclassloader(), userservice.getclass().getinterfaces(), invocationhandler); system.out.println(userserviceproxy.getname( 1 )); system.out.println(userserviceproxy.getage( 1 )); } } |
測試效果
cglib的動態(tài)代理實(shí)現(xiàn)
cglib依賴的是cglib包下的methodinterceptor接口,每調(diào)用代理類的方法,都會調(diào)用intercept方法
1
2
3
4
5
6
7
8
9
|
public class cglibmethodinterceptor implements methodinterceptor { @override public object intercept(object o, method method, object[] args, methodproxy methodproxy) throws throwable { system.out.println( "------before " + methodproxy.getsupername() + "------" ); object o1 = methodproxy.invokesuper(o, args); system.out.println( "------after " + methodproxy.getsupername() + "------" ); return o1; } } |
測試類
1
2
3
4
5
6
7
8
9
10
11
|
public class m { public static void main(string[] args) { cglibmethodinterceptor cglibproxy = new cglibmethodinterceptor(); enhancer enhancer = new enhancer(); enhancer.setsuperclass(userserviceimpl. class ); enhancer.setcallback(cglibproxy); userservice o = (userservice) enhancer.create(); o.getname( 1 ); o.getage( 1 ); } } |
測試結(jié)果
ps:cglib的動態(tài)代理,需要cglib.jar和asm.jar支持
附:點(diǎn)擊此處本站下載 cglib.jar asm.jar 。
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/zy_281870667/article/details/53216776