[REVIEW] Make controller instantiation customizable

Greg Brown greg.x.brown at Oracle.com
Tue Dec 13 11:22:39 PST 2011

This is a review request for RT-17268:


In FXML 2.0, the caller did not have any control over controller creation. This prevented an application from using a dependency injection system such as Guice or Spring to manage controller initialization. This change adds a ControllerFactory interface to facilitate delegation of controller construction:

public interface ControllerFactory {
    public Object getController(Class<?> type);

When an instance of ControllerFactory is provided to FXMLLoader, the loader will delegate controller construction to the factory. An implementation may return null 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. Implementations may also "recycle" controllers such that controller instances can be shared by multiple FXML documents. However, developers must be aware of the implications of doing this; primarily, that controller field injection should not be used in this case since it will result in the controller fields containing values from only the most recently loaded document.

More information about the openjfx-dev mailing list