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
255 0
|
Java
Java(JDK)13新特性之Dynamic CDS Archives
Java(JDK)13新特性之Dynamic CDS Archives
227 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 <?
4542 0
|
2月前
|
Java
安装JDK18没有JRE环境的解决办法
安装JDK18没有JRE环境的解决办法
325 3
|
3月前
|
Java 关系型数据库 MySQL
"解锁Java Web传奇之旅:从JDK1.8到Tomcat,再到MariaDB,一场跨越数据库的冒险安装盛宴,挑战你的技术极限!"
【8月更文挑战第19天】在Linux上搭建Java Web应用环境,需安装JDK 1.8、Tomcat及MariaDB。本指南详述了使用apt-get安装OpenJDK 1.8的方法,并验证其版本。接着下载与解压Tomcat至`/usr/local/`目录,并启动服务。最后,通过apt-get安装MariaDB,设置基本安全配置。完成这些步骤后,即可验证各组件的状态,为部署Java Web应用打下基础。
57 1
|
3月前
|
Oracle Java 关系型数据库
Mac安装JDK1.8
Mac安装JDK1.8
694 4
|
4月前
|
Java Linux
Linux复制安装 jdk 环境
Linux复制安装 jdk 环境
108 3
|
6天前
|
Ubuntu Java
Ubuntu之jdk安装
以下是Ubuntu之jdk安装的详细内容
13 0
|
1月前
|
Oracle Java 关系型数据库
jdk17安装全方位手把手安装教程 / 已有jdk8了,安装JDK17后如何配置环境变量 / 多个不同版本的JDK,如何配置环境变量?
本文提供了详细的JDK 17安装教程,包括下载、安装、配置环境变量的步骤,并解释了在已有其他版本JDK的情况下如何管理多个JDK环境。
712 0
|
3月前
|
Java 开发工具
开发工具系列 之 同一个电脑上安装多个版本的JDK
这篇文章介绍了如何在一台电脑上安装和配置多个版本的JDK,包括从官网下载所需JDK、安装过程、配置环境变量以及如何查看和切换当前使用的JDK版本,并提到了如果IDEA和JDK版本不兼容时的解决方法。
开发工具系列 之 同一个电脑上安装多个版本的JDK