How does Java serviceloader work during development? (unit tests before building jars?)

Java's serviceloader requires these entries to exist in the jar file Is there any way to programmatically add these service entries at runtime for unit testing? Especially when jars are not yet built

Solution

Don't focus too much on jar files They are the preferred way to encapsulate services, but they are not required The key is actually classloader Getresources (string) – string Arg actually becomes ("meta inf / services /" serviceclass. Getname()) Another piece of information to keep in mind is serviceloader Load (class) uses context class loader (of course, you can also use serviceloader. Load (class, classloader)) So... What you really need to do is manipulate the classpath or configure the context classloader to satisfy the serviceloader

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>