title: JavaFX custom controller factory
body: <p>I have been experimenting with the <code>FXMLLoader</code> and using the <code>setControllerFactory</code> method using a custom <code>Callback&lt;P,R&gt;</code> implementation.</p>
  
  <p><a href="http://docs.oracle.com/javafx/2/fxml_get_started/whats_new.htm#CHDDDDCC" rel="noreferrer">The ORACLE documentation</a> says the following:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>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 <code>FXMLLoader</code> load any controllers that do not require parameters.</p>
  
  <p>So if I have the following simple FXML file which uses the <code>ViewController</code> class which accepts no parameters...</p>
  
  <pre><code>&lt;StackPane fx:id="pane"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="my.package.ViewController"&gt;
  &lt;/StackPane&gt;
  </code></pre>
  
  <p>and I use the following simple controller factory implementation to signal to the <code>FXMLLoader</code> that I want it to manage the construction of the controller in this case...</p>
  
  <pre><code>loader.setControllerFactory(new Callback&lt;Class&lt;?&gt;, Object&gt;(){
      @Override
      public Object Call(Class&lt;?&gt; type) {
          return null; // Let the FXMLLoader handle construction...
      }
  });
  </code></pre>
  
  <p>after calling the <code>load()</code> method my Initialise method in the <code>ViewController</code> class is never called (I have verified this with a breakpoint).</p>
  
  <p>If I change my controller factory implementation to return an instance of the <code>ViewController</code> class then everything works as expected.</p>
  
  <p>Can anyone help me to clear up my confusion? Am I using the <code>Callback</code> interface incorrectly or is the ORACLE documentation incorrect?</p>