AI Hints
Home›Spring Boot Tutorial›@Service, Constructor Injection and @Component and Friends
Spring Boot · Lesson 31 of 120

@Service

Where the logic lives.

⏱ 2 min read▶ Video at 40:43🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

A controller's job is the web. Reading the request, and sending the response. The real work, the business logic, belongs in a service.

  • Controller: the web
  • Service: the real work
  • Keep them apart

Controller and service

The controller knows about URLs, parameters, and status codes. The service knows the rules of the business, like how discounts work. The service knows nothing about the web. So it can be reused, and tested on its own.

Controller

  • URLs and parametersStatus codes and JSONThin: calls the service

Service

  • Business rulesCalculations, decisionsNo web code at all

BookService.java

At Service marks this class as a service bean. It holds a list of books. All returns them. And price with discount does the business rule. It takes a percentage off, and rounds to two decimal places.

BookService.javajava
@Service
public class BookService {

    private final List<Book> books = List.of(
        new Book(1, "Dune", "Frank Herbert", 9.99),
        new Book(2, "Circe", "Madeline Miller", 10.99));

    public List<Book> all() {
        return books;
    }

    public double priceWithDiscount(int id, int percent) {
        Book book = books.get(id - 1);
        double price = book.price() * (100 - percent) / 100;
        return Math.round(price * 100) / 100.0;
    }
}

A thin controller

The controller receives the service, through its constructor. The endpoint reads the id, and the discount, from the URL. Then it simply asks the service, and returns the answer. Run it. Dune costs nine ninety nine. With twenty percent off, it's seven ninety nine. And Circe, with ten off, is nine eighty nine.

BookController.javajava
@RestController
public class BookController {

    private final BookService service;

    public BookController(BookService service) {
        this.service = service;
    }

    @GetMapping("/books/{id}/price")
    public String price(@PathVariable int id, @RequestParam(defaultValue = "0") int off) {
        return "Price: " + service.priceWithDiscount(id, off);
    }
}
Output
$ curl localhost:8080/books/1/price
Price: 9.99
$ curl "localhost:8080/books/1/price?off=20"
Price: 7.99
$ curl "localhost:8080/books/2/price?off=10"
Price: 9.89
Also uses Book.java
public record Book(Integer id, String title, String author, double price) {}
Also uses BookService.java
@Service
public class BookService {

    private final List<Book> books = List.of(
        new Book(1, "Dune", "Frank Herbert", 9.99),
        new Book(2, "Circe", "Madeline Miller", 10.99));

    public List<Book> all() {
        return books;
    }

    public double priceWithDiscount(int id, int percent) {
        Book book = books.get(id - 1);
        double price = book.price() * (100 - percent) / 100;
        return Math.round(price * 100) / 100.0;
    }
}
Also uses _curl.sh
curl localhost:8080/books/1/price
curl "localhost:8080/books/1/price?off=20"
curl "localhost:8080/books/2/price?off=10"

Summary

Services hold the business logic, and controllers stay thin, handling only the web. At Service makes a class a service bean. Next, constructor injection.

Key takeaways
  • @Service
  • Business logic
  • Thin controllers
Next up: Constructor Injection — the best way to inject