Java Magazine, Sept/Oct 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 58 javafx FXML view The FXMLLoader instantiates the controller automatically and thats OK most of the time However what if you need the controller to be instantiated in a diferent way Perhaps you have additional customizations that need to be done before all @ FXML annotated elements have been injected Dont worry you can use a DI framework for this task The next section shows how to do this JSR 330 and Dependency Injection JSR 330 defines the basic principles for performing DI in a Java program There are a handful of implementations out there For simplicitys sake I wont show the setup of a specific DI framework but rather Ill use the standard API provided by JSR 330 for the following examples Lets redo the previous example using a data provider mechanism instead of inserting data into the ListView using a simple for loop This data provider mechanism is defined using an interface such as this sample DataProvider java package sample import java util Collection public interface DataProvider T Collection T getData Now youre free to implement this interface in any way required by the application such as grabbing the data from a file or a database connection via JDBC or other means The updated controller class now looks like this sample AppController java package sample
You must have JavaScript enabled to view digital editions.