Java: Dynamically Load Multiple Versions of Same Class

前端 未结 7 894
一整个雨季
一整个雨季 2021-01-01 01:14

What I\'d like to be able to do is to load set of classes, probably all in the same folder. All of which implement the same interface and are the same class, then in my code

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 01:41

    1. If you can use OSGI, its as simple as snapping a finger! In oSGI you can have multiple verssions of the same class. All you do is have same bundles with different versions.

    2. Otherwise you can still write your custom class loader that reads both the classes. One way of doing it would be like this. You write two ClassLoaders, one of them loads one version of the class and the other loads the other version of the class. Now based on the need you choose the classloader1 or classloader2 to load the class. So now you can also have multiple versions of the same class loaded simultaneously in the memory.

    Note: Make sure this is actually you want to do, there may be other ways of coming around your problem.

提交回复
热议问题