# Making ServiceLoader usable: a provider factory

DevFeed: [Making ServiceLoader usable: a provider factory](<https://devfeed.tech/articles/making-serviceloader-usable-a-provider-factory-18932.md>)

Original publisher: [Read original article](<https://blog.frankel.ch/serviceloader-provider-factory/>)

Author: Stefano Fago

Published: 2026-07-12T00:00:00Z

Content type: article

Language: en

Sources: [Nicolas Fränkel](<https://devfeed.tech/sources/nicolas-frankel.md>)

Topics: [serviceloader](<https://devfeed.tech/topics/serviceloader.md>), [adapter pattern](<https://devfeed.tech/topics/adapter-pattern.md>), [JSON Web Tokens](<https://devfeed.tech/topics/jwt.md>)

Tags: [constructor](<https://devfeed.tech/tags/constructor.md>), [decoupling](<https://devfeed.tech/tags/decoupling.md>), [dependency](<https://devfeed.tech/tags/dependency.md>), [design-patterns](<https://devfeed.tech/tags/design-patterns.md>), [discovery](<https://devfeed.tech/tags/discovery.md>), [environment](<https://devfeed.tech/tags/environment.md>), [example](<https://devfeed.tech/tags/example.md>), [factory](<https://devfeed.tech/tags/factory.md>), [java](<https://devfeed.tech/tags/java.md>), [json](<https://devfeed.tech/tags/json.md>), [jwt](<https://devfeed.tech/tags/jwt.md>), [key](<https://devfeed.tech/tags/key.md>), [limits](<https://devfeed.tech/tags/limits.md>), [load](<https://devfeed.tech/tags/load.md>), [loader](<https://devfeed.tech/tags/loader.md>), [mock](<https://devfeed.tech/tags/mock.md>), [payments](<https://devfeed.tech/tags/payments.md>), [paypal](<https://devfeed.tech/tags/paypal.md>), [plugin](<https://devfeed.tech/tags/plugin.md>), [serviceloader](<https://devfeed.tech/tags/serviceloader.md>), [spi](<https://devfeed.tech/tags/spi.md>)

## AI overview

The proposed pattern loads a small factory through ServiceLoader instead of loading the service implementation directly. The factory has the required public no-argument constructor and creates the real service, which can take configuration such as a payments provider's API key. This addresses ServiceLoader's constructor constraint. The factory also provides a place to select among implementations, since ServiceLoader itself returns discovered providers in roughly classpath order without IDs, priorities, or applicability checks. The author says Java 9's static provider method can construct an object but does not solve selection. The excerpt introduces the pattern but ends before showing how selection is implemented.

## Source excerpt

I keep coming back to java.util.ServiceLoader. I have used it to put a JSON layer behind a contract, so the core code carries no direct dependency on any particular JSON library, and I can swap the implementation without touching callers. The same shape works for JWT handling, where the concrete library might be jose4j or another JOSE implementation, and you can easily find other decoupling use-cases.