My question is based on this question
Java SPI - Choosing a single implementation
When we have multiple implementations is there any way (how to do it) to select one specific. From my standpoint it just doesn't make sense to do blindingly like this
ServiceLoader.load(SomeService.class)
.forEach(service -> service.test());
I.e.
if I have three implementations of SomeService
interface SomeService {
public void test();
}
public class SomeInterfaceImpl1 implements SomeService {
<...>
}
public class SomeInterfaceImpl2 implements SomeService {
<...>
}
public class SomeInterfaceImpl3 implements SomeService {
<...>
}
How to select an implementation of SomeInterfaceImpl2?
Would doing something like this make any sense?
ServiceLoader.load(SomeService.class)
.stream()
.map(Provider::get)
.filter( s -> s instanceof SomeInterfaceImp2)
.limit(1)
.forEach(s -> s.test())
.