在Java 9中加载类和资源 [英] Loading classes and resources in Java 9
问题描述
我正在阅读有关InfoQ的这篇文章引用Reinhold:
I was reading this article on InfoQ quoting Reinhold:
开发人员仍然可以使用Java 9中的Java类路径为Java
运行时搜索类和资源文件。只需使用
Java 9的模块,开发人员就不再需要类路径了。
Developers can still use the Java class path in Java 9 for the Java runtime to search for classes and resource files. It's just that with Java 9's modules, developers no longer need the class path.
所以现在我的问题是:什么是正确的Java 9方式来完成上面列出的任务?你如何动态加载,例如一个图像(没有摆弄相对路径)?
So now my question is: what is the proper Java 9 way to do the tasks listed above? How do you dynamically load e.g. an image (short of fiddling with relative paths)?
更有趣的是,如何检查一个班级是否可用并动态做出决定(例如检查杰克逊是否是可用,如果是的话,使用它进行JSON序列化,如果不使用其他东西)?
Even more interestingly, how would one check if a class is available and make a decision dynamically (e.g. check if Jackson is available and, if so, use it for JSON serialization and if not use something else)?
文章还提到Spring Boot已经支持Java 9,而Spring Boot肯定会很多动态加载。那么也许有人知道我可以看一下Spring的代码片段吗?
The article also mentions Spring Boot already supporting Java 9, and Spring Boot definitely does a lot of dynamic loading. So maybe someone knows the priece of code from Spring that I can look at?
推荐答案
首先,要设置记录,我既没有说也没有写上面引用的文字
。我从来没有这么说过。对于所涉及的出版物来说,这只是草率的
报告。
First, to set the record straight, I neither said nor wrote the text quoted above. I’d never have put it that way. That’s just sloppy reporting on the part of the publications involved.
了解Java 9中的类加载和资源
查找最重要的事情是的,从根本上说,它们没有改变。
您可以通过调用 Class :: forName
以及各种<$ c $来搜索类和资源,方法与
一样。 c> getResource * 方法
in Class
和 ClassLoader
类,无论如何是否从类路径或模块路径加载代码
。仍然有三个
内置类加载器,就像在JDK 1.2中一样,并且它们具有
相同的委托关系。因此,许多现有代码只需
就可以开箱即用。
The most important thing to understand about class loading and resource
lookup in Java 9 is that, at a fundamental level, they have not changed.
You can search for classes and resources in the same way that you always
have, by invoking Class::forName
and the various getResource*
methods
in the Class
and ClassLoader
classes, regardless of whether your code
is loaded from the class path or the module path. There are still three
built-in class loaders, just as there were in JDK 1.2, and they have the
same delegation relationships. Lots of existing code therefore just
works, out-of-the-box.
有一些细微差别,如 JEP
261 :内置类加载器的具体类型
已经改变,现在由引导类加载器加载的一些以前
的类现在由平台类
loader加载,以提高安全性。假定
内置类加载器是 URLClassLoader
的现有代码,或者由引导类加载器
加载类的现有代码可能因此需要稍作调整。
There are some nuances, as noted in JEP
261: The concrete type
of the built-in class loaders has changed, and some classes formerly
loaded by the bootstrap class loader are now loaded by the platform class
loader in order to improve security. Existing code which assumes that a
built-in class loader is a URLClassLoader
, or that a class is loaded by
the bootstrap class loader, may therefore require minor adjustments.
最后一个重要区别是模块
中的非类文件资源默认是封装的,因此无法从
外部找到该模块,除非他们的有效套餐是
打开
。
要从您自己的模块加载资源,最好使用 Class
或模块$中的
资源查找方法c $ c>,它可以在您的模块中找到任何
资源,而不是 ClassLoader
中的资源,这可以
只找到非类文件模块的 open
包中的资源。
A final important difference is that non-class-file resources in a module
are encapsulated by default, and hence cannot be located from outside
the module unless their effective package is
open
.
To load resources from your own module it’s best to use the
resource-lookup methods in Class
or Module
, which can locate any
resource in your module, rather than those in ClassLoader
, which can
only locate non-class-file resources in the open
packages of a module.
这篇关于在Java 9中加载类和资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!