【问题标题】:springboot + hibernate + javaFX春季启动+休眠+ javaFX
【发布时间】:2017-02-20 16:04:17
【问题描述】:

我被困在这几天,没有任何进展。 这是交易..我正在构建一个使用 springboot(主要是使用 rawRepository)和 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 为空,不知道为什么......在正常执行中它应该有这样的东西:

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 入口点,但应用程序启动并退出......我从未看到 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);

    }

}

【问题讨论】:

  • 我看不到你的 Spring Boot 入口点。
  • 如果我明白你想要什么,你的意思是这个? public static void main(String[] args) { Application.launch(args); }
  • 这是标准的 JavaFX 入口点,但我看不到 Spring 上下文启动。
  • 正如你所指出的,应用程序确实有一个 spring 入口点......所以我创建了一个(编辑了原始帖子 LaunchClass.java)。但是现在,应用程序运行......并退出。我在这里迷路了。大声笑
  • FWIW,我从未尝试过。有一个springboot-javafx-support 库和一个Spring Boot Java FX integration example。我建议你试试看。

标签: java hibernate spring-boot


【解决方案1】:

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

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

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

最大的问题是范围

Spring 和 Hibernate 都主要被开发用于在具有一些明确定义的范围(应用程序、会话、请求)的 Web 应用程序中工作。

JavaFX 没有固有作用域,您必须创建自定义作用域并对其进行管理。

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

最后但并非最不重要的是管理和限定 Hibernate 会话的问题。

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

【讨论】:

  • 先生,请您回复...我会尝试看看是否有更好的方法来实现这一点,并在稍后发布一些反馈!
  • 我解决了这个问题,我的意思是......有点......重构了整个应用程序。删除了spring boot,单独使用javaFX + hibernate,它的工作。但是,在不久的将来,我会尝试jewelsea 引用的教程。西雅
  • 好,从基础开始总是明智的做法,然后,当你有坚实的基础可以建立时,再迈出一步。
  • 正如我所说,我确实重构了项目,现在项目正在运行完整的 springboot、hibernate 和 javaFX
【解决方案2】:

我终于做到了! 根据@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;
    }

}

【讨论】:

    猜你喜欢
    • 2015-06-30
    • 2020-05-17
    • 2018-04-25
    • 2020-10-21
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2016-03-24
    • 2013-01-11
    相关资源
    最近更新 更多