Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code architecture of service interface and service impl classes spring

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

like image 860
viper Avatar asked May 31 '16 08:05

viper


People also ask

What is service impl in Spring boot?

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.

What is service impl?

Service implementations define how complex services are implemented, by orchestrating calls to other automated tasks and services.

What is service implementation in Spring?

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.

What is the use of service interface in Spring boot?

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.


2 Answers

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

like image 88
Jesper Avatar answered Oct 03 '22 04:10

Jesper


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

like image 29
LNT Avatar answered Oct 03 '22 03:10

LNT