Spring doesn't autowire static
fields. That is why userRepo
field is null
. One way is to make the UserFacade
a bean itself, and then you can make userRepo
a non-static field. I would prefer this way. UserFacade
shouldn't really be a utility class, since it is interacting with the repository bean. It would make much more sense to make it a bean.
Another option is to provide a setter, and use @Autowired
on that:
@Autowired
public void setUserRepo(UserRepository userRepo) {
UserFacade.userRepo = userRepo;
}
Or using it even on a parameterized constructor would work.