suraj.ratnaparkhi suraj.ratnaparkhi - 7 months ago 23
Java Question

What is DAO and Service layer exactly in Spring framework?

What is DAO and Service layer exactly in Spring framework?

I am looking for theoretical answer.

Answer

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