Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - Difference Between @Service Annotation and @Repository Annotation

In Spring and Spring Boot applications, @Service and @Repository are both stereotypes, and they provide a way to annotate and define the roles or semantics of classes. Let's delve into their differences:

  1. Primary Purpose:

    • @Service: This annotation is used to annotate service classes, indicating that they hold business logic. Service classes might have transactional logic, call methods from the data access layer, or contain other business-related functionality.
    • @Repository: This annotation is used to annotate data access classes, indicating they will directly interact with the database. Such classes often use the Java Persistence API (JPA), JDBC, Hibernate, JdbcTemplate, or other data access technologies.
  2. Exception Translation:

    • @Service: Does not come with any built-in exception translation mechanism.
    • @Repository: When you use the @Repository annotation, Spring provides a persistence exception translation mechanism. This means that data access exceptions (typically, technology-specific, like SQLException for JDBC) are automatically translated into Spring's DataAccessException hierarchy, making error handling more consistent and easier.
  3. Typical Annotations Used With:

    • @Service: Service classes, when using Spring's declarative transaction management, will often be paired with @Transactional. This ensures that methods in the service class participate in a transaction.
    • @Repository: The classes annotated with @Repository usually work closely with the @Entity classes in JPA-based applications and often involve @Query, @Persist, @Update, etc.
  4. Layering:

    • @Service: Represents the service layer (or business layer) in a typical layered application architecture.
    • @Repository: Represents the data access layer in the architecture.
  5. Component Scanning:

    • @Service and @Repository: Both annotations are specializations of the @Component annotation. When you use <context:component-scan/> in XML configuration or @ComponentScan in Java configuration, Spring will automatically detect and register beans for classes annotated with @Service or @Repository.
  6. AOP Integration:

    • @Service: While it doesn't have specific AOP integrations, the semantic meaning of @Service often means it's a likely target for aspects that deal with business operations (like transactions).
    • @Repository: As mentioned above, one of the key AOP integrations is the exception translation mechanism.

In summary, while both @Service and @Repository help define the semantics and roles of classes in a Spring application, they serve different purposes. @Service is for business logic, and @Repository is for data access logic. These annotations, along with @Controller (for the presentation layer), help define a clear layered architecture for Spring applications.

  1. Transaction management with @Service and @Repository in Spring Boot:

    • Both @Service and @Repository classes can participate in transactions.
    • Example (@Service):
      @Service
      @Transactional
      public class UserService {
          // Business logic with transactional support
      }