Spring Stereotype Annotations – Difference between @Service and @Repository

Spring provides some stereotype annotations, among them, @Service and @Repository are special cases of @Component for specific purposes. By annotating component classes with @Service, @Repository, your classes are more clear, thus, Spring will automatically scan and import those beans into the container, so we don’t need to define them using XML.

This tutorial show you overview of Repository Pattern & a Service Layer, as well as difference between @Service & @Repository.

I. Introduction
1. Repository Pattern

A Repository provides an conceptual set, so we can work with a simple object that implements an interface which acts like a collection, with more complex querying capability. Selecting, adding, removing, updating items can be done using straightforward methods, without dealing with database concerns (connections, commands, cursors…)

The main difference between a Repository and a Data Access Object (DAO) is that the Repository returns objects that are understood by the calling layer (usually, business layer, and it returns business objects), while DAO returns data which are not in a valid business concept (data couldn’t be a whole business object).

An example of a Repository:

2. Service Layer

Service Layer exposes business logic, which uses Repository.
An example of a service:

So, our application data flow would be like this:
Controller -> Service -> Repository -> Data Source

3. @Service and @Repository in Spring

The core purpose is technically similar, both of them are specializations of @Component, so we can annotate our classes with @Component, but if annotating with @Repository, @Service instead, we could get more advantages:
– clear semantics and purposes:
+ @Service for classes at Business layer.
+ @Repository for classes at Persistence Layer which will be used as Database Repository.
@Repository is already supported for automatic exception translation in persistence layer.
– in the future of the Spring Framework, @Repository, @Service may have more additional semantics and add more values.

II. Example Overview
1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

2. Project Structure

stereotype-structure

3. Step by step

– Create Maven project
– Add Dependencies & Plugins
– Create Data Model
– Create Repository Component
– Create Service Component
– Create Controller Component
– Create Configuration XML File
– Create MainApplication Class
– Run Application & Enjoy Result

III. Practice
1. Create Maven project

– Open Spring Tool Suite, on Menu, choose File -> New -> Maven Project.
– Check Create a simple project, choose Workspace Location and click Next.
– Fill all fields in Artifact group box, then click Finish.

2. Add Dependencies & Plugins

Open pom.xml, add:

3. Create Data Model

Under package model, create Customer.java:

4. Create Repository Component

Under package repo, create interface ICustomerRepository.

ICustomerRepository.java

Then, create Implementation of that interface.

CustomerRepository.java

5. Create Service Component

Under package service, create interface ICustomerService.

ICustomerService.java

Then, create Implementation of that interface.

CustomerService.java

6. Create Controller Component

Under package controller, create class MyController.

MyController.java

7. Create Configuration XML File

Under src/main/resources, create bean.xml:

8. Create MainApplication Class

Content of MainApplication.java

9. Run Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Java Application
– Check results in Console Screen:

IV. Source Code

springstereotypeannotation

By JavaSampleApproach | September 19, 2016.

Last updated on June 4, 2017.


Related Posts


2 thoughts on “Spring Stereotype Annotations – Difference between @Service and @Repository”

  1. Hi, First and foremost. Thanks for this wonderful note. I like the MVC modelling here with simple flow. I was wondering if I were to introduce database for persistence (say, mysql), do you have any examples of code that can be used to add database persistence?

    I generally understand mysql connectivity but I would like to understand the database persistence with respect to the MVC setup here.

    Thanks in advance.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*