0
0
Spring Bootframework~30 mins

Service calling repository in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Service Calling Repository in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to manage books in a library. The application needs to store book information and retrieve it when requested.In this project, you will create a repository to hold book data and a service that calls this repository to get the list of books.
🎯 Goal: Build a Spring Boot service class that calls a repository class to fetch a list of books.This will help you understand how services and repositories work together in Spring Boot.
📋 What You'll Learn
Create a repository class with a method to return a list of book titles
Create a service class that calls the repository method
Use dependency injection to connect the service and repository
Return the list of books from the service method
💡 Why This Matters
🌍 Real World
In real applications, services call repositories to separate business logic from data access. This pattern helps keep code organized and easier to maintain.
💼 Career
Understanding how to connect services and repositories with Spring annotations is essential for backend development roles using Spring Boot.
Progress0 / 4 steps
1
Create the BookRepository class with a method to get books
Create a class called BookRepository with a method getBooks() that returns a List<String> containing exactly these book titles: "Spring Basics", "Java Fundamentals", "REST APIs".
Spring Boot
Need a hint?

Use Arrays.asList to create the list of book titles inside the getBooks() method.

2
Add a service class with a repository field
Create a class called BookService with a private field bookRepository of type BookRepository. Add a constructor that takes a BookRepository parameter and assigns it to the bookRepository field.
Spring Boot
Need a hint?

Use constructor injection to assign the bookRepository field.

3
Add a method in BookService to call the repository
In the BookService class, add a public method getAllBooks() that returns a List<String>. Inside this method, call bookRepository.getBooks() and return its result.
Spring Boot
Need a hint?

Call the repository method inside the service method and return the list.

4
Add Spring annotations to connect service and repository
Add the @Repository annotation to the BookRepository class and the @Service annotation to the BookService class. Also, add the @Autowired annotation to the constructor of BookService to enable dependency injection.
Spring Boot
Need a hint?

Use Spring annotations @Repository, @Service, and @Autowired to connect the classes.