I wrote a generic ServiceLocator
using the ServiceProviderPattern.
public <T> List<T> locateAll(final Class<T> clazz) {
final Iterator<T> iterator = ServiceLoader.load(clazz).iterator();
final List<T> services = new ArrayList<T>();
while (iterator.hasNext()) {
try {
services.add(iterator.next());
} catch (ServiceConfigurationError serviceError) {
serviceError.printStackTrace(System.err);
}
}
return services;
}
It only finds services if I add the service as a dependency to the maven module where the ServiceLocator
resists. If this service uses the ServiceLocator
itself, I will have a cirular dependency.
I need the ServiceLocator
to load classes from not known dependencies.