'Adding Spring Dependency Injection in JavaFX (JPA Repo, Service)
I have a java FX basic application which has a simple Scene(a form). I have a Mysql Db and I am using Spring JPA (spring data jpa i.e repository/entities) to interact with the same.
Now, since we know that javaFx has some lifecycle hooks namely: init() start() and stop().
Let's say I want to insert data in Database using JPA save() method. Usually, if it was my controller, a normal DB injection like:
@Autowired
EmployeeRepo employeeRepo;
Would have worked. But, I am not able to access this (or any Autowired Injection) inside the lifecycle methods.
public void start(Stage primaryStage) throws Exception {
// Some Code
employeeRepo.findAll() <- This is returning null
However, when I add a test method and use the same, It works fine:
@PostConstruct
public void test() {
// Repo object is not giving null
}
Is there a way I can manually inject the dependencies inside my button listener or pass it to the launch method.
Please let me know if there is a solution as I am new to JavaFX
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|