4

I've used the JDK's SPI mechanism in numerous other applications without any problem; however, I can't seem to get it to work within Grails.

I've tried the usual code (shown below) both from within a static initializer, and from within a class constructor, but neither worked.

ServiceLoader loader = ServiceLoader.load(QueryEngine.class);
Iterator<QueryEngine> it = loader.iterator();

The iterator doesn't actually iterate through the implementing classes.

I've also tried:

ServiceLoader.load(QueryEngine.class, Thread.currentThread().getContextClassLoader());

which should use the classloader for the current class, but that doesn't work either.

Is there some other mechanism I should be using to load implementing classes?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
mfortner
  • 331
  • 2
  • 6
  • Do you have appropriate file in META-INF/services folder? It should be named like full name of interface/abstract class (for instance "example.QueryEngine") and contain all implementation of it (like "example.QueryEngineImpl"). – Maxym Dec 16 '10 at 08:24
  • Yes. I can use the same classes in a Java program and it works without any problem. Makes me wonder if there's a some kinda Grails classloader magic going on which hoses the ServiceLoader. – mfortner Jan 26 '11 at 21:57
  • try using ServiceLoader.load(QueryEngine) and omit the .class. Otherwise you might just be loading the class Class – Niels Bech Nielsen Mar 03 '16 at 11:53

0 Answers0