@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:


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:

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:

public class NotificationsController {

    private NotificationService notificationService;

    public void sendNotification(@RequestBody Notification 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.

To learn more, check out Spring Boot tutorials page.