JavaFX : Integrating Spring framework with JavaFX app(Incorrect configuration)

I am working on a JavaFX application and I would like to integrate Spring functionality with it. Currently the code compiles without any error, but when I request service layer methods which are tagged as @Transactional and @Service, I get NullPointerException. What am I doing wrong in the Spring configuration is what I dont understand. Here is my code for JavaFX :

Main class :

public class Main extends Application {

    private static final SpringFxmlLoader loader = new SpringFxmlLoader();

    public void start(Stage stage) throws Exception {
       Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("login.fxml"));
        stage.setScene(new Scene(root, 300, 600));
    public static void main(String[] args) {


@ComponentScan(basePackages = {"packagename"})
public class ApplicationConfiguration {

    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        Properties properties = new Properties();
        return propertySourcesPlaceholderConfigurer;

    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("messages", "");
        return messageSource;

SpringLoader :

public class SpringFxmlLoader {

    private static final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);

    public Object load(String url) {
        try (InputStream fxmlStream = SpringFxmlLoader.class
                .getResourceAsStream(url)) {
            FXMLLoader loader = new FXMLLoader();
            loader.setControllerFactory(new Callback<Class<?>, Object>() {
                public Object call(Class<?> clazz) {
                    return applicationContext.getBean(clazz);
            return loader.load(fxmlStream);
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);

Now in my Controller, I have something like this :

public class Controller implements Initializable {
    public TextField usernameField;
    public PasswordField passwordField;
    public Button submitButton;
    private PersonService personService;
// Now the above personService throws me a NPE.

Am I somehow messing with Spring config for JavaFX. Kindly let me know. Thanks a lot. :-)


After changes suggested by James D..I get the following error :

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(
    at com.sun.javafx.application.LauncherImpl$$Lambda$2/ Source)
Caused by: java.lang.NullPointerException: inputStream is null.
    at javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml.FXMLLoader.load(
    at tooltank.MainClass.SpringFxmlLoader.load(
    at tooltank.MainClass.Main.start(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(
    at com.sun.javafx.application.LauncherImpl$$Lambda$57/ Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(
    at com.sun.javafx.application.PlatformImpl$$Lambda$53/ Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(
    at com.sun.javafx.application.PlatformImpl$$Lambda$55/ Source)
    at Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(
    at com.sun.javafx.application.PlatformImpl$$Lambda$54/ Source)
    at Method)
    at$$Lambda$43/ Source)
    ... 1 more

Process finished with exit code 1

It happens in at following line :

 return loader.load(fxmlStream);


You have created a SpringFxmlLoader but you are not using it. You want

SpringFxmlLoader loader = new SpringFxmlLoader();
Parent root = (Parent) loader.load(getClass().getResource("login.fxml").toExternalForm());

instead of using the FXMLLoader directly.

I would actually write the SpringFxmlLoader differently, so that it matched the standard FXMLLoader API a little more closely:

public class SpringFxmlLoader {

    private static final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);

    public <T> T load(URL url) {
        try  {
            FXMLLoader loader = new FXMLLoader(url);
            return loader.load();
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);

Then your start method looks like:

SpringFxmlLoader loader = new SpringFxmlLoader();
Parent root = loader.load(getClass().getResource("login.fxml"));

You might need to tinker with the exact path to get things right, depending on your setup.

Need Your Help

Performance of MongoDB on VPS or cloud service not having mongoDB installed

php python mongodb vps

I have to build a web application which uses Python, php and MongoDB.

worklight app not working on real android phone

android jquery-mobile ibm-mobilefirst

I have developed a hybrid application in Ibm worklight using jquerymobile framework. The application accesses external database hosted on my own domain ( through php script. It w...