What is DAO and Service layer exactly in Spring framework?

DAO - data access object, are object to handle connection to your data storage (typicaly database). You have here your queries and DAO provides data to your services.

Services should contain all your logic. If you have logic separete you can theoretically change your UI layer or DAO layer without you affected it.


DAO(Data Access Object) is a design pattern, which consists on creating for each table on your database a class,it provides a technique for separating object persistence and data access logic


There is no distinction as far as Spring is concerned. By convention you can mark DAO classes with @Repository and services with @Service. Also the former does some persistence layer exception translation.

Since you are asking theoretically: DAO should perform raw database operations and translate them to some higher level constructs (objects, collections). Services should call DAOs and perform business operations. Typically transactions demarcation is performed on service layer to span several DAO calls.

Finally DAO should abstract business logic from persistence details, ideally allowing to switch persistence layer without business logic (services) changes. This is hardly ever possible due to leaking abstraction of persistence providers (e.g. lazy loading).


It gives decoupling benefits. When source of data changes the way you process data in Service for all service users (mobile client, web-client) does not change. But you need to change the way you extract data from data source.

enter image description here