gpt4 book ai didi

java - 我应该把@Transactional注释: at an interface definition or at an implementing class?放在哪里

转载 作者:IT老高 更新时间:2023-10-28 11:44:44 25 4
gpt4 key购买 nike

代码中标题的问题:

@Transactional (readonly = true)
public interface FooService {
void doSmth ();
}


public class FooServiceImpl implements FooService {
...
}

public interface FooService {
void doSmth ();
}

@Transactional (readonly = true)
public class FooServiceImpl implements FooService {
...
}

最佳答案

来自 http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html

The Spring team's recommendation is that you only annotate 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 will only work as you would expect it to if you are using interface-based proxies. The fact that annotations are not inherited means that if you are using class-based proxies then the transaction settings will not be recognised by the class-based proxying infrastructure and the object will not be wrapped in a transactional proxy (which would be decidedly bad). So please do take the Spring team's advice and only annotate concrete classes (and the methods of concrete classes) with the @Transactional annotation.

Note: Since this mechanism is based on proxies, only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

(在第一句中添加了重点,原文中的其他重点。)

关于java - 我应该把@Transactional注释: at an interface definition or at an implementing class?放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3120143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com