0
0
Spring Bootframework~5 mins

@Service annotation in Spring Boot

Choose your learning style9 modes available
Introduction

The @Service annotation marks a class as a service provider. It helps organize business logic in your application clearly and lets Spring manage it automatically.

When you want to separate business logic from controllers and data access.
When you need Spring to create and manage the service class instance for you.
When you want to make your code easier to test by isolating service logic.
When you want to clearly indicate that a class provides core application services.
Syntax
Spring Boot
@Service
public class YourServiceClass {
    // business methods here
}
Place @Service above the class declaration.
Spring automatically detects classes annotated with @Service during component scanning.
Examples
This example shows a simple service class that returns a user name.
Spring Boot
@Service
public class UserService {
    public String getUserName() {
        return "Alice";
    }
}
This service class handles order placement logic.
Spring Boot
@Service
public class OrderService {
    public void placeOrder() {
        // order logic here
    }
}
Sample Program

This complete example shows a @Service class providing a greeting method. A @Component class uses this service. The Spring Boot application runs and prints the greeting.

Spring Boot
import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}

// Usage in another Spring-managed class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GreetingController {
    private final GreetingService greetingService;

    @Autowired
    public GreetingController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    public void sayHello() {
        System.out.println(greetingService.greet("Bob"));
    }
}

// Main application to run
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    private final GreetingController greetingController;

    public DemoApplication(GreetingController greetingController) {
        this.greetingController = greetingController;
    }

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

    @Override
    public void run(String... args) throws Exception {
        greetingController.sayHello();
    }
}
OutputSuccess
Important Notes

The @Service annotation is a specialization of @Component, so it is detected by Spring's component scanning.

Use @Service to clearly separate business logic from other layers like controllers or repositories.

Spring manages the lifecycle of @Service beans, so you don't create instances manually.

Summary

@Service marks a class as a service layer component.

It helps organize business logic and lets Spring manage the class automatically.

Use it to keep your code clean and easy to test.