You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Iterating over the ServiceLoader results in class- and resource-loading, which becomes expensive if done extensively.
A common pattern used (in e.g. "com.sun.xml.ws.api.pipe.TransportTubeFactory::create") is to search first for multiple factory-implementations before falling back to a "default" factory / implementation:
public Type exampleFunc(...) {
for (_ : ServiceFinder.find(FactoryType1.class) { return if FactoryType1-impl found }
for (_ : ServiceFinder.find(FactoryType2.class) { return if FactoryType2-impl found }
return DEFAULT_FACTORY.createType(..);
}
If there are no other implementations present besides the default-fallback-implementation, then each call to a method with this structure starts searching (again) for all non-default implementations - only to not find any implementing classes and finally falling back to the default-implementation.
Invoking such method-structures often, results in multiple unnecessary ServiceLoader-calls, because if the corresponding service-class and classloader are identical to a previous call and for this previous call the classloader was not able to determine the service-implementation, then it still won't be able to find it when retrying the ServiceLoader-call with the same parameters.
0 commit comments