简体   繁体   中英

Supporting 2 versions of an API in Java

I'm trying to find a way to support 2 different versions of an API in my code base. They have the same package names but work much differently under the hood (two completely different systems). How can I do this?

Both of these API's also have a dependency on Bouncy Castle but they use different versions of it. How do I also take this into account?

I would not recommend this unless you know what you're doing but you could use a URLClassLoader as follows:

URLClassLoader classLoaderA = URLClassLoader.newInstance(new URL[] {new URL("versionA.jar")});
URLClassLoader classLoaderB = URLClassLoader.newInstance(new URL[] {new URL("versionB.jar")});

Load the class:

classLoaderA.loadClass("SomeClass");

Another option is to have a look at OSGI .

The solution I would start with is... Loading the API into a custom class loader that loads the child class over the parent class first. If you compile Bouncy Castle inside of the API then you wont need to worry about loading it separately. If you dynamically load the Bouncy Castle jar during run time, then in the custom class loader, you would add Bouncy Castle and your API to that class loader. Using URLClassLoader and see my link below for Parent last loading.

How do I create a parent-last / child-first ClassLoader in Java, or How to override an old Xerces version that was already loaded in the parent CL?

您可以创建一个自定义类加载器,以根据加载一个版本的API与另一个版本所需的逻辑加载相应的类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM