Where should I put @Transactional annotation: at an interface definition or at an implementing class?

后端 未结 5 1408
花落未央
花落未央 2020-11-29 17:53

The question from the title in code:

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


public class FooServiceImpl implem         


        
5条回答
  •  天涯浪人
    2020-11-29 18:20

    Supporting @Transactional on the concrete classes:

    I prefer to architect a solution in 3 sections generally: an API, an Implementation and a Web (if needed). I try my best to keep the API as light/simple/POJO as possible by minimizing dependencies. It's especially important if you play it in a distributed/integrated environment where you have to share the APIs a lot.

    Putting @Transactional requires Spring libraries in the API section, which IMHO is not effective. So I prefer to add it in the Implementation where the transaction is running.

提交回复
热议问题