@Transactional 应该放在哪里?在接口规范或实现中?

发布于 2024-10-30 14:22:38 字数 50 浏览 1 评论 0原文

放置 @Transactional 注释的最佳实践是什么?我应该注释接口方法还是实现?

What is considered the best practice in placing the @Transactional annotation? Should I annotate the interface method or the implementation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风铃鹿 2024-11-06 14:22:38

在我看来,这实际上完全取决于您的应用程序架构。这取决于您如何代理您的类。如果您的应用程序设置为 proxy-target-class='true'(在您的应用程序上下文中),那么当您注释接口时,您的 @Transactional 信息将不会被获取 ”

查看 Spring 文档 -- 提示” 了解更多信息。

Spring 建议您仅使用 @Transactional 注释来注释具体类(以及具体类的方法),而不是注释接口。您当然可以将 @Transactional 注释放在接口(或接口方法)上,但这仅在您使用基于接口的代理时按照您期望的方式工作。 Java 注释不是从接口继承的事实意味着,如果您使用基于类的代理 (proxy-target-class="true") 或基于编织的切面 (mode="aspectj"),则事务设置为代理和编织基础设施无法识别,并且该对象不会被包装在事务代理中,这绝对是糟糕的。

It really all depends on your application architecture, in my opinion. It depends on how you are proxying your classes. If you have your app set to proxy-target-class='true' (in your application context, then your @Transactional information wont be picked up if you annotate the Interface.

Check out The Spring Docs -- "Tips" for more information.

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.

情深已缘浅 2024-11-06 14:22:38

好问题。我一直把它放在实施中。也许是因为它是一个实现细节,而不是一个抽象。

您可能希望不同的实现具有不同的事务行为。

El Guapo 指出,除此之外,在界面上安装可能会出现更多与代理策略相关的问题。

Good question. I've always put it in the implementation. Perhaps because it is an implementation detail, rather than an abstraction.

You may want different implementations to have different transactional behaviours.

El Guapo noted that, in addition to that, there are more issues that can arise from putting on on the interface, related to the proxying strategy.

╰◇生如夏花灿烂 2024-11-06 14:22:38

虽然事务管理在许多情况下是实现细节,但通常它也是接口细节。例如,在定义应用程序的服务接口时,您可能会考虑将 @Transactional 放入接口定义中,以明确说明您正在使用的传播策略。

While transactions management is implementation detail in many cases quite often it's an interface detail as well. For example, when defining interface of services of your application you might consider putting @Transactional into interface definition to specifically clarify what propagation strategy you're using.

黯然#的苍凉 2024-11-06 14:22:38

我没有在我的系统上使用接口,因为到目前为止我还没有真正看到是否可以在它上面实现任何东西。所以我在实现上添加了注释,我相信 Spring 会让一切对我来说都是正确的。

我不认为所有类都必须有接口。我看到很多有很多模式的架构,他们都喜欢界面。但有一个问题:如果您将 Spring 注释放入接口中,并且出于某种原因,您希望通过该接口使用另一种方法来完成实现类的事务,则您无法这样做。还是我错了?

干杯。

I don't use interfaces on my system because so far I don't really see if it will be possible to implement anything over it. So I put annotations on the implementation and I believe Spring would make everything correct to me.

I don't think that all classes must have interfaces. I see around lots of architectures with lots of patterns and they all love interfaces. But a question: if you put the Spring annotation into the interface and you, for some reason, you want another approach to the transaction of an implementation class done over this interface, you couldn't do that. Or am I wrong?

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文