Is it fine to have a @Service
annotated class calling another @Service
annotated class? Or it is a bad practice?
Eg.:
@Service
public class MyService {
// ...
@Autowired
private MyOtherService myOtherService;
// ...
}
EDIT - after 3 years
It's ok to have dependencies from one service to another. This is mainly because of reusability. If circular dependencies happen between two services from the same module, that's ok.
You should only avoid circular dependencies between modules.
Spring boot supports calling one rest service to another rest service using the RestTemplate class. RestTemplate is a synchronised client side class that is responsible for calling another rest service. RestTemplate supports all HTTP methods such as GET, POST, DELET, PUT, HEAD, etc.
When calling a method, a service method may call another service call and that may also call another service method which can lead to circular call again.
tl;dr: Use @Component or @Service on both, and there's no significant difference between them ( @Component is becoming more usual because of that).
It is not any restriction calling a service from another one. Unless you make circular dependency between services.
Circular dependency : https://en.wikipedia.org/wiki/Circular_dependency
Circular dependency in spring : https://www.baeldung.com/circular-dependencies-in-spring
Its good practice since utility class are being ignored these days, approach getting motivated by horizontal scaling... Surely services got to interact with other.
No need to worry, its like one service manager needs services of another manager.
just only one should be dependent on other, not both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With