# 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: tutorial

Language: en

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

Topics: [Java](<https://devfeed.tech/topics/java.md>), [Code](<https://devfeed.tech/topics/code.md>), [JSON Web Tokens](<https://devfeed.tech/topics/jwt.md>), [JSON](<https://devfeed.tech/topics/json.md>)

Tags: [decoupling](<https://devfeed.tech/tags/decoupling.md>), [design-patterns](<https://devfeed.tech/tags/design-patterns.md>), [java](<https://devfeed.tech/tags/java.md>), [json](<https://devfeed.tech/tags/json.md>), [jwt](<https://devfeed.tech/tags/jwt.md>), [plugin](<https://devfeed.tech/tags/plugin.md>), [plugins](<https://devfeed.tech/tags/plugins.md>), [serviceloader](<https://devfeed.tech/tags/serviceloader.md>), [spi](<https://devfeed.tech/tags/spi.md>)

## AI overview

This article presents a provider-factory pattern for Java ServiceLoader. Instead of loading a service implementation directly, ServiceLoader loads a small factory with a no-argument constructor; the factory then constructs the service and can support implementation selection and custom constructors. The pattern is illustrated with a mock payments system and is related to decoupling JSON and JWT capabilities from concrete libraries.

## 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.