Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8';s使用时缺少参数::_Java_Spring_Spring Mvc_Java 8_Method Reference - Fatal编程技术网

Java 8';s使用时缺少参数::

Java 8';s使用时缺少参数::,java,spring,spring-mvc,java-8,method-reference,Java,Spring,Spring Mvc,Java 8,Method Reference,Java8的::仅通过方法名启用方法引用 protected Object loadBeanController(String url) throws IOException { loader = new FXMLLoader(getClass().getResource(url)); ApplicationContext context = MyProjectClass.getApplicationContext(); loader.setControllerFact

Java8的
::
仅通过方法名启用方法引用

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

    loader.setControllerFactory(context::getBean);

    return loader.getController();
}
但是,根据
getBean
的规定,getBean不接受空参数-需要一些参数值:

getBean(字符串名称)
getBean(字符串名称,类requiredType)
getBean(字符串名,对象[]args)


这是如何解决的?

JavaFX的
fxmloader
方法将a作为参数

这是一个功能接口,其唯一方法是调用一个参数并返回一个结果。在本例中,参数的类型是
回调

所以,实际上,你引用的方法都不会被调用。将调用的是(此方法仅在Spring3.0之后存在,因此您不会在链接的2.5.4参考中看到它)

可以像这样重写方法表达式以使其更清楚:

loader.setControllerFactory(c -> context.getBean(c));
这里,
c
将具有类型
Class
,因为声明了
setControllerFactory
的参数

作为旁注,所有内容都将编译,因为
setControllerFactory
期望回调的结果为
Object
类型,因此
context.getBean(c)
的结果将始终匹配