简体   繁体   English

从相同名称的包中引用相同名称的不同类

[英]Referring different classes of same name from packages of same name

We have a process which needs to refer two different encryption classes having same name, at different times. 我们有一个过程,需要在不同的时间引用具有相同名称的两个不同的加密类。 Both the class names are same with same package path "com.abc.security.encryption". 这两个类名都相同,并且包路径相同:“ com.abc.security.encryption”。

Both the classes have same package name com.abc.security.encryption, however they are present in different jar files. 这两个类具有相同的包名称com.abc.security.encryption,但是它们存在于不同的jar文件中。

Let's say ENCRYPTION.class(new logic) present in Jar A and ENCRYPTION.class(old logic) is present in Jar B. Now in my process, when we call Jar B API which refers ENCRYPTION.class, is referring to ENCRYPTION.class(new logic) present in Jar A instead of ENCRYPTION.class(old logic) present in Jar B . 假设Jar A中存在ENCRYPTION.class(新逻辑),Jar B中存在ENCRYPTION.class(旧逻辑)。现在在我的过程中,当我们调用引用ENCRYPTION.class的Jar B API时,就是引用ENCRYPTION.class (新逻辑)存在于Jar A中,而不是JAR B中存在的ENCRYPTION.class(旧逻辑)。

Until I delete Jar A having ENCRYPTION.class(new logic), the ENCRYPTION.class(old logic) present in Jar B is not referred. 在删除具有ENCRYPTION.class(新逻辑)的Jar A之前,不会引用Jar B中存在的ENCRYPTION.class(旧逻辑)。

Since both the encryption logic are from different utility modules being used by many different modules, i am not able to ask them to change the name of the package. 由于两种加密逻辑都来自许多不同模块使用的不同实用程序模块,因此我无法要求它们更改程序包的名称。

I need a way to make sure both the logic are referred at required places without changing anything in those modules. 我需要一种方法来确保两个逻辑都在所需的位置被引用,而无需更改那些模块中的任何内容。 Can anything be changed in the class paths of my process or in the code, so that calling Jar B API, calls ENCRYPTION.class(old logic) present in Jar B itself. 可以在我的过程的类路径或代码中进行任何更改,以便调用Jar B API并调用Jar B本身中存在的ENCRYPTION.class(旧逻辑)。 And when i call direct ENCRYPTION.class it should refer to ENCRYPTION.class(new logic) present in Jar A. 当我直接调用ENCRYPTION.class时,它应该引用Jar A中存在的ENCRYPTION.class(新逻辑)。

Tried by adding the first class path as "." 尝试通过将第一类路径添加为“”。 for the process. 这个过程。 But it did not solve the issue. 但这并没有解决问题。 Your help is most appreciated. 非常感谢您的帮助。

Thanks, Nvn 谢谢,Nvn

You should remove the problematic jar from the classpath. 您应该从类路径中删除有问题的jar。 A classpath with multiple jars that contain the same fully qualified class names is a recipe for disaster. 具有多个包含相同完全限定的类名的jar的类路径是灾难的秘诀。

If that's not an option, you might be able to create a custom class loader which does this swapping. 如果这不是一个选择,则您可能可以创建一个自定义类加载器来执行此交换。 But it probably won't be easy. 但这可能并不容易。 There's a similar question about this which might get you started if you go down this road: Unloading classes in java? 还有一个类似的问题,如果您走这条路,它可能会让您入门: 卸载Java中的类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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