简体   繁体   English

依赖注入和 JavaFX

[英]Dependency Injection and JavaFX

Since JavaFX runtime wants to instantiate my Application object and all of my controller objects, how do I inject dependencies into these objects?由于 JavaFX 运行时想要实例化我的 Application 对象和我的所有控制器对象,我如何将依赖项注入这些对象?

If objects were instantiated by a DI framework, like Spring, the framework would wire up all the dependencies.如果对象由 DI 框架(如 Spring)实例化,则该框架将连接所有依赖项。 If I was instantiating the objects manually, I would provide the dependencies through constructor parameters.如果我手动实例化对象,我将通过构造函数参数提供依赖项。 But what do I do in a JavaFX application?但是我在 JavaFX 应用程序中做什么?

Thanks!谢谢!

You can specify a controller factory for the FXMLLoader .您可以为FXMLLoader指定控制器工厂 The controller factory is a function that maps the controller class to an object (presumably, but not necessarily, an instance of that class) which will be used as the controller.控制器工厂是一个将控制器类映射到将用作控制器的对象(可能,但不一定是该类的实例)的函数。

So if you want Spring to create the controller instances for you, this can be as simple as:因此,如果您希望 Spring 为您创建控制器实例,这可以很简单:

ApplicationContext context = ... ;

FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml"));
loader.setControllerFactory(context::getBean);
Parent root = loader.load();
SomeController controller = loader.getController(); // if you need it...
// ...

And now the FXMLLoader will create controller instances for a Class<?> c by calling context.getBean(c);现在FXMLLoader将通过调用context.getBean(c);Class<?> c创建控制器实例context.getBean(c); . .

So, eg, you could have a configuration:所以,例如,你可以有一个配置:

@Configuration
public class AppConfig {

    @Bean
    public MyService service() {
        return new MyServiceImpl();
    }

    @Bean
    @Scope("prototype")
    public SomeController someController() {
        return new SomeController();
    }

    // ...
}

with

public class SomeController {

    // injected by FXMLLoader:
    @FXML
    private TextField someTextField ;

    // Injected by Spring:
    @Inject
    private MyService service ;

    public void initialize() {
        someTextField.setText(service.getSomeText());
    }

    // event handler:
    @FXML
    private void performAction(ActionEvent e) {
        service.doAction(...);
    }
}

If you're not using a DI framework, and you want to do the injection "by hand", you can do so, but it involves using quite a lot of reflection.如果您没有使用 DI 框架,并且您想“手动”进行注入,您可以这样做,但它涉及使用相当多的反射。 The following shows how (and will give you an idea of how much ugly work Spring is doing for you!):下面展示了如何(并将让您了解 Spring 为您做了多少丑陋的工作!):

FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml"));
MyService service = new MyServiceImpl();
loader.setControllerFactory((Class<?> type -> {
    try {
        // look for constructor taking MyService as a parameter
        for (Constructor<?> c : type.getConstructors()) {
            if (c.getParameterCount() == 1) {
                if (c.getParameterTypes()[0]==MyService.class) {
                    return c.newInstance(service);
                }
            }
        }
        // didn't find appropriate constructor, just use default constructor:
        return type.newInstance();
    } catch (Exception exc) {
        throw new RuntimeException(exc);
    }
});
Parent root = loader.load();
// ...

and then just do然后就做

public class SomeController {

    private final MyService service ;

    public SomeController(MyService service) {
        this.service = service ;
    }

    // injected by FXMLLoader:
    @FXML
    private TextField someTextField ;

    public void initialize() {
        someTextField.setText(service.getSomeText());
    }

    // event handler:
    @FXML
    private void performAction(ActionEvent e) {
        service.doAction(...);
    }
}

Finally, you might want to check out afterburner.fx , which is a very lightweight (in all the best ways) JavaFX-specific DI framework.最后,您可能想查看afterburner.fx ,这是一个非常轻量级(以所有最佳方式)JavaFX 特定的 DI 框架。 (It uses a convention-over-configuration approach, where you just match FXML file names to controller class names, and optionally CSS file names, and everything just works.) (它使用约定优于配置的方法,您只需将 FXML 文件名与控制器类名以及可选的 CSS 文件名相匹配,一切正常。)

i used setControllerFactory methode of FXMLLoader class to Sets the controller factory used by this serializer.我使用 FXMLLoader 类的 setControllerFactory 方法来设置此序列化程序使用的控制器工厂。

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext();
        ctx.register(AppConfig.class);
        ctx.refresh();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/sample.fxml"));
        loader.setControllerFactory(ctx::getBean);

        Parent root = loader.load();
        Controller controller = loader.getController();
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 925, 400));
        primaryStage.show();
    }

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

    }
}

then use @component to controller class然后使用@component 到控制器类

  @Component
        public class Controller {
            @Autowired
            private ItemController itemController;
            @FXML
            private TextField item;
            @FXML
            private TextField quantity;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM