Java SPI - ServiceLoader - adding multiple JAR's to APP classpath

末鹿安然 提交于 2019-12-06 13:09:24

问题


I have a problem implementing this logic in my project. Im using ServiceLoader and I don't know or if it's even possible, but I want to dynamically add JARs to some known directory for app, so the app can load them when it starts. The only thing I achieved was that I know how to create extensible ONE JAR (with META-INF/services), so I can add more and more providers and classes (implementations) to this JAR. The JAR is included to classpath like ordinary library, so app knows about it. What I want to achieve: I don't want to extend one jar file, but I want to extend app by adding more jars like plugins with providers (services), but I don't know how.

Thanks for help!


回答1:


You just need (a) a URLClassLoader that is constructed with the URLs of all the JAR files in those directories, and (b) call ServiceLoader.load(Class service, ClassLoader loader).




回答2:


i think you may create a your customer ClassLoader. The ClassLoader will loader class from jars in dynamic mode. Many framework have some function very like what you want. you can look some code in them. the WebAppClassLoader class in jetty may a start point!



来源:https://stackoverflow.com/questions/11447699/java-spi-serviceloader-adding-multiple-jars-to-app-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!