Difference between CGLIB & JDK Dynamic Proxies

简介:
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 ,如需转载请自行联系原作者



相关文章
|
Java 数据库 Spring
【异常】because it is a JDK dynamic proxy that implements
【异常】because it is a JDK dynamic proxy that implements
844 0
|
Java
Java(JDK)13新特性之Dynamic CDS Archives
Java(JDK)13新特性之Dynamic CDS Archives
430 0
|
XML Java 数据库连接
Error:JDK1.8 Cannot change version of project facet Dynamic Web Module to 3.1.
问题: Error:JDK1.8 Cannot change version of project facet Dynamic Web Module to 3.1. 此工程为名为mybatis:maven webapp project:'mybatis':解决检查以下文件中配置的,参数: 解决设置1:设置\src\main\webapp\WEB-INF\web.xml <?
4755 0
安装JDK18没有JRE环境的解决办法
安装JDK18没有JRE环境的解决办法
1605 141
|
11月前
|
存储 Ubuntu 安全
在Ubuntu 16.04上安装openjdk-6/7/8-jdk的步骤
在整个安装过程中,你可能需要管理员权限,因此你可能要使用 `sudo` 来获取必要的权限。记得做完每一个步骤后,都要检查输出,以确保没有发生错误,并且每项操作都成功完成。如果在安装过程中遇到问题,查看 `/var/log/` 下的日志文件对于问题的解决可能是有帮助的。
705 21
|
11月前
|
IDE Ubuntu Java
在Ubuntu18.04安装兼容JDK 8的Eclipse集成开发环境的指南。
完成以上步骤后,您将在Ubuntu 18.04系统上成功安装并配置了Eclipse IDE,它将与JDK 8兼容,可以开始进行Java开发工作。如果遇到任何问题,请确保每一步骤都正确执行,并检查是否所有路径都与您的具体情况相匹配。
512 11
|
Java 关系型数据库 MySQL
在Linux平台上进行JDK、Tomcat、MySQL的安装并部署后端项目
现在,你可以通过访问http://Your_IP:Tomcat_Port/Your_Project访问你的项目了。如果一切顺利,你将看到那绚烂的胜利之光照耀在你的项目之上!
597 41
|
Oracle Java 关系型数据库
Tomcat和JDK的详细安装、下载和环境配置指南
以上就是JDK和Tomcat的下载、安装和环境配置的详细步骤。希望这个指南能帮助你顺利完成设置。
906 32
|
10月前
|
Ubuntu Java Android开发
在Ubuntu 18.04上安装与JDK 8兼容的Eclipse版本的步骤。
安装过程结束后,您就可以开始使用Eclipse来开发您的Java项目了,并且确保它与JDK 8兼容无误。这个过程涉及的是一个基本的安装流程,针对使用Java 8的用户,Eclipse的其他配置和插件安装根据个人开发环境和需求来定制。
567 0
|
开发框架 Java 关系型数据库
在Linux系统中安装JDK、Tomcat、MySQL以及部署J2EE后端接口
校验时,浏览器输入:http://[your_server_IP]:8080/myapp。如果你看到你的应用的欢迎页面,恭喜你,一切都已就绪。
827 17