【问题标题】:JavaFX SpringBoot SpringJDBC SQLite CRUD Application - configurationJavaFX SpringBoot SpringJDBC SQLite CRUD 应用程序 - 配置
【发布时间】:2020-06-11 23:41:13
【问题描述】:

我正在使用 SpringBoot + SpringJDBC + SQLite 开发 JavaFX CRUD 应用程序。我正在使用 Eclipse IDE。

故事: 我正在将此应用程序开发为 StepByStep 过程。我用 Old School JDBC Connection 实现了 JavaFX+SQLite CRUD 应用程序。但是在集成 SpringBoot + SpringJDBC 之后出现错误。我认为将应用程序配置传递给所有文件时出错。

主类

@SpringBootApplication
public class Main {

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

}

AND MyApplication.Class(没有注释)

public class MyApplication extends Application {

    protected ConfigurableApplicationContext applicationContext;

    @Override
    public void init() throws Exception {
        applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    }

    @Override
    public void stop() throws Exception {
        applicationContext.close();
        Platform.exit();
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("ExYouTub");
        stage.setOnCloseRequest(x -> {
            Platform.exit();
        });
        stage.setResizable(false);
        stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("../Sample.fxml"))));
        stage.show();
    }

}

AND AppConfig.class

@Configuration
@ConditionalOnClass(DataSource.class)
@Profile("sqlite")
@ComponentScan(basePackages= "com.fz")
@PropertySource("classpath:data/config.properties")
public class AppConfig {

    @Autowired
    Environment environment;

    private final String DB_URL = "fz.db.url";

    @Bean
    DataSource dataSource() {
        final DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setUrl(environment.getProperty(DB_URL));
        return driverManagerDataSource;
    }
}

AND SampleController.class

@Controller
public class SampleController implements Initializable {
//-- un-necessary lines are ignored to copy

    @Autowired
    @Qualifier("studentsDAOImpl")
    private StudentsDAO studentsDAO;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tableViewList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        setColumnProperties();
        loadStudentsDetails();
    }

    private void loadStudentsDetails() {
        studentsList.clear();
        studentsList.addAll(studentsDAO.getAllStudents()); // this is line 83
        tableViewList.setItems(studentsList);
    }
}

AND 错误报告

Caused by: java.lang.NullPointerException
    at com.fz.SampleController.loadStudentsDetails(SampleController.java:83)
    at com.fz.SampleController.initialize(SampleController.java:78)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    ... 17 more

到目前为止,我对这个错误的猜测是,配置无法正常工作——我想是的。我需要建议并帮助我改进。

【问题讨论】:

  • 我认为您需要将您的 SampleController 设为 Component 并使用 FxWeaver 它使用 Application 加载您的 FXML 视图上下文并在 Spring 容器中注册您的控制器。
  • 您需要以某种方式确保从 Spring 中检索控制器,而不是默认机制(FXMLLoader 直接实例化它)。我从未使用过 FXWeaver,这是一种解决方案;另一种方法是将控制器工厂设置为 Spring 应用程序上下文。见(除其他外)stackoverflow.com/questions/48290589/javafx-spring-boot-npe

标签: java sqlite spring-boot javafx spring-jdbc


【解决方案1】:

FXMLLoader 创建控制器实例的默认行为是简单地实例化它,这意味着没有依赖项被注入。要获得注入,您宁愿需要 Spring ApplicationContext 来管理实例创建。

Josh Long 发布了一篇 Spring Tips 文章,演示了如何逐步完成此操作:https://spring.io/blog/2019/01/16/spring-tips-javafx

但是,这是一个相当手动且重复的过程。这就是https://github.com/rgielen/javafx-weaver 试图解决的问题。您可以在此处找到文档和示例代码:https://github.com/rgielen/javafx-weaver/tree/master/samples/springboot-sample

免责声明:我创建了库,所以我有偏见:)

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 2018-11-03
    • 2019-08-19
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多