I have been experimenting with the FXMLLoader
and using the setControllerFactory
method using a custom Callback<P,R>
implementation.
The ORACLE documentation says the following:
An implementation might return a null value to indicate that it does not or cannot create a controller of the given type; in this case, the default controller construction mechanism will be employed by the loader.
The result I want to achieve is that I can use a dependency injection framework to create any controllers that require parameters but I will let the FXMLLoader
load any controllers that do not require parameters.
So if I have the following simple FXML file which uses the ViewController
class which accepts no parameters...
<StackPane fx:id="pane"
xmlns:fx="http://javafx.com/fxml"
fx:controller="my.package.ViewController">
</StackPane>
and I use the following simple controller factory implementation to signal to the FXMLLoader
that I want it to manage the construction of the controller in this case...
loader.setControllerFactory(new Callback<Class<?>, Object>(){
@Override
public Object Call(Class<?> type) {
return null; // Let the FXMLLoader handle construction...
}
});
after calling the load()
method my Initialise method in the ViewController
class is never called (I have verified this with a breakpoint).
If I change my controller factory implementation to return an instance of the ViewController
class then everything works as expected.
Can anyone help me to clear up my confusion? Am I using the Callback
interface incorrectly or is the ORACLE documentation incorrect?
getController(Class<?> type)
in there, while it iscall(Class<?> param)
in 2.2. So the behavior may also be changed, try with version 2.1. – SquabsetController(controllerFactory.call(type));
. So it maybe the same in 2.2. Just curious which DI framework are you using? – SquabFXMLLoader
and the controller factory callback. – Bonbonnull
, instead, returntype.newInstance();
. – Celeski