【发布时间】: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