Spring Boot JPA Repository
Problem Statement : Want to access JPA repository functions like findAll(); etc inside a normal Java class.
For example : I have created a 1. student.java (model/getter setters) 2. student controller which talks to service 3. student service which talks to repository 4. student repository This all works fine i can get list of all students. localhost/student/all
However now i have a worker class (normal java class working on a thread) and from that worker class i want get student list how can i do that ???
Error I am getting is : java.lang.NullPointerException
I created a object of studentservice inside worker class and tried to access service function to get all students however getting above error.
Same is the case when i create object for student repository.
Please HElpppppp
Student Repository
import org.springframework.data.repository.CrudRepository;
public interface StudentRepository extends CrudRepository<Student, Integer> {
}
Student Service class which has StudentRepository as above
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<FxRates> getAllStudents(){
return (List<FxRates>) studentRepository.findAll();
}
}
Worker Class :
@Component
public class SomeWorker {
@Autowired
private StudentService studentService;
public SomeWorker() {
}
public void somefunction() {
System.out.println(studentService); /*** NULL **/
System.out.println(studentService.getAllStudents());
/*** Here i get error --> java.lang.NullPointerException ***/
}
}
Even if i use or @Service and @Configurable in worker class i get error.