当前位置: 首页 > news >正文

[Spring]为什么Spring动态代理默认使用CGlib,而不是JDK代理?

文章目录

  • 原因一:CGlib不需要接口
  • 原因二:CGlib效率高
  • 原因三:JDK代理会导致注解失效
  • 如果希望使用JDK代理
  • 扩展
  • AOP in Spring Boot, is it a JDK dynamic proxy or a Cglib dynamic proxy?
      • Spring
      • SpringBoot

原因一:CGlib不需要接口

Spring动态代理默认使用CGlib,是因为它可以代理那些没有实现任何接口的类,而JDK动态代理仅能代理实现了接口的类。

原因二:CGlib效率高

CGlib相对于JDK动态代理来说,在代理类的创建和执行的速度上更快,因此在某些情况下,使用CGlib代理可以提高系统性能。

原因三:JDK代理会导致注解失效

如果Spring是JDK代理,那么就会导致某些注解失效。

如果希望使用JDK代理

  • Spring可以设置proxyTargetClass属性为false来强制使用JDK代理。
  • SpringBoot的AOP 默认使用 cglib,且无法通过proxyTargetClass进行修改。
    如果想修改的话,在Spring配置文件中添加spring.aop.proxy-target-class=false。

——————————————————End————————————————


扩展

AOP in Spring Boot, is it a JDK dynamic proxy or a Cglib dynamic proxy?

As we all know, the underlying AOP is dynamic proxies, and there are two ways to implement dynamic proxies in Java:

  • JDK-based dynamic proxy
  • Dynamic proxy based on Cglib

The biggest difference between these two is that JDK-based dynamic proxies require the object being proxied to implement an interface, while Cglib-based dynamic proxies do not require the object being proxied to implement an interface.

So, how is AOP implemented in Spring? Is it a dynamic proxy based on JDK or a dynamic proxy based on Cglib?

Spring

Let’s start with the conclusion that dynamic proxies in Spring, which one to use, are divided into cases.

If the proxy object implements the interface, then use the JDK dynamic proxy, otherwise it is the Cglib dynamic proxy.
If the proxy object does not implement an interface, then it is a direct Cglib dynamic proxy.

SpringBoot

Spring Boot and Spring are the same, so is it the same strategy for dynamic proxies? Sorry, it’s not really the same.

The handling of this issue in Spring Boot, with Spring Boot 2.0 as the node, is not the same.

Before Spring Boot 2.0, the code for automating the configuration of Aop looked like this (Spring Boot 1.5.22.RELEASE)

@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {@Configuration@EnableAspectJAutoProxy(proxyTargetClass = false)@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",matchIfMissing = true)public static class JdkDynamicAutoProxyConfiguration {}@Configuration@EnableAspectJAutoProxy(proxyTargetClass = true)@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",matchIfMissing = false)public static class CglibAutoProxyConfiguration {}}

As you can see, this automation configuration is mainly discussing the value of the spring.aop.proxy-target-class property in the application.properties configuration file.

The @ConditionalOnProperty annotation is what does the trick. To illustrate a few of the properties in this annotation.

  • prefix: The prefix of the configuration file.
  • name: the name of the configuration file, and prefix together form the key of the configuration.
  • having: the value of the expected configuration. If the actual configuration is the same as the value of having, then the configuration will take effect, otherwise it will not.
  • matchIfMissing: if the developer did not configure it in application.properties, then this configuration class will take effect or not.

Based on the introduction as above, it is easy to see that.

  • If the developer has set spring.aop.proxy-target-class to false, then the JDK proxy is used.
  • If the developer has spring.aop.proxy-target-class set to true, then the Cglib proxy is used.
    = If the developer did not configure the spring.aop.proxy-target-class property in the first place, then the JDK proxy is used.
    This was the case before Spring Boot 2.0.

Let’s look at the situation after Spring Boot 2.0 (inclusive) (Spring Boot 2.0.0.RELEASE).

@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {@Configuration@EnableAspectJAutoProxy(proxyTargetClass = false)@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)public static class JdkDynamicAutoProxyConfiguration {}@Configuration@EnableAspectJAutoProxy(proxyTargetClass = true)@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)public static class CglibAutoProxyConfiguration {}}

As you can see, most of the configuration is the same, with one area that is not quite the same, and that is the value of the matchIfMissing property.
As you can see, starting with Spring Boot 2.0, if the user does not configure anything, the Cglib proxy is used by default.

来自Springboot的一篇英语文章

相关文章:

[Spring]为什么Spring动态代理默认使用CGlib,而不是JDK代理?

文章目录 原因一:CGlib不需要接口原因二:CGlib效率高原因三:JDK代理会导致注解失效如果希望使用JDK代理扩展AOP in Spring Boot, is it a JDK dynamic proxy or a Cglib dynamic proxy?SpringSpringBoot 原因一:CGlib不需要接口 …...

最长上升子序列(二分)代码模板

用二分的思想求最长上升子序列的思想就是保持单调性,用一个q[]数组来作为一个单调数组。 每次将a[i]放进q数组中,但是要保持单调性,q数组的长度就是答案。 q[]数组中存的是所以以下标为长度的最长子序列的结尾的最小值。 理解q[]数组的意义…...

存储优化知识复习一详细版解析

存储优化 知识复习一 一、 选择题 1、1948 年,____提出了“信息熵”(shāng) 的概念,解决了对信息的量化度量问题。 A、薛定谔 B、香农 C、克劳修斯 D、纳什 【参考答案】B2、 RAID2.0技术下,LUN是建立在____上。 A、硬盘 B、条带 C、Chun…...

“暂停加息,股市低迷:242只股票创新低,比特币突破2.8万美元后看涨趋势不可挡!“

11 月1日 FOMC 会议 美联储主席杰罗姆鲍威尔周五在纽约发表讲话,毫不意外地,他采取了更加鸽派的立场,因为在不确定的世界中,美国政府的过度杠杆化和可能即将到来的经济衰退已成为共识。 根据鲍威尔对未来加息的最低限度讨论&…...

微信小程序会议OA系统其他页面

前言: 及上一文章:https://blog.csdn.net/djssubddbj/article/details/133895170?spm1001.2014.3001.5501我们所写的会议OA的首页,在这个上面我们继续完成我们的会议OA系统,这是我们的本期所要完成的页面 自定义组件 微信小程序…...

LabVIEW中使用Get LV Class Default Value 出现错误1498

LabVIEW中使用Get LV Class Default Value 出现错误1498 在LabVIEW中开发了一个应用程序,其中包含可以在执行时动态配置插件的基类。生成可执行文件后,当应用程序要执行子类时,收到以下错误信息。 Error1498 occurred at Gen LV Class Defa…...

RabbitMQ中的核心概念和交换机类型

目录 一、RabbitMQ相关概念二、Exchange类型三、RabbitMQ概念模型总结 一、RabbitMQ相关概念 Producer:生产者,就是投递消息的一方。生产者创建消息,然后发布到RabbitMQ中。消息一般可以包含两个部分:消息体和附加消息。 消息体…...

HarmonyOS开发:Log工具类源码分析

前言 一转眼就十月中旬了,国庆的劲真大,到现在还未缓过来,以至于要更新的文章迟迟未发布,大家可以看到,最近一段时间的文章,都是关于HarmonyOS相关的,两个原因吧,一是我司有这样的任…...

FFmpeg和rtsp服务器搭建视频直播流服务

下面使用的是ubuntu的,window系统可以参考: 通过rtsp-simple-server和ffmpeg实现录屏并发布视频直播_rtsp simple server_病毒宇宇的博客-CSDN博客 一、安装rtsp-simple-server (1)下载rtsp-simple-server 下载地址:R…...

数据图册页面(左边一列图片缩略图,右边展示图片大图)

最近要写这么一个页面,左侧一列图片缩略图,点击左侧缩略图后有选中效果,然后右侧展示图片原图,还能够左右翻页查看。 最后写了一个demo出来,demo还不是很完善,需要自己修改,后面我也给出了修改建…...

leetcode:105从前序与中序遍历序列构造二叉树

105:从前序与中序遍历序列构造二叉树 啊,好久都没有更新算法题目了。曾今是C,如今是Java,感慨啊。 像树这样的算法题,基本都逃不开递归。递归的思想是:将大任务拆分为小任务。我们不妨构建一个函数&#…...

H5前端开发——DOM

H5前端开发——DOM 在H5前端开发中,DOM(Document Object Model)是一个非常核心的概念,指的是文档对象模型。简单来说,DOM是浏览器将HTML文档转换为一棵树形结构的方式,这样我们可以通过JavaScript脚本语言来操作和修改HTML文档。 DOM模型由节点组成,节点包括元素(ELEM…...

专访 Web3Go 新产品 Reiki:培育 AI 原生数字资产与创意新土壤

从 DeFi 到 NFTFi、SocialFi,web3 从业者在尝试 crypto 与区块链技术能为我们的生活、创作、娱乐和文化带来何种新体验,而生成式人工智能的突破性发展则为我们与链上世界的交互、社区内容创作等带来了新的体验,改变互动、交易和价值创造方式。…...

Docker仓库harbor私服搭建

Harbor和Registry都是Docker的镜像仓库,但是Harbor作为更多企业的选择,是因为相比较于Regisrty来说,它具有很多的优势。 提供分层传输机制,优化网络传输 Docker镜像是是分层的,而如果每次传输都使用全量文件(所以用FT…...

【LangChain系列 11】Prompt模版——拼装组合

原文地址:【LangChain系列 11】Prompt模版——拼装组合 本文速读: 多prompt模版组合 单prompt模版拼装 在平常业务开发中,我们常常需要把一些公共模块提取出来作为一个独立的部分,然后将业务中去将这些模块进行组合。在LLM应用…...

JVM三色标记

三色标记 什么是三色标记法 三色标记法,也被称为Tri-color Marking Algorithm,是一种用于追踪对象存活状态的垃圾回收算法。它基于William D. Hana和Mark S. McCulleghan在1976年提出的两色标记法的基础上进行了改进。 与两色标记法只能将对象标记为“…...

UE5--物体卡片与材质入门

参考资料: 《Unreal Engine5 入门到精通》--左央 虚幻引擎5.2文档:https://docs.unrealengine.com/5.2/zh-CN/ 前言: 跟着左央老师的《Unreal Engine5 入门到精通》学习制作AI版胡闹厨房,把学习过程与学习到的东西归纳总结起来。 …...

ios 实现TEXT、DOC、PDF等文档读取与预览

文章目录 一、前言二、iCould相关配置三、功能实现3.1 UIDocumentPickerViewController 选取控制器3.2 读取文件3.3 文档预览3.3.1 下载并保存3.3.2 QLPreviewController预览文档四、总结一、前言 最近正在研发的项目有一个需求: 允许用户将iCloud中的文档上传,实现文件的流…...

智慧矿山:让AI算法提高未戴安全带识别率!

未穿戴安全带识别AI算法,作为智慧矿山的重要应用之一,不仅可以有效提高矿山工作人员的安全意识,还可以降低事故发生的概率。然而,识别准确率的提高一直是该算法面临的挑战之一。为了解决这个问题,研究人员不断努力探索…...

【Unity程序技巧】公共Update管理器

👨‍💻个人主页:元宇宙-秩沅 👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅! 👨‍💻 本文由 秩沅 原创 👨‍💻 收录于专栏:Uni…...

多场景 OkHttpClient 管理器 - Android 网络通信解决方案

下面是一个完整的 Android 实现&#xff0c;展示如何创建和管理多个 OkHttpClient 实例&#xff0c;分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

安宝特方案丨XRSOP人员作业标准化管理平台:AR智慧点检验收套件

在选煤厂、化工厂、钢铁厂等过程生产型企业&#xff0c;其生产设备的运行效率和非计划停机对工业制造效益有较大影响。 随着企业自动化和智能化建设的推进&#xff0c;需提前预防假检、错检、漏检&#xff0c;推动智慧生产运维系统数据的流动和现场赋能应用。同时&#xff0c;…...

【SpringBoot】100、SpringBoot中使用自定义注解+AOP实现参数自动解密

在实际项目中,用户注册、登录、修改密码等操作,都涉及到参数传输安全问题。所以我们需要在前端对账户、密码等敏感信息加密传输,在后端接收到数据后能自动解密。 1、引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId...

【决胜公务员考试】求职OMG——见面课测验1

2025最新版&#xff01;&#xff01;&#xff01;6.8截至答题&#xff0c;大家注意呀&#xff01; 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:&#xff08; B &#xff09; A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...

数据库分批入库

今天在工作中&#xff0c;遇到一个问题&#xff0c;就是分批查询的时候&#xff0c;由于批次过大导致出现了一些问题&#xff0c;一下是问题描述和解决方案&#xff1a; 示例&#xff1a; // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

【JavaSE】绘图与事件入门学习笔记

-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角&#xff0c;以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向&#xff0c;距离坐标原点x个像素;第二个是y坐标&#xff0c;表示当前位置为垂直方向&#xff0c;距离坐标原点y个像素。 坐标体系-像素 …...

ios苹果系统,js 滑动屏幕、锚定无效

现象&#xff1a;window.addEventListener监听touch无效&#xff0c;划不动屏幕&#xff0c;但是代码逻辑都有执行到。 scrollIntoView也无效。 原因&#xff1a;这是因为 iOS 的触摸事件处理机制和 touch-action: none 的设置有关。ios有太多得交互动作&#xff0c;从而会影响…...

Rapidio门铃消息FIFO溢出机制

关于RapidIO门铃消息FIFO的溢出机制及其与中断抖动的关系&#xff0c;以下是深入解析&#xff1a; 门铃FIFO溢出的本质 在RapidIO系统中&#xff0c;门铃消息FIFO是硬件控制器内部的缓冲区&#xff0c;用于临时存储接收到的门铃消息&#xff08;Doorbell Message&#xff09;。…...

Xen Server服务器释放磁盘空间

disk.sh #!/bin/bashcd /run/sr-mount/e54f0646-ae11-0457-b64f-eba4673b824c # 全部虚拟机物理磁盘文件存储 a$(ls -l | awk {print $NF} | cut -d. -f1) # 使用中的虚拟机物理磁盘文件 b$(xe vm-disk-list --multiple | grep uuid | awk {print $NF})printf "%s\n"…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...