Table of contents
1.
Introduction
2.
@Service Annotation
2.1.
Features of @Service Annotation
2.2.
Syntax
3.
Spring @Service Example
3.1.
Why Use @Service?
3.2.
How to Use @Service
4.
Testing
4.1.
Why Test Service Classes?
4.2.
Steps to Test a Service Class
5.
Procedure
5.1.
Step 1: Add Spring Boot Dependency
5.2.
Step 2: Create a Service Class
5.3.
Step 3: Use the Service in a Controller
5.4.
Step 4: Run the Application
5.5.
Expected Output
6.
Usage of @Service Annotation
6.1.
1. Business Logic Implementation
6.2.
2. Interacting with Repository Layer
6.3.
3. Exception Handling
7.
Frequently Asked Questions
7.1.
What is the purpose of the @Service annotation in Spring Boot?
7.2.
Is @Service mandatory for a service class in Spring Boot?
7.3.
How does @Service differ from @Component?
8.
Conclusion
Last Updated: Feb 15, 2025
Medium

@Service Annotation in Spring Boot

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Spring Boot, the @Service annotation is used to define service layer components in an application. It marks a class as a business logic service, allowing Spring to manage it as a Spring Bean. This annotation helps in separating business logic from the controller layer, improving code maintainability and modularity.

@Service Annotation in Spring Boot

In this article, you will learn the importance of @Service in Spring Boot, its usage, and examples of how to implement it effectively.

@Service Annotation

The @Service annotation in Spring Boot is a specialization of the @Component annotation. It is used to mark a class as a service provider. The primary purpose of using @Service is to indicate that a class contains business logic and should be managed by the Spring container as a service layer bean.

Features of @Service Annotation

  • It helps in separating business logic from other components like controllers and repositories.
     
  • It is a specialization of the @Component annotation, which means it can be automatically detected during component scanning.
     
  • It improves the readability and maintainability of code by clearly indicating that the class contains service logic.

Syntax

import org.springframework.stereotype.Service;

@Service
public class MyService {
    public String getMessage() {
        return "Hello from MyService!";
    }
}

Spring @Service Example

The `@Service` annotation is a specialization of the `@Component` annotation in Spring. It is used to indicate that a class is a service component, typically containing business logic. When you annotate a class with `@Service`, Spring automatically detects it during component scanning & registers it as a bean in the application context.

Why Use @Service?

Using `@Service` makes your code more organized & easier to maintain. It separates business logic from other layers like controllers or data access layers. This separation is crucial for building scalable & clean applications.

How to Use @Service

To use the `@Service` annotation, you need to follow these steps:

1. Create a Service Class: Define a class & annotate it with `@Service`.
 

2. Add Business Logic: Write the methods that contain your business logic.
 

3. Inject the Service: Use `@Autowired` to inject the service into other components like controllers.
 

Let’s create a simple example to show this.

 

1. Create a Spring Boot Project: If you don’t have a project, you can create one using [Spring Initializr](https://start.spring.io/). Add the `Spring Web` dependency.

 

2. Define the Service Class:

Create a package named `com.example.service` & add a class called `UserService`.

package com.example.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

public String greetUser(String name) {
     return "Hello, " + name + "! Welcome to Spring Boot.";
    }
   }

 

Here, the `UserService` class is annotated with `@Service`. It contains a method `greetUser` that returns a greeting message.

 

3. Create a Controller to Use the Service:

Create another package named `com.example.controller` & add a class called `UserController`.

package com.example.controller;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
 public class UserController {

 @Autowired
 private UserService userService;

 @GetMapping("/greet")
 public String greet(@RequestParam String name) {
      return userService.greetUser(name);
       }
   }

 

In this class:

  • The `UserService` is injected using `@Autowired`.
     
  • The `greet` method maps to the `/greet` endpoint & calls the `greetUser` method from the service.
     

4. Run the Application:

Create the main application class in the root package.

   package com.example;

   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;

   @SpringBootApplication
   public class ServiceAnnotationExampleApplication {

       public static void main(String[] args) {
           SpringApplication.run(ServiceAnnotationExampleApplication.class, args);
       }
   }

 

5. Test the Application:

Run the application & open your browser or Postman. Navigate to:

   http://localhost:8080/greet?name=John

 

You should see the output:

   Hello, John! Welcome to Spring Boot.

Testing

Testing ensures that your code works as expected & helps catch bugs early. In Spring Boot, you can write unit tests for your service classes using JUnit (a testing framework) & Mockito (a mocking framework). Let’s write a unit test for the `UserService` class.

Why Test Service Classes?

Service classes contain the core business logic of your application. Testing them ensures that:

  • Your business logic works correctly.
     
  • Changes in the future don’t break existing functionality.
     
  • You can confidently refactor or improve your code.

Steps to Test a Service Class

 

1. Add Dependencies: Ensure you have the necessary testing dependencies in your `pom.xml` (for Maven) or `build.gradle` (for Gradle).
 

2. Write the Test Class: Create a test class for your service & write test methods.
 

3. Use Mockito for Mocking: If your service depends on other components, you can mock them using Mockito.

Complete Code Example


1. Add Dependencies:

If you’re using Maven, add the following dependencies to your `pom.xml`:

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
   </dependency>
   <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
       <scope>test</scope>
   </dependency>

 

If you’re using Gradle, add these to your `build.gradle`:

   testImplementation 'org.springframework.boot:spring-boot-starter-test'
   testImplementation 'org.mockito:mockito-core'

 

2. Write the Test Class: Create a test class for `UserService` in the `src/test/java` directory. Name it `UserServiceTest`.

   package com.example.service;

   import org.junit.jupiter.api.Test;
   import org.junit.jupiter.api.extension.ExtendWith;
   import org.mockito.InjectMocks;
   import org.mockito.junit.jupiter.MockitoExtension;
   import static org.junit.jupiter.api.Assertions.assertEquals;

   @ExtendWith(MockitoExtension.class)
   public class UserServiceTest {

       @InjectMocks
       private UserService userService;

       @Test
       public void testGreetUser() {
           // Arrange
           String name = "John";

           // Act
           String result = userService.greetUser(name);

           // Assert
           assertEquals("Hello, John! Welcome to Spring Boot.", result);
       }
   }

 

Here’s what’s happening in this code:

  • `@ExtendWith(MockitoExtension.class)`: Enables Mockito in the test class.
     
  • `@InjectMocks`: Injects the `UserService` instance into the test class.
     
  • `@Test`: Marks the method as a test case.
     
  • `assertEquals`: Checks if the actual result matches the expected result.

3. Run the Test:

You can run the test using your IDE (like IntelliJ or Eclipse) or from the command line using Maven/Gradle. For example, with Maven:

   mvn test

 

If everything is set up correctly, the test should pass, & you’ll see output like this:

   [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Procedure

To use the @Service annotation in a Spring Boot application, follow these steps:

Step 1: Add Spring Boot Dependency

Make sure you have the required Spring Boot dependencies in your pom.xml file:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Step 2: Create a Service Class

Create a service class and annotate it with @Service.

import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    public String getGreeting() {
        return "Welcome to Spring Boot!";
    }
}

Step 3: Use the Service in a Controller

Create a controller class and inject the service using @Autowired.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class GreetingController {
    
    @Autowired
    private GreetingService greetingService;

    @GetMapping("/greet")
    public String greet() {
        return greetingService.getGreeting();
    }
}

Step 4: Run the Application

Run your Spring Boot application and access the endpoint:

http://localhost:8080/api/greet

Expected Output

Welcome to Spring Boot!

Usage of @Service Annotation

The @Service annotation is mainly used to define service-layer beans in a Spring application. Here are some common use cases:

1. Business Logic Implementation

Services are used to implement business logic separately from controllers and repositories.

@Service
public class CalculationService {
    public int add(int a, int b) {
        return a + b;
    }
}

2. Interacting with Repository Layer

A service class can interact with a repository to fetch and process data.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }
}

3. Exception Handling

The service layer can also be used for handling exceptions and applying custom error logic.

@Service
public class DivisionService {
    public double divide(int a, int b) {
        if (b == 0) {
            throw new ArithmeticException("Cannot divide by zero");
        }
        return (double) a / b;
    }
}

Frequently Asked Questions

What is the purpose of the @Service annotation in Spring Boot?

The @Service annotation is used to indicate that a class contains business logic and should be managed as a service component by the Spring container.

Is @Service mandatory for a service class in Spring Boot?

No, it is not mandatory. However, using @Service makes it clear that the class contains service logic and allows Spring to detect it during component scanning.

How does @Service differ from @Component?

@Service is a specialization of @Component, meaning it is specifically meant for service layer beans, while @Component is a generic annotation used for any Spring-managed component.

Conclusion

In this article, we learned the @Service annotation in Spring Boot, which is used to define service layer components. It helps in managing business logic and ensures better separation of concerns in an application. By using @Service, developers can create reusable and maintainable code, improving the overall structure and efficiency of Spring Boot applications.

Live masterclass