Im trying to use the setControllerFactory() method in FXMLLoader for dependency injection, but stumbled upon an issue that looks like a bug to me. The controller factory works fine when loading the fxml from an input stream, but does not get invoked when loading from an uri. I wrote a simple unit test to demonstrate the issue:
public class FXMLLoaderTest {
private FXMLLoader loader;
@Test
public void testFXMLFactory() throws IOException {
FXMLLoader loader = new FXMLLoader();
Callback<Class<?>, Object> callback = new Callback<Class<?>, Object>() {
@Override
public Object call(Class<?> aClass) {
System.out.println("Controller factory called");
return new String("Works");
}
};
loader.setControllerFactory(callback);
// This makes the test fail
//Object node = loader.load(getClass().getClassLoader().getResource("controllerTest.fxml"));
// While this works as expected
Object node = loader.load(getClass().getClassLoader().getResourceAsStream("controllerTest.fxml"));
assertThat(loader.getController(), is(equalTo((Object) "Works")));
}
}
Im using javafx 2.2 shipped with JDK 7u21. Am I missing something or should I file an issue for this?