0
0
Spring Bootframework

Service calling repository in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the role of a Service class in Spring Boot when calling a Repository?
The Service class contains business logic and calls the Repository to access or modify data in the database. It acts as a bridge between controllers and repositories.
Click to reveal answer
beginner
How do you inject a Repository into a Service class in Spring Boot?
You inject a Repository into a Service class using the @Autowired annotation or constructor injection, allowing the Service to use Repository methods.
Click to reveal answer
beginner
Why should a Service call a Repository instead of directly accessing the database?
The Service layer separates business logic from data access, making code easier to maintain, test, and reuse. It keeps responsibilities clear.
Click to reveal answer
beginner
What annotation marks a class as a Service in Spring Boot?
The @Service annotation marks a class as a Service component, making it a candidate for Spring's component scanning and dependency injection.
Click to reveal answer
beginner
Show a simple example of a Service method calling a Repository method.
Example:
@Service
public class UserService {
  private final UserRepository userRepository;

  public UserService(UserRepository userRepository) {
    this.userRepository = userRepository;
  }

  public User getUserById(Long id) {
    return userRepository.findById(id).orElse(null);
  }
}
Click to reveal answer
What annotation is typically used to inject a Repository into a Service in Spring Boot?
A@Autowired
B@Repository
C@Service
D@ComponentScan
Which layer should contain business logic in a Spring Boot application?
ARepository layer
BEntity layer
CController layer
DService layer
What does the Repository layer mainly handle?
AUser interface rendering
BData access and database operations
CBusiness rules
DHTTP request handling
Which annotation marks a class as a Service in Spring Boot?
A@Controller
B@Repository
C@Service
D@Entity
Why is it better to call a Repository from a Service instead of directly from a Controller?
ATo separate business logic from data access
BTo make the Controller heavier
CTo avoid using Spring annotations
DTo skip database transactions
Explain the relationship between Service and Repository layers in Spring Boot.
Think about who talks to the database and who processes the data.
You got /4 concepts.
    Describe how to inject a Repository into a Service class and why it is important.
    Focus on how Spring manages dependencies between classes.
    You got /4 concepts.