Spring boot 没有FXML的JavaFx和Spring引导

Spring boot 没有FXML的JavaFx和Spring引导,spring-boot,spring-mvc,javafx-8,Spring Boot,Spring Mvc,Javafx 8,spring和javafx实现的所有示例都显示了fxml的使用,我想回避这一点 然后是GUI类 @Component public class TestGui extends BorderPane { // here would be gui and necesery gui logic, also here would i use my services impls @Autowired PersonService personService; public TestG

spring和javafx实现的所有示例都显示了fxml的使用,我想回避这一点

然后是GUI类

@Component
public class TestGui extends BorderPane {
// here would be gui and necesery gui logic, also here would i use my services impls
    @Autowired
    PersonService personService;

  public TestGui() {
    HBox hbox = new HBox();
    Button btn = new Button();
    Label lbl = new Label();
    hbox.getChildren().addAll(btn,lbl);
    setCenter(hbox);

    btn.setOnAction(e->lbl.setText(personService.getSomething()));
  }
最后是一个将gui返回到主类工作台的类

public class TestModule extends WorkbenchModule {
        //it just serves that TestGui class to the main classes workbench
  public TestModule() {
    super("Hello World", MaterialDesignIcon.HUMAN_HANDSUP);
  }

  @Override
  public Node activate() {
    return new TestGui();
  }

}
我假设我必须将TestGui类注释为组件,以便以后的spring知道如何管理它。 我不想让其他类知道它只是Dao、POJO和服务及其实现

在我的AppConfig类中,我想告诉spring扫描模块包中的bean

通常,当他们使用SpringBoot时,他们使用Fxml,所以当他们使用Fxml加载程序加载文件时,他们会这样做。是否有任何方法可以重新制作此文件以帮助我完成所需的工作?我不需要或计划使用fxml,所以我只想加载JavaClass 私有最终资源包ResourceBundle; 私有最终应用程序上下文上下文

@Autowired
public AppConfig(ApplicationContext context, ResourceBundle resourceBundle) {
    this.resourceBundle = resourceBundle;
    this.context = context;
}

public Parent load(String fxmlPath) throws IOException {
    FXMLLoader loader = new FXMLLoader();
    loader.setControllerFactory(context::getBean); //Spring now FXML Controller Factory
    loader.setResources(resourceBundle);
    loader.setLocation(getClass().getResource(fxmlPath));
    return loader.load();
}
除了pom中的spring和mysql依赖项之外,我还使用它将gui类加载到主阶段


我知道我要问的是一个非常广泛的问题,但我还没有找到一个没有fxml的spring boot和javafx的示例。

为WorkbenchModule添加注释

@Component
public class TestModule extends WorkbenchModule {
  private Node node;

  public TestModule() {
    super("Test module", MaterialDesignIcon.DATABASE);
    TextField id = new TextField();
    Button load = new Button("Test");
    node = new FlowPane(id, load);
  }

  @Override
  public Node activate() {
    return node;
  }

}
  • 注入到应用程序类中
  • 通过注释initWorkbench方法创建工作台bean
  • 在init方法中初始化Spring
  • 创建场景时,请确保使用Spring使用springContext.getBean(Workbench.class)创建的工作台bean
  • *

    @Component
    public class TestModule extends WorkbenchModule {
      private Node node;
    
      public TestModule() {
        super("Test module", MaterialDesignIcon.DATABASE);
        TextField id = new TextField();
        Button load = new Button("Test");
        node = new FlowPane(id, load);
      }
    
      @Override
      public Node activate() {
        return node;
      }
    
    }
    
    @SpringBootApplication
    @EnableScheduling
    public class TestApplication extends Application {
      private ConfigurableApplicationContext springContext;
      // 1
      @Autowired
      private TestModule testModule;
    
      public static void main(String[] args) {
        launch(args);
      }
    
      // 3
      @Override
      public void init() throws Exception {
        springContext = SpringApplication.run(OssApplication.class);
      }
    
      @Override
      public void start(Stage primaryStage) {
        // 4
        Workbench workbench = springContext.getBean(Workbench.class);
        Scene scene = new Scene(workbench);
    
        primaryStage.setScene(scene);
        primaryStage.setWidth(1000);
        primaryStage.setHeight(700);
        primaryStage.show();
        primaryStage.centerOnScreen();
      }
    
      @Override
      public void stop() {
        springContext.close();
      }
    
      // 2
      @Bean
      public Workbench initWorkbench() {
        Workbench workbench = Workbench.builder(
                testModule
                )
                .build();
        return workbench;
      }
    }