@Service Annotation in Spring

In this tutorial, you will learn what the @Service annotation is and how to use it in your Spring Boot applications.

@Service annotation is a part of the Spring Framework, and you will use it to mark a class as a service layer component. This annotation is typically used in conjunction with @Component, which is a general-purpose annotation for marking a class as a component in Spring.

To learn about other Spring Boot annotations, check out Spring Stereotype Annotations tutorial.

How to Use @Service Annotation

@Service annotation can be used to denote a class that performs a specific service or function in a Spring Boot application. You will use it to specify that a class performs business logic or data manipulation tasks.

To use @Service annotation in a Spring Boot Rest application with Maven, follow these steps:

Step 1. Add Spring Boot Web Dependency

Add the Spring Boot starter dependency in your pom.xml file:

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

Step 2. Annotate Class with @Service Annotation

Create a class that performs a specific service or function in your application. For example, let’s say you want to create a class that sends notification messages. You can create a class called NotificationService:

@Service
public class NotificationService {

    public void sendNotification(Notification notification) {
        // Code to send notification message 
    }
}

Notice that this class is annotated with the @Service annotation above the class name. This means that the NotificationService is a Service/Business layer component and will contain the main business logic of the Notification Service.

Step 3. Inject Service Class into a Controller Class

You can now use dependency injection and inject the NotificationService class into the component or controller that needs to use it. For example, if you want to use the NotificationService class in a controller, you can do the following:

@RestController
public class NotificationsController {

    @Autowired
    private NotificationService notificationService;

    @PostMapping("/notifications/send")
    public void sendNotification(@RequestBody Notification notification) {
        notificationService.sendNotification(notification);
    }
}

Now you can run your Spring Boot application and make a request to the /notifications/send endpoint to trigger the NotificationService’s sendNotification() method.

Frequently Asked Questions

  1. What is the purpose of the @Service annotation?
    The primary purpose of the @Service annotation is to serve as a specialization of @Component. This lets the developer and other readers of the code understand that the class isn’t just any component, but a service with specific business logic.
  2. How does the @Service annotation work with Spring’s dependency injection?
    When you mark a class with @Service, Spring will automatically register it as a single bean in the application context, which can be auto-wired into other beans as a dependency.
  3. What’s the difference between @Service, @Repository, and @Controller in Spring?
    These are all stereotype annotations that mark classes as Spring components. They each serve different roles within the MVC pattern: @Repository classes interact with the database and handle data operations, @Service classes contain business logic, and @Controller classes handle incoming HTTP requests and return responses.
  4. Can I use @Component instead of @Service? What’s the difference?
    Technically, you can use @Component instead of @Service, as both mark a class as a bean. However, using @Service provides additional semantic value by making it clear that the class is intended to hold business logic.
  5. Do I need to use @Service annotation in Spring Boot?
    While not mandatory, using the @Service annotation is recommended in Spring Boot to take advantage of Spring’s automatic configuration and dependency injection features.
  6. What is the scope of a @Service bean?
    By default, a @Service bean is a singleton, meaning Spring will create exactly one instance of the class and share it among all other beans. However, you can change this default scope using the @Scope annotation.

Conclusion

@Service annotation is a useful tool for organizing and separating business logic and data manipulation tasks in a Spring Boot application. By using @Service annotation, you can clearly denote which classes in your application are responsible for performing specific services and functions.

To learn more, check out Spring Boot tutorials page.