简体   繁体   中英

Instantiate default class loader more than once

I have two versions of a dependence java library in a small part of a really big java spring project that I really have to use in combination for some time, however:

-the new version is not backward compatible with the older one

-both versions use the same class uris

Thus, I ended up trying to instatiate both library versions separately somehow because the default class loader keeps keeping the first instance and things are going crazy. I searched a little for some hours, many people recommend OSGi but I don't want to integrate all that framework just for this little. Also, I tried jarjar via maven but I didn's seem to work for the target dependency jars (I think they are obfuscated anyway). So, I am trying to simply instatiate a separate ClassLoader that can do the trick. Any of you know any trivial way to do that?

Can you encapsulate usage of the framework to small set of functions, that could be described by an interface?

If so, and if you are in an Application-Server Environment, you could do something like this:

Define your Interface and put it to the lib-Folder of your app-server with two factory-methods. Your app goes to WebArchive1 Framework in Version 1 goes to WebArchive2 and populates factory-method1 from your lib-Folder Framework in Version 2 goes to WebArchive3 and populates factory-method2 from your lib-Folder

--> lib
public interface MyInterface {
   // do Stuff
}

public class MyFactory {
  private static MyInterface v1;
  private static MyInterface v2;

  public MyInterface getV1() {...}
  public MyInterface getV1() {...}
}

--> WebArchive2
MyFactory.setV1(new Lib());

--> WebArchive3
MyFactory.setV2(new Lib());

Nevermind, problem fixed the easy way. I contacted the library authors and they refactored their old version package for me . So no class loader hacks needed anymore :)

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