当前位置:网站首页>@Transactional注解在类上还是接口上使用,哪种方式更好?
@Transactional注解在类上还是接口上使用,哪种方式更好?
2022-08-01 23:41:00 【drhrht】
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。
Spring @Transactional想必大家都很熟悉,那它是在类上或实现类的方法上和在接口上或接口方法上哪种使用方式是更好的选择呢?
@Transactional介绍
@Transactional 是Spring提供的声明式事务管理,使用起来简单、方便,又能和业务逻辑解耦。
Spring的声明式事务管理在底层是建立在 AOP 的基础之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。
Spring的动态代理
Spring AOP是通过动态代理实现的。如果代理对象实现了接口,默认使用JDK的动态代理,反之没有实现接口则使用cglib的动态代理。
言归正传
回到问题上,在正确使用@Transactional注解时,不管@Transactional注解是在类上或实现类的方法上还是在接口上或接口方法上,它的事务功能都是可以实现的,只是选择那种方式更优雅一点而已。
Spring官方文档是这样写的:
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.
翻译过来大概意思是 Spring官方建议在具体的类或类的方法上使用@Transactional注解,而不是在接口或接口方法上使用。@Transactional也可以在接口上使用,但是这仅限于你使用的是基于接口的动态代理。在java中注解是不会被继承的,如果使用的是基于类的动态代理或者使用aspectj,@Transactional注解的作用就失效了。
总结
Spring 官方建议还是在具体的类上或类的方法上。我本人赞同Spring官方的观点,事务也是一个实现细节,它应该在具体的类上或方法上,到底具体的实现是否是需要事务要看具体的实现细节。
当然,不是说一定要选择将@Transactional使用在具体类或者方法上一定是最好的,如果项目架构更适合用接口的动态代理,将@Transactional注解应用到接口或者接口方法上也是可以的。一定要选择一个最合适自己的,如果没有找到最合适自己的,那就选择一个大多数人都认为好的。
能力一般,水平有限,如有错误,请多指出。如果对你有用点个关注给个赞呗
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 怎样做才能让这条SQL变成一条危险的SQL?
- 仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)
- cdh6打开oozieWeb页面,Oozie web console is disabled.
- Spark Sql之join on and和where
- 深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
- 工作5年,测试用例都设计不好?来看看大厂的用例设计总结
- Calculate the angle of a line defined by two points
- 递归:方法调用自身
- ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
- 对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
猜你喜欢

E - Integer Sequence Fair
![[C language advanced] file operation (2)](/img/4d/49d9603aeed16f1600d69179477eb3.png)
[C language advanced] file operation (2)

经典文献阅读之--DLO

技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu

数据机构---第五章树与二叉树---二叉树的概念---应用题

C语言——分支语句和循环语句

DRF generating serialization class code

nodejs--process

Secondary Vocational Network Security Competition B7 Competition Deployment Process
随机推荐
Avoid hidden text when loading fonts
中职网络安全竞赛B7比赛部署流程
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
【参营经历贴】2022网安夏令营
Getting started with IDEA is enough to read this article
程序员还差对象?new一个就行了
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
路径压缩、、
How do programmers solve online problems gracefully?
Chapter 12 End-User Task As Shell Scripts
Spark Sql之union
Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
UML diagram of soft skills
最短路模板
颜色透明参数
drf生成序列化类代码
D - Linear Probing- 并查集
问题解决方式了
PostgreSQL 基础--常用命令
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环