Spring AOP self-invocation及其他问题汇总

@Transactional self-invocation (in effect, a method within the target object calling another method 在Spring框架中,注解用于声明事务管理。当你在一个类的方法上使用注解时,Spring 会为这个方法创建一个事务代理。这意味着方法的调用实际上是通过代理对象来转发的,以便Spring可以管理事务。然而,当一个注解的方法在同一个类的内部调用另一个注解的方法时,这种调用被称为“自调用”(self-invocation)。在这种情况下,Spring事务代理不会介入,因为代理对象没有被创建,或者调用没有通过代理对象转发。 阅读详情

spring reference中描述:

. It means that self-invocation is not going to result in the advice associated with a method invocation getting a chance to execute.

……

Finally, it must be noted that AspectJ does not have this self-invocation issue because it is not a proxybased AOP framework.

……

Method visibility and cache annotations
When using proxies, you should apply the cache annotations only to methods with public visibility. If you do annotate protected, private or package-visible methods with these annotations, no error is raised, but the annotated method does not exhibit the configured caching settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods as it changes the bytecode itself.

Tip
Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Cache* annotation, as opposed to annotating interfaces. You certainly can place the @Cache* annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies ( proxy-targetclass="true") or the weaving-based aspect ( mode="aspectj"), then the caching settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a caching proxy, which would be decidedly bad.
Note
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual caching at runtime even if the invoked method is marked with @Cacheable - considering using the aspectj mode in this case. Also, the proxy must be fully initialized to provide the expected behaviour so you should not rely on this feature in your initialization code, i.e. @PostConstruct.


Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.

You can place the @Transactional annotation before an interface definition, a method on an interface, a class definition, or a public method on a class. However, the mere presence of the @Transactional annotation is not enough to activate the transactional behavior. The @Transactional annotation is simply metadata that can be consumed by some runtime infrastructure that is @Transactional-aware and that can use the metadata to configure the appropriate beans with transactional behavior. In the preceding example, the <tx:annotation-driven/> element switches on the transactional behavior.
Tip
Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies ( proxy-target-class="true") or the weaving-based aspect ( mode="aspectj"), then the transaction settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a transactional proxy, which would be decidedly bad.
Note
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. Also, the proxy must be fully initialized to provide the expected behaviour so you should not rely on this feature in your initialization code, i.e. @PostConstruct.
Consider the use of AspectJ mode (see mode attribute in table below) if you expect self-invocations to be wrapped with transactions as well. In this case, there will not be a proxy in the first place; instead, the target class will be weaved (that is, its byte code will be modified) in order to turn @Transactional into runtime behavior on any kind of method.

Spring注解驱动之再说事务啊 问题引入 Spring中事务传播有哪几种,分别是怎样的? 理解注解事务的自动配置? SpringBoot启动类为什么不需要加@EnableTransactionManagement注解? 声明式事务的实现原理?(待补充) 1 声明式事务 系统开发中必然与数据打交道,事务管理必不可少。Spring支持声明式事务,通过*@Transactional注解控制方法是否支持事务。声明式事务,基于AOP实现,将具体业务和业务逻辑解耦。 Spring提供了@EnableTransactionManagemen 阅读详情

相关推荐

Spring AOP源码深度解析:从动态代理原理到实战应用

面向切面编程(AOP)是一种重要的编程范式,其核心思想是将横切关注点(如日志、事务、权限校验)从业务逻辑中分离出来,实现代码的模块化和解耦。其实现原理主要基于动态代理技术,通过在运行时为目标对象创建代理,将通知逻辑织入到方法调用链中。这种机制为Java应用带来了显著的工程价值,能够有效减少代码重复、提升可维护性,并统一处理系统级的非功能需求。在Spring框架中,AOP被广泛应用于事务管理、安全控制、性能监控和日志记录等典型场景。本文以权限校验为例,深入剖析了Spring AOP中JDK动态代理与CGLIB

weixin_34038293的博客 328

同一个类中方法调用,导致@Transactional失效

在这个例子中,A方法和B方法属于同一个类,同一个类的两个方法之间的调用,并没有“coming in through the proxy”,没有。我这样修改了之后,事务就会回滚了,验证了Spring官方文档的这段话是正确的。A方法调用同一个类的另一个方法,称之为“B方法”,@Transactional注解加在B方法上面,查了一下Spring文档,就是上面这段话所说的情况。另外一个类的方法调用A方法,跨越了两个类,这样才是。了Spring的动态代理。再来看官方文档的这句话,,称之为“A方法”,

Enable1234___的专栏 2452

Spring事务与AOP源码深度解析:从原理到实践,手写实现核心机制

在Java企业级开发中,Spring框架的事务管理和面向切面编程(AOP)是构建高内聚、低耦合应用的核心技术。事务管理通过声明式事务抽象,确保了复杂业务场景下的数据一致性,其底层依赖于动态代理和连接绑定等机制。AOP作为面向对象编程的有力补充,通过切面、通知和代理模式,将日志、安全等横切关注点与核心业务逻辑解耦,极大地提升了代码的可维护性。理解其原理,不仅能帮助开发者精准定位诸如事务传播行为不生效、AOP切面执行顺序错乱等常见问题,更能为应对高级技术面试和进行框架级定制开发奠定坚实基础。本文以Spring事

weixin_30698297的博客 358

Spring注解驱动之后再说事务啊

问题引入 Spring中事务传播有哪几种,分别是怎样的? 理解注解事务的自动配置? SpringBoot启动类为什么不需要加@EnableTransactionManagement注解? 声明式事务的实现原理?(待补充) 1 声明式事务 系统开发中必然与数据打交道,事务管理必不可少。Spring支持声明式事务,通过*@Transactional注解控制方法是否支持事务。声明式事务,基于AOP实现,将具体业务和业务逻辑解耦。 Spring提供了@ EnableTransactionManageme

aa119101的博客 378

Spring事务回滚机制深度解析:从AOP代理到异常处理的完整链路

在Java企业级应用开发中,事务管理是确保数据一致性的核心技术。其核心原理在于通过数据库事务的ACID特性,将一系列操作封装为原子性单元。Spring框架通过AOP(面向切面编程)技术,将事务管理这一横切关注点从业务逻辑中解耦,实现了声明式事务管理。具体而言,Spring利用动态代理在运行时为目标方法织入事务增强逻辑,由TransactionInterceptor作为核心拦截器,统一处理事务的创建、提交与回滚。这一机制的技术价值在于大幅简化了开发复杂度,提升了代码的可维护性。其典型应用场景包括金融转账、订单

weixin_30896825的博客 319

Spring面试源码级通关:IoC/AOP原理与AI新趋势实战

Spring是Java企业级开发的核心框架,其IoC容器和AOP代理机制构成了现代应用架构的基石。理解Bean生命周期、三级缓存设计、事务代理原理等,本质是掌握Spring如何通过反射、字节码增强与回调机制实现松耦合与横切关注点分离。这些能力不仅决定面试成败,更直接影响高并发、分布式及云原生场景下的系统稳定性与可扩展性。随着Spring Boot自动配置、Spring Security权限演进及Spring AI 2.0对RAG与MCP的原生支持,框架已从传统Web容器升级为AI就绪的声明式开发平台。本文聚

csdndownload11123的博客 445

Spring AOP与AspectJ:选型与实战指南

Spring AOP 核心要点解析 Spring AOP 提供了两种实现方式:轻量级的 Spring AOP 和功能完整的 AspectJ。选择时需考虑项目需求、工具支持和团队熟悉度。Spring AOP 仅支持方法拦截,适用于简单场景;AspectJ 则支持字段访问、构造方法等更丰富的切点类型。在实现风格上,@AspectJ 注解方式比 XML 配置更灵活,支持切点组合和代码模块化。Spring AOP 底层采用代理机制:JDK 动态代理用于接口实现类,CGLIB 代理用于非接口类。需注意代理限制(如无法

m0_37607945的博客 752

Windows 24H2下eNSP AR_40启动失败的终极修复指南

本文针对Windows 11 24H2更新后eNSP AR_40路由器启动失败的问题,提供了终极修复方案。核心在于解决系统强制虚拟化安全(VBS/Hyper-V)与老版模拟软件的兼容性冲突。指南详细介绍了安装关键系统更新KB5053656、精细调整Windows虚拟化功能以及后续排错步骤,帮助用户在24H2系统上稳定运行eNSP进行网络实验。

weixin_29233333的博客 507

Spring AOP环绕通知:从动态代理到实战避坑指南

面向切面编程(AOP)是一种通过预编译方式和运行期动态代理实现程序功能统一维护的技术,其核心原理是在不修改源代码的情况下,为程序动态添加额外行为。Spring AOP作为Java生态中最主流的AOP框架,通过动态代理机制实现方法拦截,为事务管理、日志记录、性能监控等横切关注点提供了优雅的解决方案。环绕通知(@Around)作为AOP中最强大的通知类型,赋予开发者对目标方法执行流程的完全控制权,能够实现参数修改、返回值处理、异常捕获等高级功能,在性能监控、声明式重试、缓存防击穿等场景中具有不可替代的价值。理解

weixin_33919950的博客 346

从Chimera到ChimeraX:科研绘图升级指南,让你的论文配图更专业

本文详细介绍了从UCSF Chimera升级到ChimeraX的科研绘图指南,帮助研究者提升论文配图质量。ChimeraX在渲染引擎、大分子处理能力和动画功能上具有显著优势,特别适合生命科学和结构生物学领域。文章涵盖材质设置、专业配色、工作流程和期刊适配等核心技巧,助力产出出版级分子可视化图像。

weixin_33670786的博客 933

群晖NAS实战:从零部署Container Manager与Docker代理配置全攻略

本文详细介绍了在群晖NAS上从零部署Container Manager与配置Docker代理的完整流程。针对国内用户常见的镜像拉取问题,提供了图形化界面与SSH命令行两种解决方案,并分享了MySQL容器部署的实战案例。文章特别强调了网络配置技巧和日常维护方法,帮助用户高效管理NAS上的容器应用。

weixin_29254673的博客 1303

Spring AOP 失效的真正元凶

Spring AOP 失效的真正元凶 Understanding AOP proxies Spring AOP is proxy-based. It is vitally important that you grasp the semantics of what that last statement actually means before you write your

huangshulang1234的博客 4918

别再被systemd-resolved坑了!银河麒麟V10下用Bind9搭建内网DNS的完整避坑指南

本文详细介绍了在银河麒麟V10和Ubuntu系统下部署Bind9 DNS服务器时,如何解决与systemd-resolved的端口冲突问题。提供了五种专业级解决方案,包括彻底禁用systemd-resolved、智能端口转发等,并特别针对银河麒麟V10的安全加固需求给出了配置建议和诊断工具,帮助用户高效搭建内网DNS服务。

weixin_30342209的博客 655

@Transactional self-invocation does not lead to an actual transaction at runtime解决办法

接着,methodA()内部调用methodB(),这时候的调用是直接调用this.methodB(),也就是目标对象自己的方法,而不是代理对象的方法,因此@Transactional的拦截器不会被触发,导致methodB()不会在事务中执行。总结一下,原话的意思就是说,在同一个类中的方法A调用带有@Transactional的方法B时,由于是自调用,没有经过Spring的代理,所以方法B上的事务注解不会生效,不会开启事务。所以,想要让自调用也应用事务,可能需要通过代理来调用方法,而不是直接调用。

大户影视的博客 1370

Rijndael密码算法

一 什么是Rijndael Rijndael是由比利时密码学家设计的分组密码算法,于2000年被选为新一代的标准密码算法——AES。今后会有越来越多的密码软件支持这种算法。 Rijndael的分组长度和密钥长度可以分别以32比特为单位在128比特和256比特的范围内进行选择。不过在AES的规格中,分组长度固定为128比特,密钥长度只有128、192和256比特三种。 二 Rijndael的加...

实践求真知 1万+

习题加餐4. 冒险者工会

这个问题通过贪心算法解决,展示了贪心算法在解决一些优化问题中的强大能力。关键在于如何识别贪心选择的性质,并通过数学证明确保这种方法能够达到全局最优解。通过对问题的深入理解和逻辑推理,我们可以设计出有效的算法来解决问题,同时也需要注意代码实现中的细节,如排序和双指针的使用,以确保算法的效率和正确性。贪心算法的应用这个问题展示了贪心算法在实际问题中的应用。贪心算法通过在每一步选择当前最优的解决方案,期望最终达到全局最优解。该问题中,每次选择能力最接近但足以完成最难任务的冒险者,就是贪心选择的体现。

tang7mj的博客 1349

Spring事务与AOP核心原理深度解析:从源码到实战手写框架

在Java企业级开发中,Spring框架的事务管理与面向切面编程(AOP)是构建高可靠、可维护应用的核心技术。事务管理通过AOP机制,以非侵入方式为方法调用提供声明式事务控制,其底层依赖于动态代理技术实现逻辑织入。理解其原理对于解决实际开发中的事务失效、性能优化等问题至关重要。Spring AOP默认采用CGLIB代理,它通过生成目标类的子类来拦截方法调用,这解释了为何同类内部调用会导致@Transactional注解失效。通过手动实现一个简化版的AOP框架,可以直观体会代理创建、拦截器链与增强逻辑编织的过

weixin_33725807的博客 416

YOLOv13全网首发:CVPR2026 MixerCSeg | DEGConv方向引导边缘门控,破解细长裂缝检测难题

DEGConv通过方向先验嵌入、边缘增强卷积和动态门控三大机制,针对性解决了YOLO在处理裂缝类细长、多方向、低对比度目标时的特征提取不足问题,显著提升了裂缝检测的精度与鲁棒性。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 441

Spring

Spring AOP通过动态代理技术实现横切逻辑的统一管理,核心概念包括切面(Aspect)、连接点(Join Point)、切入点(Pointcut)和通知(Advice)。当AOP失效时,通常由于直接调用目标对象而非代理对象,常见场景包括同类内部调用、private/static/final方法或非Spring管理的对象。 **IOC(控制反转)**将对象创建与依赖管理交给Spring容器,通过依赖注入(DI)实现解耦,推荐使用构造器注入。其核心是BeanFactory和ApplicationConte

qq_41893505的博客 1079

Spring AI 项目实战(十):Spring Boot + AI + DeepSeek 构建智能合同分析技术实践(附完整源码)

摘要:基于Spring AI的智能合同审核系统开发 本文介绍了基于Spring Boot 3.2.0和Spring AI 1.0.0-M4构建的智能合同审核系统。系统采用分层架构设计,整合DeepSeek API实现文档解析、条款提取和风险分析功能。核心技术栈包括Spring AI、H2数据库、Apache POI/PDFBox等文档处理工具,实现了合同条款语义分析、异常识别和审核报告自动生成。项目解决了传统人工审核效率低、主观性强等问题,为合同管理提供了智能化解决方案。文章详细阐述了系统架构设计、技术选型

博客 654

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Spring Boot 中使用 @Transactional 注解配置事务管理

下面分别介绍一下的几个属性。

2509_94010562的博客 659

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

@Cacheable加缓存导致的跳过校验 & self自调用

上面的代码在getData方法前面加了@Cacheable注解,实现了加缓存的操作;不过仔细梳理逻辑就会发现:当一次调用该方法请求数据;cache中没有数据,于是调用getData方法获取数据,并且将查到的数据存在缓存当中;当第二次查数据的时候,会直接从cache里面找数据,此时不会调用getData方法;因此也就直接跳过了校验。导致后面查找数据时不需要校验就可以直接从缓存里面获取数据。

m0_47498690的博客 1187

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

@Async 异步任务的五个坑

本文深入剖析了Spring中@Async异步任务注解的五个常见陷阱,通过可运行的demo和真实日志展示了问题现象与解决方案。

Leighteen 246

在 Spring 框架中,**Transactions(事务)、Caching(缓存)和 AOP(面向切面编程)** 都依赖于 **代理机制(Proxy)

1. **代理类型** Spring 默认使用 **JDK 动态代理**(基于接口);当目标类没有实现接口时,自动回退到 **CGLIB 代理**(生成子类)。可通过 `@EnableAspectJAutoProxy(proxyTargetClass = true)` 强制启用 CGLIB。

[Blog][Domain] programb.blog.youkuaiyun.com 394

为什么事务注解自调用会失效?一文搞懂 AOP 代理原理与解决方案

摘要:本文探讨了Spring事务注解失效的根本原因及其解决方案。通过分析发现,事务失效是由于AOP代理机制导致的内部调用绕过代理对象所致。Spring AOP基于JDK动态代理或CGLIB动态代理实现,代理对象只拦截外部调用,同类内部方法相互调用时无法通过代理增强。文章提出了四种解决方案:自我注入、从ApplicationContext获取代理对象、使用AopContext获取当前代理对象,以及重构代码结构。理解这些机制不仅能解决事务问题,更有助于合理运用AOP特性进行系统设计。

2401_83334206的博客 957
上一篇: conversion-service
下一篇: JQuery操作表单总结(不断总结中……)
backzero
博客等级 码龄15年 3粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值