Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put business logic in spring mvc framework?

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 :)

like image 290
Mark Vincent Osea Avatar asked Aug 18 '14 01:08

Mark Vincent Osea


People also ask

Where do you write business logic in spring boot?

The business logic is performed in the Service layer. The spring boot performs all the logic over the data of the database which is mapped to the spring boot model class through Java Persistence Library(JPA). The JSP page is returned as Response from the controller.

Where do you put your business logic typically?

Business logic should live in the data model. And, what's more, it should live in the graph data model because that's the right abstraction for the next twenty years. If you've been paying attention to this blog or to Stardog generally, then you must have known this is where we were going to end up.

Where should business logic reside Java?

The very definition of the terms "front end" and "back end" comes from the separation of business logic (back end) from the user interface (front end). So yes, business logic should be in the back-end, whether that is a remote service or simply a different layer in the same application.

Which layer contains the business logic and the business data?

The business logic layer is the business components that provide OAGIS services to return data or start business processes. The presentation layer uses these OAGIS services to display data, or to invoke a business process. The business logic provides data required by the presentation layer.


3 Answers

@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.

like image 193
Luiggi Mendoza Avatar answered Oct 16 '22 20:10

Luiggi Mendoza


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.

like image 45
Gigi Avatar answered Oct 16 '22 21:10

Gigi


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.

like image 1
Neil McGuigan Avatar answered Oct 16 '22 21:10

Neil McGuigan