There you have it. The controller instance is fetched from the DI container. This fetch operation varies from framework to framework and, thus, is omitted from the example. You can even apply a lambda expression to define a simple ControllerFactory. Given that the AppController class is now managed by the DI container, you can leverage other features such as methods annotated with @PostConstruct, thus allowing further customizations on the controller. Just remember that any methods annotated with @PostConstruct will be invoked before any @FXMLannotated fields are injected into the controller instance. As a side efect of this setup, the value of fx:controller becomes irrelevant. However, it is still a good idea to specify the actual type of the controller supplied by the DI container, because your IDE will give you hints on the types and names of widgets that can be injected using the @FXML annotations, as well as the names of event handlers supplied by the controller. As an alternative, you could set the controller property directly in FXMLLoader, but I decided to show the factory approach, because that might come in handy for lazily calculating the value.
Partager