2

Yesterday I tried to create project connecting Spring Boot and JavaFX. I was basing on http://www.greggbolinger.com/let-spring-be-your-javafx-controller-factory/

So I created a project and when I run application spring context is created and JavaFx application is run. But the problem is when I try to create some beans e.g using @Repository annotation. When I am autowiring then value is null.

CarGarageApplication.java

package com.car.garage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.car.garage.dao.UsersRepository;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

@SpringBootApplication
@ComponentScan
@EnableJpaRepositories("com.car.garage.dao")
public class CarGarageApplication extends Application {

    private ConfigurableApplicationContext mainContext;
    private Parent rootNode;

    @Autowired
    UsersRepository usersRepository;

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() throws Exception {
        mainContext = SpringApplication.run(CarGarageApplication.class);
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/WelcomePage.fxml"));
        loader.setControllerFactory(mainContext::getBean);
        rootNode = loader.load();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene(new Scene(rootNode));
        primaryStage.setResizable(false);
        primaryStage.show();
        System.out.println(usersRepository);
    }

    @Override
    public void stop() throws Exception {
        mainContext.close();
    }
}

UsersRepository.java

package com.car.garage.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.car.garage.model.User;

@Repository
public interface UsersRepository extends CrudRepository<User, Long> {

}

User.java

package com.car.garage.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;

    private String username;
    private String password;

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

EDIT:

mainContext.getBean(UsersRepository.class) return correct bean instance of UsersRepository but why @Autowired doesn't work

Rafał Sokalski
  • 1,817
  • 2
  • 17
  • 29
  • Have you tried the steps proposed by this post? https://stackoverflow.com/questions/40118876/spring-jpa-not-implementing-autowiring-repository-despite-enablejparepositories – Tonino Sep 19 '19 at 08:22
  • Yes, after that there is the same problem – Rafał Sokalski Sep 19 '19 at 08:26
  • 1
    You should not directly autowire repository class in a boot class. there should be some upper layer like component class which autowire this repository class. – GauravRai1512 Sep 19 '19 at 08:38
  • Ok I understand, thank you for answer. Is there any reason why it is not allowed ? – Rafał Sokalski Sep 19 '19 at 08:42
  • It's mandatory to have SPRING STEREOTYPES class(@Component, @Service like that) from where you are autowiring the another class and in your main class which is boot class there is no such stereotype annotation present. – GauravRai1512 Sep 19 '19 at 08:45

1 Answers1

1

We should follow below approach to autowire repository class. because It's mandatory to have SPRING STEREOTYPES class(@Component, @Service like that) from where you are autowiring the another class

@Component
 public class Anotherclass{

     @Autowired
     private UsersRepository usersRepository;
}
GauravRai1512
  • 834
  • 6
  • 14