Spring data jpa repo, why need interface service and service implementation

Viewed 6384

I just started work on Spring Boot, using Spring Data JPA. When I generate model from table, I create a modelRepo which extends JpaRepository<myModel, String>

public interface userRepository extends JpaRepository<User, String>{
}

Then from a controller, I can easy call userRepository.findAll() to get data.

However, when I look on some tutorials, they have extra several steps before calling findAll(). Take a look below:

public interface userService{
Iterator findAll();

}

    public class userServiceImpl implements userService{
       @Autowired
       UserRepository userRepository
       @Override
       Iterator findAll(){
        return userRepository.findAll();
       }
    }

Somethings like that, I can query the data directly from userRepository as long as @Autowired to inject userRepository.

In some example, they do same structure at above. Can anyone explain why do we need service and serviceImpl before calling data.

3 Answers
Related