How does Java ServiceLoader work during development? (Unit test before creating the JAR?)

The Java ServiceLoader needs these entries to be present in the JAR file. Is there a way to programmatically add these runtime service records for unit testing when inside an IDE? Especially when JARs are not yet built.

+3
source share
1 answer

Do not go in cycles in JAR files. They are the preferred way to encapsulate services, but they are not required. The key really ClassLoader.getResources(String)is where arg Stringeffectively becomes ("META-INF/services/" + serviceClass.getName()). Another bit of information to keep in mind is what the context class loaderServiceLoader.load(Class) uses (of course, you can also use ). So ... what you really need to do is to manipulate the class of the classes or configure the context class loader to make it happy.ServiceLoader.load(Class, ClassLoader)ServiceLoader

+7
source

Source: https://habr.com/ru/post/1752511/


All Articles