1

Since I found a problem in my project, looking for information, I noticed that people use the @Service annotation in different positions:
some on the service interface,
some on the ServiceImpl class.

The same with regards to the @Transactional annotation:
some used it over the methods implemented in the ServiceImpl class,
others over the methods in the Service interface,
others over the RestController methods,
still others in the Repository.

In short, rather than clearing my ideas, they confused me.
Can anyone explain the pros and cons of those choices and what is the best place to put them?

Paul Marcelin Bejan
  • 990
  • 2
  • 7
  • 14

1 Answers1

1

The @Service annotation helps Springboot to inject some code necessary for common services. If you have an interface with many implementations it could go there, but if you have no interface and a single implementation then it can go on the class itself. It also makes sure the class is injectable into other beans. See here for more details.

The @Transactional annotation is to be put at the method where the atomicity is required. The method cannot be private though, see here

fpezzini
  • 774
  • 1
  • 8
  • 17