java @Transactional 放在哪里?在接口规范或实现中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5551541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to put @Transactional? In interface specification or implementation?
提问by John Manak
What is considered the best practice in placing the @Transactional
annotation? Should I annotate the interface method or the implementation?
放置@Transactional
注释的最佳做法是什么?我应该注释接口方法还是实现?
采纳答案by Bozho
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.
El Guapo 指出,除此之外,在界面上可能会出现更多与代理策略相关的问题。
回答by El Guapo
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.
在我看来,这真的完全取决于您的应用程序架构。这取决于您如何代理您的类。如果您的应用程序设置为proxy-target-class='true'
(在您的应用程序上下文中,那么@Transactional
如果您注释 Interface.
Check out The Spring Docs -- "Tips"for more information.
查看Spring Docs——“提示”以获取更多信息。
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 注释不是从接口继承的事实意味着如果您使用基于类的代理 (proxy-target-class="true") 或基于编织的方面 (mode="aspectj"),那么事务设置是不被代理和编织基础设施识别,并且对象不会被包装在事务代理中,这绝对是糟糕的。
回答by oiavorskyi
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.
虽然事务管理在很多情况下是实现细节,但它通常也是一个接口细节。例如,在定义应用程序的服务接口时,您可能会考虑放入@Transactional
接口定义中以明确说明您使用的传播策略。
回答by Luiz Feij?o Veronesi
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.
我不在我的系统上使用接口,因为到目前为止我真的不知道是否可以在它上面实现任何东西。所以我在实现上添加了注释,我相信 Spring 会让一切对我来说都是正确的。
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?
我不认为所有的类都必须有接口。我看到很多架构都有很多模式,他们都喜欢接口。但是有一个问题:如果您将 Spring 注释放入接口中,并且出于某种原因,您想要另一种方法来通过此接口完成实现类的事务,那么您就不能那样做。还是我错了?
Cheers.
干杯。