ClassLoader.getResources vs Module.getResourceAsStream
Remi Forax
forax at univ-mlv.fr
Mon Jul 16 08:09:22 UTC 2018
----- Mail original -----
> De: "Sander Mak" <sander.mak at luminis.eu>
> À: "jigsaw-dev" <jigsaw-dev at openjdk.java.net>
> Envoyé: Lundi 16 Juillet 2018 08:52:41
> Objet: Re: ClassLoader.getResources vs Module.getResourceAsStream
> In that case you'd expose the factories through the services mechanism
> (`provides com.acme.api.MyWidgetFactory with
> com.acme.factories.XmlBasedWidgetFactory` in the module descriptor). Or, if you
> must expose the XML itself to the outside world rather than the factories, you
> can create a service that offers the XML as String or InputStream. Each module
> then has an implementation reading its own encapsulated XML. The former
> approach is preferable IMO.
>
> Note that you can also use the services mechanism without module descriptors by
> placing text files in META-INF/services
> (https://docs.oracle.com/javase/tutorial/ext/basics/spi.html). However, if your
> code isn't a reusable library itself, you can also choose to modularize your
> own code and depend on non-modularized dependencies through their automatic
> module name
> (http://branchandbound.net/blog/java/2017/12/automatic-module-name/).
>
You have to do both, each service declared in a module-info MUST be declared in META-INF/services too because you can use a modular jar in the classpath as a plain old jar.
Alan, we should patch jar to warn when there is a module-info.class that declare services with no corresponding META-INF/services.
> Sander
>
Rémi
> On 14 Jul 2018, at 17:58, Michał Zegan
> <webczat_200 at poczta.onet.pl<mailto:webczat_200 at poczta.onet.pl>> wrote:
>
> It is a completely new code. It is not modularized for now because some
> dependencies are not modularized, but I want it to be compatible to ease
> later modularization.
> My actual goal is to load xml files defining and configuring some
> factories, from all modules that contain them. Not sure how would you do
> that with services?
>
> W dniu 14.07.2018 o 17:53, Alan Bateman pisze:
> On 14/07/2018 16:38, Michał Zegan wrote:
> What is then a recommendation for searching for all resources with name
> x that i can access? Something like load all configuration including
> some kind of extensions. I cannot list resources easily.
>
> Services is the cleaner way to do this kind of thing, esp. if you have
> flexibility to change the code and move away from legacy ClassLoader
> getResources.
>
> -Alan
More information about the jigsaw-dev
mailing list