Complete the code to inject the repository into the service class.
public class UserService { private final UserRepository [1]; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } }
The service class should declare a field named userRepository to match the constructor parameter and follow common naming conventions.
Complete the code to annotate the service class for Spring to detect it as a service.
[1] public class UserService { // service methods }
The @Service annotation marks the class as a service component in Spring.
Fix the error in the service method to call the repository's findById method correctly.
public User getUserById(Long id) {
return userRepository.[1](id).orElse(null);
}The correct repository method to find by ID returning Optional is findById.
Fill both blanks to complete the service method that saves a user using the repository.
public User saveUser(User [1]) { return userRepository.[2]([1]); }
The method parameter should be named user and the repository method to save is save.
Fill all three blanks to complete the service method that deletes a user by ID using the repository.
public void deleteUserById([1] [2]) { userRepository.[3]([2]); }
The method parameter type is Long, the parameter name is id, and the repository method to delete by ID is deleteById.