11

Java 8's :: enables method referencing via method name alone.

protected Object loadBeanController(String url) throws IOException {
    loader = new FXMLLoader(getClass().getResource(url));
    ApplicationContext context = MyProjectClass.getApplicationContext();

    loader.setControllerFactory(context::getBean);

    return loader.getController();
}

But, however, according to BeanFactory Interface (Spring) getBean() getBean does not take empty parameters - Some parameter values are expected:

getBean(String name)
getBean(String name, Class requiredType)
getBean(String name, Object[] args)

How is this resolved?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142

1 Answers1

15

JavaFX's FXMLLoader method setControllerFactory takes a Callback as parameter.

This is a functional interface whose sole method is call taking one parameter and returning one result. In this case, the type of the argument is Callback<Class<?>, Object>. So the lambda expression expects an argument of type Class<?>.

So, actually, none of the methods you cited will be called. What will be called is getBean(Class<T> requiredType) (this method only exists since Spring 3.0 so you won't see it in your linked 2.5.4 reference).

It is possible to rewrite the method expression like this to make this more clear:

loader.setControllerFactory(c -> context.getBean(c));

Here, c will have the type Class<?> because of the declared parameter of setControllerFactory.

As a side note, everything will compile because setControllerFactory expects the result of the callback to be of type Object so the result of context.getBean(c) will always match.

Tunaki
  • 132,869
  • 46
  • 340
  • 423