Answer the question
In order to leave comments, you need to log in
Spring. What is the service layer for?
In all guides, they give the following project structure:
In the same guides, looking at the code, you can only see that the service layer completely duplicates DAO. What is this layer for? How to use it correctly?
Answer the question
In order to leave comments, you need to log in
The service layer contains business logic, for the implementation of which it refers to different DAOs
These are general words and terminology of a person who understands thoroughly or almost thoroughly the structure and principles of interaction inside the Spring container. Yes, and the logic is very often simple in services - to get data and sometimes transform it to return the control. It is clear that there are also complex services, but in the vast majority it is just getting data from the database according to criteria.
Using this framework for more than one year, I also often sinned by throwing out the service layer, delegating its functionality to the DAO layer (only as one guru from one resource correctly noted, in Spring's terminology, it is correct to call this layer not DAO, but the repository - Repository). It was simple and convenient, especially since for small projects, in fact, the code in the Tao and the service is repeated. And the differences are that @ Repository - our DAO - is a bean-singleton, and when it is accessed by several threads, which is a normal thing for MVC, access to the same data from different threads can occur. The visibility of @ Service is determined by the visibility of the calling @ Controller. Perhaps, with the definition and terminology, I have some tensions, and fine-tuning of the components can certainly be done, but the general principle and purpose have been transferred.
But I've never understood the need for DAO. In fact, this is just a wrapper over the EntityManager. With the advent of Spring Data, this has become even more obvious. But I could be wrong, I don't have much experience in this area.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question