springboot + hibernate + javaFX

时间:2017-02-20 16:04:18

标签: java hibernate spring-boot

我被困在这里几天没有到达任何地方。 这是合约。我正在构建一个使用springboot(主要是使用crudeRepository)和javafx和hibernate的应用程序。

但我不断收到此错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 52 more
Caused by: java.lang.NullPointerException
    at br.jus.trt9.PjeMonitor.view.MonitorPjeController.mouseEvent(MonitorPjeController.java:153)
    ... 62 more

这是因为crudrepository clientRepo为null,不知道为什么......在正常执行中它应该有这样的东西:

org.springframework.data.jpa.repository.support.SimpleJpaRepository@5a0a3243

任何人,知道为什么吗?提前谢谢你。

ClientRepo.java     进口......

/*TODO essa classe deve implementar o CrudRepository
que vem do spring-DATA-core
*/
@Repository
public interface ClientRepo extends CrudRepository<Client, Integer> {

}

MainApp.java

package br.jus.trt9.PjeMonitor;

imports
... 

/*
 * @AutoConfigurationPackage
 * 
 * @ComponentScan(basePackages = "br.jus.trt9.PjeMonitor")
 * 
 * @EnableAutoConfiguration
 */

//FIXME esse warning na inicialização
/* Unable to proxy method [public final void br.jus.trt9.PjeMonitor.dao.Ge
nericDao.setClazz(java.lang.Class)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.*/
@SpringBootApplication
@ComponentScan(basePackages = "br.jus.trt9.PjeMonitor")
public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    private ObservableList<Audiencia> audData = FXCollections.observableArrayList();
    private ConfigurableApplicationContext springContext;
    private static String[] args;

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

    @Override
    public void init() throws Exception {
        // applicationContext.ge3tAutowireCapableBeanFactory().autowireBean(this);


    }

    @Override
    public void start(Stage primaryStage) {

//      primaryStage.setScene(new Scene(rootLayout));
//      primaryStage.show();

         this.primaryStage = primaryStage;
         this.primaryStage.setTitle("Pje Midias Monitor");

        initRootLayout();

        showTableAudiencia();

    }

    public void stop() throws Exception {
        springContext.close();
    }

    public MainApp() {

        audData.add(new Audiencia("12123123213213", LocalDate.now(), "sim"));
        audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
        audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
        audData.add(new Audiencia("55555555555555", LocalDate.now(), "sim"));
        audData.add(new Audiencia("6666666666666", LocalDate.now(), "nao"));
        audData.add(new Audiencia("7777777777777", LocalDate.now(), "nao"));
        audData.add(new Audiencia("8888888888888", LocalDate.now(), "sim"));
        audData.add(new Audiencia("99999999999999", LocalDate.now(), "nao"));
        audData.add(new Audiencia("100010110101001", LocalDate.now(), "nao"));

    }

    /**
     * Initializes the root layout.
     */

     public void initRootLayout() {
     try {
     // Load root layout from fxml file.
     FXMLLoader loader = new FXMLLoader();
     loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
     rootLayout = (BorderPane) loader.load();

     // problema do classLoader do javaFX
     loader.setClassLoader(this.getClass().getClassLoader());

     // Show the scene containing the root layout.
     Scene scene = new Scene(rootLayout);
     primaryStage.setScene(scene);
     primaryStage.show();

     } catch (IOException e) {
     e.printStackTrace();
     }
     }

    public void showTableAudiencia() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/visao.fxml"));
            AnchorPane TableOverview = (AnchorPane) loader.load();

            //loader.setControllerFactory(springContext::getBean);

            // Set person overview into the center of root layout.  
            rootLayout.setCenter(TableOverview);

            // Give the controller acckess to the main app.
            MonitorPjeController controller = loader.getController();
            controller.setMainApp(this);
            System.out.println("carregou main app");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showCadastroClients() {

        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/clientcadastro.fxml"));
            AnchorPane page = (AnchorPane) loader.load();

            // Create the dialog Stage.
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Cadastro de máquinas");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public ObservableList<Audiencia> getAudData() {
        return audData;
    }

    public void setAudData(ObservableList<Audiencia> audData) {
        this.audData = audData;
    }

}

MonitorPjeController.java

package br.jus.trt9.PjeMonitor.view;

import ...;

public class MonitorPjeController {

    @FXML
    private TableView<Audiencia> audTable;

    @FXML
    private TableColumn<Audiencia, String> audId;

    @FXML
    private TableColumn<Audiencia, LocalDate> dataId;

    @FXML
    private TableColumn<Audiencia, String> assId;

    @FXML
    private ComboBox<Client> clientcomboBox;

    @FXML
    private Button BtnListaId = new Button();

    @FXML
    private Button BtnListarMaq;

    private ObservableList<Client> myClientComboBoxData = FXCollections.observableArrayList();

    // referencia a aplicacao principal
    private MainApp mainapp;

    //FIXME
    @Autowired
    ClientRepo clientRepo;



        public MonitorPjeController() {

    }

    @FXML
    private void initialize() {
        audId.setCellValueFactory(cellData -> cellData.getValue().processoProperty());
        dataId.setCellValueFactory(cellData -> cellData.getValue().dataProperty());
        assId.setCellValueFactory(cellData -> cellData.getValue().assinadoProperty());

        Client client = new Client("teste123", "21 vt");

        // sets the combobox options
        myClientComboBoxData.add(client);
        myClientComboBoxData.add(new Client("cta122223", "10 vt"));
        myClientComboBoxData.add(new Client("cta122223", "17 vt"));
        myClientComboBoxData.add(new Client("cta120337", "18 vt"));

        clientcomboBox.setItems(myClientComboBoxData);

        clientcomboBox.setCellFactory((comboBox) -> {
            return new ListCell<Client>() {
                @Override
                protected void updateItem(Client item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null || empty) {
                        setText(null);
                    } else {
                        setText(item.getVara());
                    }
                }
            };
        });

        clientcomboBox.setConverter(new StringConverter<Client>() {

            @Override
            public String toString(Client maquina) {
                if (maquina == null) {
                    return null;
                } else {
                    return maquina.getVara();
                }
            }

            @Override
            public Client fromString(String string) {
                // TODO Auto-generated method stub
                return null;
            }
        });

    }

    public void setMainApp(MainApp mainApp) {
        this.mainapp = mainApp;

        audTable.setItems(mainApp.getAudData());

    }


    // FIXME nao salva
    // TODO em caso de erro enviar msg de erro
    @FXML
    public void mouseEvent() {
        Client client = new Client(1, "cta 12344", "18 vt");
        clientRepo.save(client);

    }

    public ObservableList<Client> getMyClientComboBoxData() {
        return myClientComboBoxData;
    }

    public void setMyClientComboBoxData(ObservableList<Client> myClientComboBoxData) {
        this.myClientComboBoxData = myClientComboBoxData;
    }

}

正如@minus指出的那样,我创建了一个springBoot EntryPoint,但应用程序启动并退出...我从未看到过javaFX视图。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Lazy;


@SpringBootApplication
public class LaunchClass {

    public static void main(String[] args) {
        SpringApplication.run(MainApp.class, args);

    }

}

2 个答案:

答案 0 :(得分:2)

你想要达到的目标并不像看起来那么容易。

您可以开始阅读this articlethis,然后继续阅读。

事实上,将javafx和Spring + Hibernate结合起来有几个问题。

最大的问题是范围

Spring和Hibernate大部分都是在Web应用程序中开发的,这些应用程序具有一些定义明确的范围(应用程序,会话,请求)。

JavaFX没有固有的范围,您必须创建自定义范围并进行管理。

同样,Spring和JafaFX FXMLLoader都应该在同一个对象(控制器)中注入字段,这是一个非常重要的细节。

最后但并非最不重要的是管理和确定Hibernate会话的范围。

这是很多工作,我知道这是因为我做了一些与WELD + JavaFX非常相似的事情。

答案 1 :(得分:1)

我终于做到了!       按照@jewelsea给出的提示,我研究了由Felix Roske创建的框架。它花了一些代码重构,主要是在控制器部分。

App.java     进口......

@SpringBootApplication
public class App extends AbstractJavaFxApplicationSupport {

    /**
     * Note that this is configured in application.properties
     */
    @Value("${app.ui.title:PJe Midia Monitor}")//
    private String windowTitle;



    public static void main(String[] args) {

        launchApp(App.class, VisaoView.class, args);
    }

}

2-视图类

imports ....


@FXMLView(value = "view/visao.fxml")
public class VisaoView extends AbstractFxmlView {

}

3-控制器

imports ....

@FXMLController
public class MonitorPjeController {

    @FXML
    private TableView<Audiencia> audTable;

    @FXML
    private TableColumn<Audiencia, String> audId;

    @FXML
    private TableColumn<Audiencia, LocalDate> dataId;

    @FXML
    private TableColumn<Audiencia, String> assId;

    @FXML
    private ComboBox<Client> clientcomboBox;

    @FXML
    private Button BtnListaId = new Button();

    @FXML
    private Button BtnListarMaq;

    @FXML
    private ObservableList<Client> myClientComboBoxData = FXCollections.observableArrayList();

    @FXML
    private ObservableList<Audiencia> audData = FXCollections.observableArrayList();

    @Autowired
    private ClienteRepo clienteRepo;

    @FXML
    private void initialize() {

        populaTabela();

        Client client = new Client("teste123", "21 vt");

        // sets the combobox options
        myClientComboBoxData.add(client);
        myClientComboBoxData.add(new Client("cta122223", "10 vt"));
        myClientComboBoxData.add(new Client("cta122223", "17 vt"));
        myClientComboBoxData.add(new Client("cta120337", "18 vt"));

        clientcomboBox.setItems(myClientComboBoxData);

        clientcomboBox.setCellFactory((comboBox) -> {
            return new ListCell<Client>() {
                @Override
                protected void updateItem(Client item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null || empty) {
                        setText(null);
                    } else {
                        setText(item.getVara());
                    }
                }
            };
        });

        clientcomboBox.setConverter(new StringConverter<Client>() {

            @Override
            public String toString(Client maquina) {
                if (maquina == null) {
                    return null;
                } else {
                    return maquina.getVara();
                }
            }

            @Override
            public Client fromString(String string) {
                // TODO Auto-generated method stub
                return null;
            }
        });

    }


    public void populaTabela() {
        audData.add(new Audiencia("12123123213213", LocalDate.now(), "sim"));
        audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
        audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
        audData.add(new Audiencia("55555555555555", LocalDate.now(), "sim"));
        audData.add(new Audiencia("6666666666666", LocalDate.now(), "nao"));
        audData.add(new Audiencia("7777777777777", LocalDate.now(), "nao"));
        audData.add(new Audiencia("8888888888888", LocalDate.now(), "sim"));
        audData.add(new Audiencia("99999999999999", LocalDate.now(), "nao"));
        audData.add(new Audiencia("100010110101001", LocalDate.now(), "nao"));


        audTable.setItems(getAudData());

        audId.setCellValueFactory(cellData -> cellData.getValue().processoProperty());
        dataId.setCellValueFactory(cellData -> cellData.getValue().dataProperty());
        assId.setCellValueFactory(cellData -> cellData.getValue().assinadoProperty());
    }

    public ObservableList<Audiencia> getAudData() {
        return audData;
    }

    public void setAudData(ObservableList<Audiencia> audData) {
        this.audData = audData;
    }

}