34

I don't know where to put the business logic in spring mvc because I'm new to it. I have a clue on what to do but because of lack in knowledge in spring mvc, I don't know where to start. I would also like to ask if somebody knows where I can get a good tutorial on this or a complete sample of a spring mvc web application that has a business logic on it? Anyways, the business logic that I was talking about is all about database handling :)

Amir169
  • 45
  • 1
  • 9
Mark Vincent Osea
  • 679
  • 2
  • 9
  • 17

3 Answers3

88

@Controller classes serve as C from MVC. Note that the real controller in Spring MVC is DispatcherServlet that will use the specific @Controller class to handle the URL request.

@Service classes should serve for your service layer. Here you should put your business logic.

@Repository classes should serve for your data access layer. Here you should put CRUD logic: insert, update, delete, select.

@Service, @Repository and your entity classes will be M from MVC. JSP and other view technologies(e.g. JSP, Thymeleaf etc.) will conform V from MVC.

@Controller classes should only have access to @Service classes through interfaces. Similar, @Service classes should only have access to other @Service classes and for a specific set of @Repository classes through interfaces.

Sadeq Dousti
  • 3,346
  • 6
  • 35
  • 53
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 6
    Do you (or anybody) have links to the documentation or useful links for that answer? – Adrien Brunelat Jul 19 '16 at 14:46
  • 4
    I often find myself needing to use Services from within other Services. This can easily end up looking quite messy. Is there any preferred way in Spring to arrange this? Such as having a certain typ of Service(Service-that-can-access-services) one layer above the rest of the service. – why_vincent Dec 05 '16 at 17:38
  • 1
    @why_vincent it isn't. But that design is the preferred. I think the design should follow GRASP and SOLID, focus on that more than if a `@Service` calls many `@Service`s or note. – Luiggi Mendoza Dec 05 '16 at 17:51
  • @Luiggi Mendoza thanks for the answer. Why do you have access to Service in Controller only through interfaces and not interface implementations? – Ana Sustic Sep 20 '21 at 11:49
  • 1
    @AnaSustic it focuses on loose coupling. As an example, imagine you find that a specific operation is slow and is due how the service is implemented. You could create a new implementation that complies with the current interface, and then execute the application twice, one with the current service interface implementation and another with the new one. Then you can get performance results (throughput or user load, depends what you're measuring) and choose the best without affecting the rest of the components in your app. – Luiggi Mendoza Sep 20 '21 at 12:08
17

Many people would recommend to add the business logic to the service layer. I personally find out that's not a great idea, specially when you start testing: you may have to deal either with the persistence and business logic at the same time, or mocking everything around, and then things can get very messy.

I do recommend reading this article before taking any conclusions: The Biggest Flaw of Spring Web Applications

Resuming, the idea would be to move the business logic to the model layer and simplify your services methods.

Gigi
  • 353
  • 3
  • 10
1

Generally, your business logic goes in the service layer. Though you can put basic validation rules in your pojos with JSR annotations.

For a Spring MVC app you have controllers, which handle http requests, and a domain layer, which are pojos representing your business models. You often have a persistence layer, or DAO. You might have a service layer as well, for helping with non-trivial logic.

Your comment about database handling doesn't make sense. Business rules are orthogonal to storing data. Your database handling should go in your persistence layer.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • I dont know if database handling is the correct phrase for it but I have a task that i must fill up the data of other tables in a database. – Mark Vincent Osea Aug 18 '14 at 02:39