When to use ServiceLoader?

When to use ServiceLoader?



I came across the documentation of ServiceLoader and am unclear as to what use cases it suits.


ServiceLoader



When would one use ServiceLoader?


ServiceLoader




4 Answers
4



You use ServiceLoader when you want your program to have a “plugin” functionality. When you want to allow people to customize your application by adding jar files to the classpath which contain implementations of a specific subset of functionality, you can use a ServiceLoader to look for those implementations in the classpath.



ServiceLoader is itself an implementation of the jar SPI specification, which has been around for a long time. (I believe it was introduced in Java 1.3.)



Java SE already uses it for exactly that purpose in a lot of places, including:






the docs say it comes from Java 1.6

– Andrew Tobilko
Sep 6 '18 at 14:57






@AndrewTobilko Yes, ServiceLoader was introduced in 1.6 as a convenient way to accomplish what the jar specification had already documented for many years prior. Before ServiceLoader, one would have to do it manually with something like classLoader.getResources("/META-INF/services/" + spiClass.getName()).

– VGR
Sep 6 '18 at 21:48



classLoader.getResources("/META-INF/services/" + spiClass.getName())



ServiceLoader is Java's light weight alternative to a full blown IoC container such as Spring, Guice, etc. It has far less bells and whistles than those frameworks, but works well for basic use cases when you just want to find what classes implement an interface.


ServiceLoader



Most application servers will have some usages of ServiceLoader you can see in practice:



https://github.com/apache/tomee/search?q=ServiceLoader&unscoped_q=ServiceLoader



https://github.com/apache/tomcat/search?q=ServiceLoader&unscoped_q=ServiceLoader



https://github.com/wildfly/wildfly/search?q=ServiceLoader&unscoped_q=ServiceLoader



Are you familiar with the principle "Inversion of Control"?



Java implemented it by ServiceLoader. The class is designed to locate implementation classes of an interface on the classpath. You pass in a service interface, you get an implementation(s) of that service.


ServiceLoader



You might find a good practical example here.



P.S: Even though it's an out-of-the-box solution and a quite simple tool, I think it's outdated and not flexible enough compared to the Spring IoC Container and Google Guice.



When I hear something about ServiceLoader, I start to think about JDBC. This technology loads jdbc driver classes from classpath, which it can find. It let us not write Class.forName before using jdbc.


ServiceLoader


Class.forName



I am sure many examples of using ServiceLoader, except JDBC exist


ServiceLoader



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ