更新时间:2022-05-15 02:59:42
简短答案:不,不是(从JavaFX 2.x和8.0开始).它可能是未来版本(JFX> 8)
Short answer: No, it is not (as of JavaFX 2.x and 8.0). It may be in a future version (JFX >8)
详细答案: FXMLLoader当前不设计为充当模板提供程序,该模板提供程序一遍又一遍地实例化同一项目.而是要成为大型GUI的一次性加载程序(或对其进行序列化).
Long answer: The FXMLLoader is currently not designed to perform as a template provider that instantiates the same item over and over again. Rather it is meant to be a one-time-loader for large GUIs (or to serialize them).
性能很差,因为在每次调用load()
时,取决于FXML文件,FXMLLoader必须通过反射来查找类及其属性.这意味着:
The performance is poor because depending on the FXML file, on each call to load()
, the FXMLLoader has to look up the classes and its properties via reflection. That means:
对于代码中完成的对同一FXML文件的load()
的后续调用,当前也没有任何改进.这意味着:不缓存找到的类,不缓存BeanAdapters等等.
There is also currently no improvement for subsequent calls to load()
to the same FXML file done in the code. This means: no caching of found classes, no caching of BeanAdapters and so on.
但是,通过为FXMLLoader实例设置自定义类加载器,可以解决第1步的问题:
There is a workaround for the performance of step 1, though, by setting a custom classloader to the FXMLLoader instance:
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class MyClassLoader extends ClassLoader{
private final Map<String, Class> classes = new HashMap<String, Class>();
private final ClassLoader parent;
public MyClassLoader(ClassLoader parent) {
this.parent = parent;
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
Class<?> c = findClass(name);
if ( c == null ) {
throw new ClassNotFoundException( name );
}
return c;
}
@Override
protected Class<?> findClass( String className ) throws ClassNotFoundException {
// System.out.print("try to load " + className);
if (classes.containsKey(className)) {
Class<?> result = classes.get(className);
return result;
} else {
try {
Class<?> result = parent.loadClass(className);
// System.out.println(" -> success!");
classes.put(className, result);
return result;
} catch (ClassNotFoundException ignore) {
// System.out.println();
classes.put(className, null);
return null;
}
}
}
// ========= delegating methods =============
@Override
public URL getResource( String name ) {
return parent.getResource(name);
}
@Override
public Enumeration<URL> getResources( String name ) throws IOException {
return parent.getResources(name);
}
@Override
public String toString() {
return parent.toString();
}
@Override
public void setDefaultAssertionStatus(boolean enabled) {
parent.setDefaultAssertionStatus(enabled);
}
@Override
public void setPackageAssertionStatus(String packageName, boolean enabled) {
parent.setPackageAssertionStatus(packageName, enabled);
}
@Override
public void setClassAssertionStatus(String className, boolean enabled) {
parent.setClassAssertionStatus(className, enabled);
}
@Override
public void clearAssertionStatus() {
parent.clearAssertionStatus();
}
}
用法:
public static ClassLoader cachingClassLoader = new MyClassLoader(FXMLLoader.getDefaultClassLoader());
FXMLLoader loader = new FXMLLoader(resource);
loader.setClassLoader(cachingClassLoader);
这大大提高了性能.但是,步骤2没有解决方法,因此这仍然可能是个问题.
This significantly speeds up the performance. However, there is no workaround for step 2, so this might still be a problem.
但是,在官方JavaFX jira中已经有对此功能的请求.您很高兴能够支持此请求.
However, there are already feature requests in the official JavaFX jira for this. It would be nice of you to support this requests.
链接:
FXMLLoader应该能够缓存导入和load()调用之间的属性:
https://bugs.openjdk.java.net/browse/JDK-8090848
FXMLLoader should be able to cache imports and properties between to load() calls:
https://bugs.openjdk.java.net/browse/JDK-8090848
将setAdapterFactory()添加到FXMLLoader:
https://bugs.openjdk.java.net/browse/JDK-8102624
add setAdapterFactory() to the FXMLLoader:
https://bugs.openjdk.java.net/browse/JDK-8102624