java.util.ServiceLoader not loading my provider class -
i trying put basic spi-based registry of handlers, lookup handlerregistry. when use serviceloader.load(handler.class) initialize providers, , iterate list lazily load them, not seeing instances of class. keeping simple possible, handlerregistry class is:
public class handlerregistry { private static handlerregistry registry; private serviceloader<handler> handlerloader; private handlerregistry() { handlerloader = serviceloader.load(handler.class); } public static synchronized handlerregistry getregistry() { if (registry == null) { registry = new handlerregistry(); registry.init(); } return registry; } private void init() { system.out.println("handlerregistry.init()"); } public handler lookup(string item) { system.out.println("lookup("+item+")"); try { iterator<handler> = handlerloader.iterator(); while (it.hasnext()) { handler handler = it.next(); system.out.println("found handler "+handler); } } catch (serviceconfigurationerror err) { err.printstacktrace(); } return null; } }
i have com.example.handler.handler interface (empty simplicity), , com.example.handler.handlers.dummyhandler class implements interface. have created file in jar called meta-inf/services/com.example.handler.handler, contains single line
com.example.handler.handlers.dummyhandler
according javadoc. unit test calls lookup() method verify looking handler item. of course there eventulaly need check of kind see if right handler item, @ point not seeing dummyhandler class loaded registry. doing wrong here?
thanks!
the answer appears in sensitivity how configured. had been placing provider name resource file (the 1 named com.example.handler.handler) directly in top level project directory, i.e., /resources/meta-inf/services/com.example.handler.handler. had configured build.gradle pull file out , put jar:
jar { from('resources') { include 'meta-inf/services/*.*' } }
when inspected jar file, file there, right expected be, thought well. on kick, happened move resources folder down under src/main, , presto! works. inspected jar file , appears identical 1 built previous way, reason 1 works. update further if can determine difference, @ least test case works now.
Comments
Post a Comment