-
Notifications
You must be signed in to change notification settings - Fork 38.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revisit SpringFactoriesLoader vs. ServiceLoader infrastructure #27753
Comments
@jhoeller, @sdeleuze and I have discussed this and came up with the following plan:
As part of this issue, we can revisit |
Closing this for the time being since we have no immediate need for closer alignment with the ServiceLoader mechanism. From a core framework perspective, Broader appeal of |
SpringFactoriesLoader
has been added in Spring Framework 3.2 (see 988f376) as a simple and internal factory loading mechanism. At the time, the 3.x generation had a Java 1.5 baseline and could not requirejava.util.ServiceLoader
, introduced in Java 1.6.Over the years, this mechanism has been used more and more by many Spring projects for loading factories. Its contract has also been extended in #15158 for loading factory names and possibly instantiating them "manually" using reflection.
Third party projects are also expected to use the same infrastructure if they want to provide their own support (for example, auto-configurations).
With this issue, the Spring Framework team should discuss whether we should reconsider the current situation, given that:
Java 17 baseline
ServiceLoader
is now mature and has been evolved over the years. TheServiceLoader::stream()
method (added in Java 9) allows to iterate over the types without instantiating them. With that, we could probably replaceSpringFactoriesLoader
.The Java Platform Module System (JPMS)
Again with Java 9, new mechanisms were added to
ServiceLoader
to help with the newly introduced module system.With the JPMS, modules must declare what they export and what they require; without that, they cannot access classes (reflectively or not) if they're not explicitly required by the module descriptor.
With
ServiceLoader
, any module can declare service implementations. By definition, service clients cannot be aware of all of them and are not in a position to declare them in their module descriptor.ServiceLoader
has been improved with new methods and its ownprovides ... with ...
anduses ...
keywords in the module descriptor.SpringFactoriesLoader
will hit the JPMS limitations and doesn't currently have any mechanism to address it.The Spring AOT engine
Spring Native has implemented an AOT engine that pre-processes application configuration and generates Java source files. It greatly helps the GraalVM native compilation phase by making the application startup more explicit and leaner.
Because dynamic class loading and reflection are well-known limitations with native compilation, Spring Native added a specific processing of Spring Factories at build time. This process is listing all factories and their implementations available at build time, and writes a Java source file that explicitly references them so that the GraalVM native static analysis phase discovers them.
Depending on the choice made here, the AOT engine will need to have a
SpringFactoriesLoader
-specific mechanism or possibly extend the existingServiceLoader
support in GraalVM native.The text was updated successfully, but these errors were encountered: