Controllerare singletons, so you need some way to access the instance of a specific component. Tornado FX supports dependency injection, but you can also lookup components with the
find, the component corresponding to the given class is looked up in a global component registry. If it did not exist prior to the call, it will be created and inserted into the registry before the function returns.
injectdelegate instead. This is a lazy mechanism, so the actual instance will only be created the first time you call a function on the injected resource. Using
injectis always prefered, as it allows your components to have circular dependencies.
DIContainerinterface when you start your application. Let's say you have a Guice module configured with a fictive
HelloService. Start Guice in the
initblock of your
Appclass and register the module with TornadoFX:
The DIContainer implementation is configured to delegate lookups to
MyModule, use the
didelegate instead of the
didelegate accepts any bean type, while
injectwill only allow beans of type
ScopedInstance, which includes TornadoFX's
Controller. This keeps a clean separation between your UI beans and any beans configured in the external dependency injection framework.
ApplicationContextis done as follows:
FX.dicontainer. Now you can inject Spring beans like this:
id. This can be done in tornadoFX too:
getInstanceuses both the type of the bean and the id of the bean. Instantiating a bean is down as: