【问题标题】:How to inject objects which are not annotated Spring classes如何注入没有注释的 Spring 类的对象
【发布时间】:2017-12-09 09:52:28
【问题描述】:

假设我有一个 JavaFx 类和一个 ViewState 类,它们需要在 start 方法中创建 stage 的引用。我如何能够自动装配这种依赖关系?我知道 Stage.class 没有被注释为 @Component 所以 Spring 无法检测到 duch @Bean

@SpringBootApplication
@ComponentScan({"controller","service","dao","javafx.stage.Stage"})
@EntityScan( basePackages = {"Model"})
@Import({ SpringConfig.class})
public class JavaFXandSpringBootTestApplication extends Application{

    private ApplicationContext ctx;

    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage stage) throws Exception {
    ViewState viewState = ctx.getBean(ViewState.class);
}

ViewState 类:

@Componenet
public class ViewState {

@Autowired
private ApplicationContext ctx;
private Stage stage;

@Autowired
public ViewState(Stage stage)
{
    this.stage = stage;
}
}

编译器按摩:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javafx.stage.Stage' available: expected at least 1

【问题讨论】:

    标签: spring javafx


    【解决方案1】:

    您可能根本不想这样做:您的 ViewState 类似乎是某种模型类,因此它不应该引用 UI 元素(例如 Stages)。

    不过,为了完整起见,这里有一个使用ConfigurableApplicationContext.getBeanFactory().registerResolvableDependency(...) 的示例。请注意,由于在注册阶段之前您将无法创建 View 对象,因此您需要创建 View bean @Lazy

    package example;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Lazy;
    import org.springframework.stereotype.Component;
    
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    @Component
    @Lazy
    public class View {
    
        @Autowired
        public View(Stage stage) {
            Button close = new Button("Close");
            close.setOnAction(e -> stage.hide());
            StackPane root = new StackPane(close);
            Scene scene = new Scene(root, 400, 400);
            stage.setScene(scene);
            stage.show();
        }
    }
    
    package example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    import javafx.application.Application;
    import javafx.stage.Stage;
    
    @SpringBootApplication
    public class Main extends Application {
    
        private ConfigurableApplicationContext ctx ;
    
        @Override
        public void start(Stage primaryStage) {
            ctx = SpringApplication.run(Main.class);
            ctx.getBeanFactory().registerResolvableDependency(Stage.class, primaryStage);
            ctx.getBean(View.class);
        }
    
        @Override
        public void stop() {
            ctx.close();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-22
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 2021-05-26
      相关资源
      最近更新 更多