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?
✗ Incorrect
The @Autowired annotation is used to inject dependencies like a Repository into a Service.
Which layer should contain business logic in a Spring Boot application?
✗ Incorrect
The Service layer contains business logic and calls the Repository for data access.
What does the Repository layer mainly handle?
✗ Incorrect
The Repository layer handles data access and database operations.
Which annotation marks a class as a Service in Spring Boot?
✗ Incorrect
The @Service annotation marks a class as a Service component.
Why is it better to call a Repository from a Service instead of directly from a Controller?
✗ Incorrect
Calling Repository from Service separates business logic from data access, improving code organization.
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.