A Java geek
Read post

Making ServiceLoader usable: a provider factory

Java's ServiceLoader has two well-known limitations: it requires a no-arg constructor and provides no built-in selection mechanism. This post presents a provider factory pattern that addresses both. Instead of loading the service directly, you load a lightweight factory (ServiceProvider<S>) that carries an ID, a priority, and a supports() check. The real service is constructed inside create() with whatever constructor it needs. A generic ServiceRegistry loads all providers once into an immutable list sorted by priority, then exposes get(id, cfg) and getDefault(cfg) for selection. Caching is added as a decorator using ConcurrentHashMap, lifecycle reuses AutoCloseable, and cross-cutting concerns attach via a UnaryOperator post-processor. The pattern requires no dependencies beyond the JDK, works on both classpath and module path, and avoids dynamic proxies — making it GraalVM native-image friendly.

    #java#design-patterns#dependency-injection
Jul 12•11m read time•From blog.frankel.ch
Post cover image
3.3K Impressions
A Java geek's image
A Java geek

Frankel's platform covers topics related to software development, technology trends, and coding tut...

83 Followers

•

1.2K Upvotes

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard