当前位置:网站首页>@Transactional注解在类上还是接口上使用,哪种方式更好?
@Transactional注解在类上还是接口上使用,哪种方式更好?
2022-07-30 20:15:00 【葬心啊】
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。
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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
随机推荐
[PM only] Quickly count who else in the team has not registered and reported information, and quickly screen out the members of their own project team who have not completed the list of XXX work items
Recommendation system: evaluation index [offline evaluation index: RMSE (root mean square error), AUC, precision, recall, F1] [online evaluation: A/B test] [generally required response time <0.5s]
倾斜文档扫描与字符识别(opencv,坐标变换分析)
WPS没有在任务栏显示所有窗口选项怎么回事?
Network layer protocol------IP protocol
KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']
Database indexes: indexes are not a panacea
【考研词汇训练营】Day18 —— amount,max,consider,account,actual,eliminate,letter,significant,embarrass,collapse
推荐系统:冷启动问题【用户冷启动、物品冷启动、系统冷启动】
7.联合索引(最左前缀原则)
MySQL kills 10 questions, how many questions can you stick to?
推荐系统:评估指标【离线评估指标:RMSE(均方根误差)、AUC、准确率、召回率、F1】【在线评估:A/B测试】【一般要求响应时间<0.5s】
360杜跃进:太空安全风险加剧,需打造一体化防御体系
excel数字下拉递增怎么设置?
MySQL database master-slave configuration
历史上的今天:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
Redisson 的分布式锁找不到?
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
WPS表格怎么自动1234排下去?wps表格怎么自动生成序号?
推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】