Why to use Service Layer in Spring MVC

Alam Khan
4 min readSep 25, 2018

--

There has been ambiguity about the use of Service layer in Spring ,also it is very difficult to find information on internet about the usage of service-layer.

In this blog we will learn the usage of Service layer.As usual we will walk through a small example to understand the concept.

Anybody who has started learning Spring MVC will be aware how model,controller,View interact with each other as part of a Spring MVC Application.

For someone who has fundamental knowledge of MVC framework must be knowing that Controller interacts with DAO layer to persist data to the Database.

Generally the DAO layer should be as light as possible and should exist solely to provide a connection to the DB, sometimes abstracted so different DB backends can be used together.

The service layer is there to provide logic to operate on the data sent to and from the DAO and the client. Very often these 2 pieces will be bundled together into the same module, and occasionally into the same code, but you’ll still see them as distinct logical entities.

Reasons to use :

1.Provides separation of concern-

Service layer provides code modularity,the business logic and rules are specified in the service layer which in turn calls DAO layer ,the DAO layer is then only responsible for interacting with DB.

2.Provides Security -

If you provide a service layer that has no relation to the DB, then it is more difficult to gain access to the DB from the client except through the service. If the DB cannot be accessed directly from the client (and there is no trivial DAO module acting as the service) then an attacker who has taken over the client cannot have access to your data directly.

3.Provide Loose Coupling-

Service layer can also be used to serve loose coupling in the application.Suppose your controller has 50 methods and in turn it calls 20 Dao methods,Now at later point you decide to change the Dao methods serving these controllers.You need to change all the 50 methods in controller. Instead if you have 20 service methods calling those 20 Dao methods, you need to make change in only 20 Service methods to point to a new Dao.

Flow of an application using Service layer

Let’s take an implementation case how Service layer can be used in an application.

Consider a customer management system where you can perform basic adding ,updating ,deleting,listing of customer .

Flow of Demo App without Service Layer

So if you see in the diagram above ,Browser sends the request to Controller,then it passes the control to DAO layer to access data from Database,the data received is then used for rendering View on the browser.

This pattern involves lot of risk as we are exposing our DB connection to the controller class,also if we want to do some business processing then we have to write all the code in Controller class which is not a good practice. So instead it is preferred to use Service layer in between the Controller and DAO layer.Service layer will have some business logic for our customers and in turn it will call DAO class to interact with the Database.

Flow of Demo App With Service Layer.

In context of our Demo App ,We have a Customer controller class which will get the request from the browser,according to the request appropriate method of controller will be called and processing will be done.

Customer Controller

Now we will call the Service “CustomerServiceImpl” ,it has Customerdao object which is autowired automatically as the class is loaded.Here @Service annotation is used over CustomerService class to mark the class as a service provider. We have annotated it with @Service annotation so that spring context can autodetect it and we can get its instance from the context.

Service class
Customer Dao class

As the flow reaches here a transactional method will be invoked and it will perform its task.Suppose getCustomer() is called then it will provide all the customers from the Database. Notice how CustomerDAO object is injected into Service class and CustomerServiceImpl object is injected into Controller class and controller uses the Service object to access Dao layer .Finally CustomerDaoImpl uses the instance of sessionfactory bean to create a session and persist data into Database.

So in above demo i have tried explain how to use Service layer in an application.Feel free to share other use cases as well as why to use it ? in an application.

❤️ Like, Share or Leave A Comment!

If you enjoyed this post, don’t forget to give it a 👏🏼, share it with a friend you think might benefit from it and leave a comment!. Any feedback is greatly appreciated.

Say hi to me on Linkedin

Thanks !!

--

--