0

I am trying to autowire my jpa repository. However, it is getting autowired as null. I was following a tutorial! that said the interface that extends CrudRepository will be created in runtime by spring for me. Of course, I can write all the implemented methods from scratch. Still, I want to know if the tutorials spring base configuration is possible for me.

I have tried using the @EnableJPARepositories annotation, and it did not work.

-- java.com.GMorgan.RateMyFriendv5
  -- Controller
    ...
  -- Entity
    ...
  -- Repository
    UserRepositroy
  -- Service
    UserService
  RateMYFreindv5Application

UserRepository

public interface UserRepository extends CrudRepository<User, UUID> {
        List<User> findByUsername(String username);
        List<User> findByEmail(String email);
}

UserService

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserRepository repository;

    public boolean login(String username, String password) {
        List<User> userList = repository.findByUsername(username);
        boolean isSuccessful = userList.get(0).isPassword(password);
        log.info("Username: {} isSucessful: {}", username, isSuccessful);
        return isSuccessful;
    }

    public boolean signUp(String email, String username, String password) {
        if (userEmailExists(email) || userUsernameExists(username)) return false;
        User user = new User();
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(Role.USER);
        repository.save(user);
        log.info("User email: {} username: {}", email, username);
        return true;
    }

    private boolean userEmailExists(String email) {
        return !repository.findByEmail(email).isEmpty();
    }

    private boolean userUsernameExists(String username) {
        return !repository.findByUsername(username).isEmpty();
    }
}

RateMyFriend

@EnableJpaRepositories(repositoryBaseClass = UserRepository.class)
@SpringBootApplication(scanBasePackages = {"com.GMorgan.RateMyFriendv5.Service",
        "com.GMorgan.RateMyFriendv5.Controller"})
public class RateMyFriendv5Application {

    public static void main(String[] args) {
        SpringApplication.run(RateMyFriendv5Application.class, args);
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rahmi Pruitt
  • 569
  • 1
  • 8
  • 28

0 Answers0