title: How to wire multiple fxml controllers using spring dependency-injection?
body: <p>I read few spring tutorials with basic examples, and I am a bit confused on how to wire up things properly.</p>
  
  <p>The trouble is, I wanted to use application context to pull singleton controller references, but I read on few other topics that application context should not be accessed directly unless it is absolutely necessary. I think I should use constructor to instantiate reference I want, but here things get all blurry for me.</p>
  
  <p>I have javafx application with several fxml files. I have one main fxml and other are loaded dynamically inside main.</p>
  
  <p>I'll use simplified code for example with two fxml controllers, <strong>MainController.java</strong> (for main fxml) and <strong>ContentController.java</strong> (for content fxml)</p>
  
  <p>The idea is that content fxml has TabPane, and main fxml has button which opens new tab in TabPane on ContentController.</p>
  
  <p>I am currently doing something like this</p>
  
  <p>bean xml:
      </p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
  &lt;bean id="contentController"
  class="ContentController"
  scope="singleton" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>MainControler:</p>
  
  <pre><code>public class MainOverlayControler {
  
  ApplicationContext context;
  
  @FXML
  private BorderPane borderPane;
  
  @FXML
  private void initialize() {
      loadContentHolder();
  }
  @FXML
  private Button btn;
  
  @FXML
  private void btnOnAction(ActionEvent evt) {
      ((ContentController)context.getBean("contentController")).openNewContent();
  }
  
  private void loadContentHolder() {
      //set app context
      context = new ClassPathXmlApplicationContext("Beans.xml");
  
      Node fxmlNode;
      FXMLLoader fxmlLoader = new FXMLLoader();
      fxmlLoader.setController(context.getBean("contentController"));
  
      try {
      fxmlNode = (Node)fxmlLoader.load(getClass().getResource("Content.fxml").openStream());
      borderPane.setCenter(fxmlNode);
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>ContentController:</p>
  
  <pre><code>public class ContentController {
  @FXML
  private TabPane tabPane;
  
  public void openNewContent() {
      Tab newContentTab = new Tab();
      newContentTab.setText("NewTab");
      tabPane.getTabs().add(newContentTab);
      }
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>public class MainFX extends Application {
  @Override
  public void start(Stage primaryStage) {
      try {
          FXMLLoader fxmlLoader = new FXMLLoader();
          Parent root = (Parent) fxmlLoader.load(getClass().getResource("main.fxml").openStream());
          Scene scene = new Scene(root);
          primaryStage.setScene(scene);
          primaryStage.show();
      } catch(Exception e) {
      e.printStackTrace();
      }
  public static void main(String[] args) {
      launch(args);
      }
  }
  </code></pre>
  
  <p><strong>Question:</strong>
  I would like to know how to do same thing with constructor DI, or some other way if I misunderstood the concept.</p>
  
  <p>I need to be able to call "openNewContent" on singleton instance of "ContentController" from multiple other controllers as well.</p>