As we know, create proxy in runtime, we can use two different techniques, CGLIB or JDK dynamic proxies, what's different between them? when should we use CGLIB? and when should we use JDK proxies? here we have short view about it.
JDK dynamic proxise
If the target class implements one or more interfaces, we should create a JDK dynamic proxy that implements every interface.
(Tip:The source code case can see the article:http://danni505.blog.51cto.com/15547/217359)
CGLIB Proxy
If the target class implements no interfaces, one class itself, in this case, the JDK proxy may be not can used, we should better use CGLIB to create a new class on the fly that is a subclass ("extends") the target class.
(Tip:The source code case can see the article:《How does proxy do in CGLIB?》)
Compare
Compare above two articles we can find this conclusion:
JDK dynamic proxy cannot be casted to the original target class because it's simply a dynamic proxy that happens to implement the same interface(s) as the target. This has the effect of "nudging" you to program to interfaces if they're being used in your application's model, since proxies will usually be invoked through those interfaces.
On the other hand, if interfaces are completely absent from your model, create CGLIB proxies that can be treated more-or-less just like the target class itself.
(Tip: Welcome any discussion with people thinking this things, msn me by danni-505@hotmail.com)
本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/346896
,如需转载请自行联系原作者