I found out that in an MVC pattern, there are mainly 4 classes; the controller, the service, the service impl and repo.
Service is an interface and service impl implements service class and contains all the logical codes. The structure would be something like :-
Service
interface
Service{ public void someMethod(); }
ServiceImpl
class
ServiceImpl implements Service{ public void someMethod(){ //do something } }
But when we want to access the service impl codes from controller, we call the method of service class as :-
@Autowired Service service; Object obj = service.someMethod();
How does the controller execute code of ServiceImpl
class
Service Components are the class file which contains @Service annotation. These class files are used to write business logic in a different layer, separated from @RestController class file.
Service implementations define how complex services are implemented, by orchestrating calls to other automated tasks and services.
The service implementation should be a Spring bean (it either has to have a @Component or @Service annotation, or should be defined in a Spring XML configuration file), so that Spring will find it and register it in the Spring application context.
There are various reasons why Service layer interfaces are created. The first and most important reason is testability. You can create mocks of service interface and test your code easily, if you cannot create mocks using a mocking library then you can create test stubs. Thanks.
This is basically how Spring works:
The service implementation should be a Spring bean (it either has to have a @Component
or @Service
annotation, or should be defined in a Spring XML configuration file), so that Spring will find it and register it in the Spring application context.
Then you use dependency injection, through the @Autowired
annotation, to inject the implementation of the service into the controller. This means that Spring will look at your controller, it will find the @Autowired
annotation on the service
member variable and initialize it with a bean that it finds in the application context, which will be the instance of the service implementation class that it has registered earlier. So, after Spring is done, service
will refer to the instance of ServiceImpl
.
See the Spring Framework reference documentation for information on how dependency injection works with Spring: The IoC container
Basic idea behind having this kind of architecture is little different than just spring convention.
Lets say tomorrow you decide, you dont want to have single application for both projects, and go into one deployment for webapp and another for service Example UserService WebApp
so for WebApp to connect to UserService it will have to make http requests to get any kind of data. then you will have to change all your WebApp code to make it compatible to new changes. For example instead directly calling method of Service you will call httpClient. To avoid this rework what you can do is, Using interface Service you implement your own ServiceImpl and make all http request in there, rest remains intact.
Similar stuff will be done in UserService, it will have its own ServiceImpl as before but will be called in Controller as a singleton object.
Your answer : You can refer to ServiceImpl directly, it will serve the purpose, difference is only when ServiceImpl is not part of current module or any dependency, but final bundled project will have its implementation through some sibling module probably
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