'Spring boot - @Service class calling another @Service class

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.



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

@Service
public class MyService {
// ...  
@Autowired
private MyOtherService myOtherService = new MyOtherService();
// ...}

Try this. It is worked for me. You can use the methods of a service in many sevice.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emre Savcı
Solution 2 Rohit Kumar
Solution 3 procrastinator