我正在用SpringBoot+SpringJDBC+SQLite开发一个JavaFX CRUD应用程序。我正在使用Eclipse IDE。
故事。我正在以StepByStep的方式开发这个应用程序。而且我用老式的JDBC连接实现了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
到目前为止,我对这个错误的猜测是,配置没有正常工作 - 我想是这样。我需要建议和帮助我改善这个问题。
FXMLLoader创建控制器实例的默认行为是简单的实例化,这意味着没有依赖关系被注入。要想获得注入,你反而需要Spring ApplicationContext来管理实例的创建。
Josh Long发布了一个Spring Tips的装置,演示了如何一步步进行。https:/spring.ioblog20190116spring-tips-javafx。
然而,这是一个相当手动和重复的过程。这就是 https:/github.comrgielenjavafx-weaver。 试图解决。你可以在这里找到一个文档和示例代码。https:/github.comrgielenjavafx-weavertreemastersamplesspringboot-sample。
声明:我创建了这个图书馆,所以我有偏见:)