JavaFX实战 -- 02. JavaFX与Spring 集成

主要使用 FXMLLoader的 setControllerFactory()指定控制器(或者说是Bean)工厂来与Spring集成。

当然还需要使用@Controller对FXML的Controller类进行注解


public class Main extends Application {
	
	private ApplicationContext applicationContext;

	@Override
	public void start(Stage primaryStage) {
		System.setProperty("spring.profiles.active", "prod");		
		applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
		
		try {
			FXMLLoader loader = new FXMLLoader();
			loader.setControllerFactory(applicationContext::getBean);
			loader.setLocation(getClass().getResource("/view/Main.fxml"));
			
			BorderPane root = (BorderPane)loader.load();
			Scene scene = new Scene(root, 800, 600);
			scene.getStylesheets().add(getClass().getResource("/view/application.css").toExternalForm());
			
			primaryStage.setScene(scene);
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}




你可能感兴趣的:(Java,JavaFX,Spring)