1
I’m on the following case:
I have an application developed with Spring Boot, and I am developing the front end with Javafx. It is a local application, not a web one. I can run the project, Spring runs, and the window prepared with Javafx appears. However, when trying to manipulate data, bringing them to be displayed in the window with Javafx, I get an error:
Cannot invoke "services.ClienteService.findAll()" because "this.cs" is null at com.fgwr.jpcorretora.FrontApp.<init>(FrontApp.java:39)
Without Javafx the application runs normally. I am running Javafx + Spring Boot as follows:
Spring Boot class:
@SpringBootApplication
public class JpcorretoraApplication implements CommandLineRunner {
@Autowired
ClienteService cs;
public JpcorretoraApplication() {
List <Cliente> allcli = cs.findAll();
for (Cliente cliente : allcli) {
clienteData.add(cliente);
System.out.println(cliente.getNome());
}
}
public static void main(String[] args) {
Application.launch(FrontApp.class, args);
}
@Override
public void run(String... args) throws Exception {
Javafx class:
public class FrontApp extends Application {
@Autowired
ClienteService cs;
private Stage primaryStage;
private BorderPane rootLayout;
private ConfigurableApplicationContext applicationContext;
private ObservableList<Cliente> clienteData = FXCollections.observableArrayList();
public FrontApp() {
clienteData = (ObservableList<Cliente>) cs.findAll();
for (Cliente cliente : clienteData) {
System.out.println(clienteData);
}
}
public ObservableList<Cliente> getClienteData() {
return clienteData;
}
@Override
public void init() {
String[] args = getParameters().getRaw().toArray(new String[0]);
this.applicationContext = new SpringApplicationBuilder().sources(JpcorretoraApplication.class).run(args);
}
In any of the classes I try to perform my service cs
, get the bug. From what I’ve worked on spring boot, I want it to be an injection failure, in which the service cs
is not being injected when I try to work outside the context of Spring Boot, that is, inside javafx. I tried several solutions, but none satisfied my need.
All classes have their respective annotations correct (e.g.: @Service
).
The idea is that I can recover the data from my database that is being operated by Jparepository in spring boot and move on to the javafx show on the screen. Anyone can help?
Ñ know how it works but try reading this: https://stackoverflow.com/q/33612130/2241463 It seems that the annotation
@SpringBootApplication
although it seems that it would only be for the main application may serve to be used in various applications within the main one, so it could give to include it in its classFrontApp
. Another idea is to solve this via configuration file, Springboot should not know that in this classFrontApp
has something to be injected, have to warn him in some way. This should not be a problem for theJpcorretoraApplication
on account of that annotation.– Piovezan
I’ve tried these solutions. None of them worked. It’s like Springboot has isolated my class from Javafx so it doesn’t have access to the environment. I can’t run any service within Javafx classes.
– Fabrício Gustavo