在春季启动应用程序中在哪里指定@transactional注释?

发布于 2025-02-09 03:55:54 字数 655 浏览 2 评论 0原文

在最近的Spring Boot应用程序(v。2.7.0)的上下文中,有许多选项,用于应用@transactional注释的

控制器或服务?

几乎普遍看来,建议使用服务,但并不完全清楚为什么。

实现类还是接口?

假设我们选择将注释应用于我们的服务,而这些服务实现接口,我们是否应该注释服务接口或实现类?

,建议注释课堂

Spring建议您仅注释带有 @Transactional注释的具体类(以及混凝土类的方法),而不是注释接口。当然,您可以将@transactional注释放在接口(或接口方法)上,但是如果您使用基于接口的代理,则只能按照期望的方式工作。 Java注释不是从接口继承的事实,这意味着,如果您使用基于类的代理(代理 - target-class =“ true”)或基于编织的方面(mode =“ factexj”),则交易设置为代理和编织基础架构未认可,并且该对象不会包裹在交易代理中,这绝对是不好的。

但是,此建议并未出现在文档的5.2.x版本中,所以我想知道它是否仍然适用?基于我的测试,似乎将@transactional注释到接口上确实可以在带有默认代理行为的Spring Boot应用程序中使用。

In the context of a recent Spring Boot app (v. 2.7.0), there are a number of options for where the @Transactional annotation is applied

Controller or Service?

Almost universally it seems that service is recommended, but it's not entirely clear why.

Implementation class or interface?

Assuming we choose to apply the annotation to our services and those services implement an interface, should we annotate the service interface or the implementation class?

In older versions of the Spring docs, it was recommended to annotate the class

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.

But this advice does not appear in the 5.2.X version of the docs, so I'm wondering if it still applies? Based on my testing it seems that applying the @Transactional annotation to interfaces does work in a Spring Boot app with the default proxying behaviour.

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

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

发布评论

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

评论(1

梦旅人picnic 2025-02-16 03:55:54

@transactional最常在服务级别使用,尽管没有什么可以阻止您进行控制器方法。这实际上取决于您如何构建应用程序。

恕我直言,使您的服务层交易是一种更好的方法。通常,在此级别上,您可能会调用其他持久方法(DAOS,存储库等)。可以在多个表上进行读取/写入,因此它使服务层成为定义交易边界的好地方。

@Transactional is most commonly used at service level, though there is nothing stopping you from making your controller methods transactional. It really depends on how you structure your application.

IMHO making your service layer transactional is a better approach. It is typically at this level that you might be calling other persistence methods (DAOs, repositories, etc). There could be reads/writes across multiple tables, so it makes the service layer a good place to define transaction boundaries.

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