如何将Gluon View与Spring Boot控制器绑定并注入showView至addViewFactory
addViewFactory Got it, let's fix the separation between your Spring-managed controller and Gluon view lifecycle. The core idea is to tie your controller's showView() logic directly into Gluon's view factory, so view creation and controller behavior are no longer independent.
Step 1: Refactor Your MainController to Handle View Creation
First, adjust your controller to either build the Gluon View instance directly or encapsulate the showView() logic within a method that returns the view. This lets Spring manage the controller, while the controller drives view creation.
@Component public class MainController { // Replace your existing showView() with a method that creates/configures the view public View buildHomeView() { BasicView homeView = new BasicView(); // Add your original showView() logic here: // Example: Set view content, bind data, attach event listeners homeView.getChildren().add(new Label("Hello from Spring-Managed Controller!")); return homeView; } }
If your showView() handles navigation instead of view setup, you can inject the MobileApplication into the controller to manage view switching:
@Component public class MainController { private final MobileApplication gluonApplication; // Inject the Gluon app instance (Spring will manage this since RxGluonApp is a bean) public MainController(MobileApplication gluonApplication) { this.gluonApplication = gluonApplication; } public View buildHomeView() { BasicView homeView = new BasicView(); // Add navigation logic from your original showView() Button navigateBtn = new Button("Go to Another View"); navigateBtn.setOnAction(e -> gluonApplication.switchView("ANOTHER_VIEW")); homeView.getChildren().add(navigateBtn); return homeView; } }
Step 2: Update RxGluonApp to Use the Controller in addViewFactory
Modify the init() method to pull the controller from the Spring context and use it as the source for your view in addViewFactory. This replaces the separate controller.showView() call and direct BasicView instantiation.
@ComponentScan(basePackages = {"toto", "toto.core"}) @SpringBootApplication @EnableFeignClients public class RxGluonApp extends MobileApplication { private ConfigurableApplicationContext context; public static void main(String[] args) { Application.launch(RxGluonApp.class, args); } @Override public void init() { context = SpringApplication.run(RxGluonApp.class); final MainController controller = context.getBean(MainController.class); // Inject the controller's view-building logic directly into the factory addViewFactory(HOME_VIEW, controller::buildHomeView); // No need for a separate controller.showView() call anymore! // The view will be created and displayed when Gluon initializes the HOME_VIEW } @Override public void postInit(Scene scene) { Swatch.AMBER.assignTo(scene); ((Stage) scene.getWindow()).getIcons().add(new Image(RxGluonApp.class.getResourceAsStream("/icon.png"))); } @Override public void stop() { Platform.exit(); context.stop(); } }
Why This Works
- Your Spring-managed controller now drives view creation, so all business logic, data binding, and navigation are tied directly to the view's lifecycle.
- No more disconnected calls to
controller.showView()andaddViewFactory—the view factory uses the controller as the single source of truth for the view's setup. - Any Spring dependencies your controller needs (like Feign clients) will be properly injected, and the view will inherit that context.
内容的提问来源于stack exchange,提问作者Hilmi Reda

