You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Gluon View与Spring Boot控制器绑定并注入showView至addViewFactory

Integrating Spring Boot Controller Logic into Gluon's 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() and addViewFactory—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:22:42