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?

3
  • 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, 2010 at 8: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, 2011 at 21:57
  • try using ServiceLoader.load(QueryEngine) and omit the .class. Otherwise you might just be loading the class Class Mar 3, 2016 at 11:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.