Unfortunately I don't find the link to the solution, that works for me, anymore...
But: I have the code, which I tested to some extend.
First you need your Application class:
package eu.dzim.yatafx;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import eu.dzim.yatafx.model.app.ApplicationModel;
import eu.dzim.yatafx.spring.service.FXMLLoaderService;
import eu.dzim.yatafx.util.Utils;
import eu.dzim.yatafx.util.res.StringResource;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class YataFXApplication extends Application implements CommandLineRunner {
private static final Logger LOG = LogManager.getLogger(FileSyncFXApplication.class);
@Override
public void run(String... args) {
// something to call prior to the real application starts?
}
private static String[] savedArgs;
// locally stored Spring Boot application context
private ConfigurableApplicationContext applicationContext;
// we need to override the FX init process for Spring Boot
@Override
public void init() throws Exception {
// set Thread name
Thread.currentThread().setName("main");
// LOG.debug("Init JavaFX application");
applicationContext = SpringApplication.run(getClass(), savedArgs);
applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
}
// ... and close our context on stop of the FX part
@Override
public void stop() throws Exception {
// LOG.debug("Stop JavaFX application");
super.stop();
applicationContext.close();
}
protected static void launchApp(Class<? extends FileSyncFXApplication> appClass, String[] args) {
FileSyncFXApplication.savedArgs = args;
Application.launch(appClass, args);
}
@Autowired
private FXMLLoaderService mFXMLLoaderService;
@Autowired
private ApplicationModel mApplicationModel;
@Override
public void start(Stage primaryStage) {
// set Thread name
Thread.currentThread().setName("main-ui");
try {
FXMLLoader loader = mFXMLLoaderService.getLoader(Utils.getFXMLResource("Root"), StringResource.getResourceBundle());
Pane root = loader.load();
Scene scene = new Scene(root, 1200, 800);
scene.getStylesheets().add("/eu/dzim/filesyncfx/ui/application.css");
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(windowEvent -> {
LOG.debug("tear down JavaFX application");
// mApplicationModel.setLoggedIn(!mLoginService.logout());
// orderly shut down FX
Platform.exit();
// But: there might still be a daemon thread left over from OkHttp (some async dispatcher)
// so assume everything is fine and call System.exit(0)
System.exit(0);
});
primaryStage.show();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
public static void main(String[] args) throws Exception {
// SpringApplication.run(SampleSimpleApplication.class, args);
savedArgs = args;
Application.launch(FileSyncFXApplication.class, args);
}
}
I used org.springframework.boot:spring-boot-starter-parent:1.3.3.RELEASE
as the base.
Note the FXMLLoaderService
interface I autowired here:
package eu.dzim.yatafx.spring.service;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXMLLoader;
public interface FXMLLoaderService {
FXMLLoader getLoader();
FXMLLoader getLoader(URL location);
FXMLLoader getLoader(URL location, ResourceBundle resourceBundle);
}
The implementation looks like this:
package eu.dzim.yatafx.spring.service.impl;
import java.net.URL;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import eu.dzim.yatafx.spring.service.FXMLLoaderService;
import javafx.fxml.FXMLLoader;
import javafx.util.Callback;
@Component
@Scope("singleton")
public class FXMLLoaderServiceImpl implements FXMLLoaderService {
private static final Logger LOG = LogManager.getLogger(FXMLLoaderServiceImpl.class);
@Autowired
private ConfigurableApplicationContext context;
@PostConstruct
private void postConstruct() {
LOG.debug("PostConstruct: set up " + getClass().getName());
}
@Override
public FXMLLoader getLoader() {
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(new Callback<Class<?>, Object>() {
@Override
public Object call(Class<?> param) {
return context.getBean(param);
}
});
return loader;
}
@Override
public FXMLLoader getLoader(URL location) {
FXMLLoader loader = new FXMLLoader(location);
loader.setControllerFactory(new Callback<Class<?>, Object>() {
@Override
public Object call(Class<?> param) {
return context.getBean(param);
}
});
return loader;
}
@Override
public FXMLLoader getLoader(URL location, ResourceBundle resourceBundle) {
FXMLLoader loader = new FXMLLoader(location, resourceBundle);
loader.setControllerFactory(new Callback<Class<?>, Object>() {
@Override
public Object call(Class<?> param) {
return context.getBean(param);
}
});
return loader;
}
@PreDestroy
private void preDestroy() {
LOG.debug("PreDestroy: tear down " + getClass().getName());
}
}
The usage is already displayed in the Application class: Just @Autowire the service and create the sub-views from there. Since I rely almost exclusivly on FXML, this one is importand to me, since I want to use all that nice DI stuff in my Controllers.
Best example is the global Application, wich holds some JavaFX properties I attach to in the controller classes.
While the shown application is more a stub (the application and the FXML service), I had a fun project where I used this approach in concurrency to a parallel developed Web application, that was RESTing on a "Micro"service I developed at work.
Hope the code is enough an example to get it to work on your side. Please just ask, if you have more questions.
Cheers,
Daniel
Edit: I think the mistake in your code is simply the part in the FXML service. I have this injected ConfigurableApplicationContext which I use to create the controller from. You would need a ControllerFactory for this.
There are some third party samples to assist with JavaFX and Spring Boot integration: